2
0

LBXScanNetAnimation.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // LBXScanLineAnimation.m
  3. //
  4. //
  5. // Created by lbxia on 15/11/3.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "LBXScanNetAnimation.h"
  9. @interface LBXScanNetAnimation()
  10. {
  11. BOOL isAnimationing;
  12. }
  13. @property (nonatomic,assign) CGRect animationRect;
  14. @property (nonatomic,strong) UIImageView *scanImageView;
  15. @end
  16. @implementation LBXScanNetAnimation
  17. - (instancetype)init{
  18. self = [super init];
  19. if (self) {
  20. self.clipsToBounds = YES;
  21. [self addSubview:self.scanImageView];
  22. }
  23. return self;
  24. }
  25. - (UIImageView *)scanImageView{
  26. if (!_scanImageView) {
  27. _scanImageView = [[UIImageView alloc] init];
  28. }
  29. return _scanImageView;
  30. }
  31. - (void)stepAnimation
  32. {
  33. if (!isAnimationing) {
  34. return;
  35. }
  36. self.frame = _animationRect;
  37. CGFloat scanNetImageViewW = self.frame.size.width;
  38. CGFloat scanNetImageH = self.frame.size.height;
  39. __weak __typeof(self) weakSelf = self;
  40. self.alpha = 0.5;
  41. _scanImageView.frame = CGRectMake(0, -scanNetImageH, scanNetImageViewW, scanNetImageH);
  42. [UIView animateWithDuration:1.4 animations:^{
  43. weakSelf.alpha = 1.0;
  44. _scanImageView.frame = CGRectMake(0, scanNetImageViewW-scanNetImageH, scanNetImageViewW, scanNetImageH);
  45. } completion:^(BOOL finished)
  46. {
  47. [weakSelf performSelector:@selector(stepAnimation) withObject:nil afterDelay:0.3];
  48. }];
  49. }
  50. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  51. [self performSelector:@selector(stepAnimation) withObject:nil afterDelay:0.3];
  52. }
  53. - (void)startAnimatingWithRect:(CGRect)animationRect InView:(UIView *)parentView Image:(UIImage*)image
  54. {
  55. [self.scanImageView setImage:image];
  56. self.animationRect = animationRect;
  57. [parentView addSubview:self];
  58. self.hidden = NO;
  59. isAnimationing = YES;
  60. [self stepAnimation];
  61. }
  62. - (void)dealloc
  63. {
  64. [self stopAnimating];
  65. }
  66. - (void)stopAnimating
  67. {
  68. self.hidden = YES;
  69. isAnimationing = NO;
  70. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  71. }
  72. @end