2
0

LBXScanLineAnimation.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // LBXScanLineAnimation.m
  3. //
  4. //
  5. // Created by lbxia on 15/11/3.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "LBXScanLineAnimation.h"
  9. @interface LBXScanLineAnimation()
  10. {
  11. int num;
  12. BOOL down;
  13. NSTimer * timer;
  14. BOOL isAnimationing;
  15. }
  16. @property (nonatomic,assign) CGRect animationRect;
  17. @end
  18. @implementation LBXScanLineAnimation
  19. - (void)stepAnimation
  20. {
  21. if (!isAnimationing) {
  22. return;
  23. }
  24. CGFloat leftx = _animationRect.origin.x + 5;
  25. CGFloat width = _animationRect.size.width - 10;
  26. self.frame = CGRectMake(leftx, _animationRect.origin.y + 8, width, 8);
  27. self.alpha = 0.0;
  28. self.hidden = NO;
  29. __weak __typeof(self) weakSelf = self;
  30. [UIView animateWithDuration:0.5 animations:^{
  31. weakSelf.alpha = 1.0;
  32. } completion:^(BOOL finished)
  33. {
  34. }];
  35. [UIView animateWithDuration:3 animations:^{
  36. CGFloat leftx = _animationRect.origin.x + 5;
  37. CGFloat width = _animationRect.size.width - 10;
  38. weakSelf.frame = CGRectMake(leftx, _animationRect.origin.y + _animationRect.size.height - 8, width, 4);
  39. } completion:^(BOOL finished)
  40. {
  41. self.hidden = YES;
  42. [weakSelf performSelector:@selector(stepAnimation) withObject:nil afterDelay:0.3];
  43. }];
  44. }
  45. - (void)startAnimatingWithRect:(CGRect)animationRect InView:(UIView *)parentView Image:(UIImage*)image
  46. {
  47. if (isAnimationing) {
  48. return;
  49. }
  50. isAnimationing = YES;
  51. self.animationRect = animationRect;
  52. down = YES;
  53. num =0;
  54. CGFloat centery = CGRectGetMinY(animationRect) + CGRectGetHeight(animationRect)/2;
  55. CGFloat leftx = animationRect.origin.x + 5;
  56. CGFloat width = animationRect.size.width - 10;
  57. self.frame = CGRectMake(leftx, centery+2*num, width, 2);
  58. self.image = image;
  59. [parentView addSubview:self];
  60. [self startAnimating_UIViewAnimation];
  61. // [self startAnimating_NSTimer];
  62. }
  63. - (void)startAnimating_UIViewAnimation
  64. {
  65. [self stepAnimation];
  66. }
  67. - (void)startAnimating_NSTimer
  68. {
  69. timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(scanLineAnimation) userInfo:nil repeats:YES];
  70. }
  71. -(void)scanLineAnimation
  72. {
  73. CGFloat centery = CGRectGetMinY(_animationRect) + CGRectGetHeight(_animationRect)/2;
  74. CGFloat leftx = _animationRect.origin.x + 5;
  75. CGFloat width = _animationRect.size.width - 10;
  76. if (down)
  77. {
  78. num++;
  79. self.frame = CGRectMake(leftx, centery+2*num, width, 2);
  80. if (centery+2*num > (CGRectGetMinY(_animationRect) + CGRectGetHeight(_animationRect) - 5 ) )
  81. {
  82. down = NO;
  83. }
  84. }
  85. else {
  86. num --;
  87. self.frame = CGRectMake(leftx, centery+2*num, width, 2);
  88. if (centery+2*num < (CGRectGetMinY(_animationRect) + 5 ) )
  89. {
  90. down = YES;
  91. }
  92. }
  93. }
  94. - (void)dealloc
  95. {
  96. [self stopAnimating];
  97. }
  98. - (void)stopAnimating
  99. {
  100. if (isAnimationing) {
  101. isAnimationing = NO;
  102. if (timer) {
  103. [timer invalidate];
  104. timer = nil;
  105. }
  106. [self removeFromSuperview];
  107. }
  108. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  109. }
  110. @end