ScanAnimationView.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // ScanAnimationView.m
  3. //
  4. //
  5. // Created by Megatron on 11/6/15.
  6. // Copyright © 2015 Megatron. All rights reserved.
  7. //
  8. #import "ScanAnimationView.h"
  9. @interface ScanAnimationView ()
  10. {
  11. __block CGFloat _addHeight;
  12. }
  13. @property (nonatomic,strong) UIImageView *bgImage; // 背景图片
  14. @property (nonatomic,strong) UIImageView *moveView; // 扫描横线
  15. @end
  16. @implementation ScanAnimationView
  17. -(instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.backgroundColor = [UIColor clearColor];
  22. self.bgImage = [[UIImageView alloc] initWithFrame:self.bounds];
  23. self.bgImage.image = [UIImage imageNamed:@"scan_round.png"];
  24. [self addSubview:self.bgImage];
  25. CGFloat myWidth = self.bounds.size.width;
  26. CGFloat myBorder = 5;
  27. self.moveView = [[UIImageView alloc] initWithFrame:CGRectMake(myBorder, myBorder, myWidth - myBorder * 2, 6)];
  28. // self.moveView.backgroundColor = [UIColor colorWithRed:44/255.0 green:248/255.0 blue:152/255.0 alpha:1];
  29. self.moveView.image = [UIImage imageNamed:@"scan_move_line"];
  30. [self addSubview:self.moveView];
  31. }
  32. return self;
  33. }
  34. //开始扫描动画
  35. -(void)startScanAnimation
  36. {
  37. self.moveView.hidden = NO;
  38. CGFloat myWidth = self.bounds.size.width;
  39. CGFloat myBorder = 5;
  40. _addHeight = self.moveView.frame.origin.y;
  41. if (_addHeight >= self.frame.size.height - myBorder) {
  42. _addHeight = myBorder - 10;
  43. }
  44. [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^(){
  45. self.moveView.frame = CGRectMake(myBorder, _addHeight + 10, myWidth - myBorder * 2, 6);
  46. }completion:^(BOOL ifCompeleted)
  47. {
  48. if (ifCompeleted) {
  49. [self startScanAnimation];
  50. }
  51. }];
  52. }
  53. @end