IDBackView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // IDBackView.m
  3. // ORC
  4. //
  5. // Created by lorin on 2017/6/21.
  6. // Copyright © 2017年 lorin. All rights reserved.
  7. //
  8. #import "IDBackView.h"
  9. #import "xingchuangke-Swift.h"
  10. // iPhone5/5c/5s/SE 4英寸 屏幕宽高:320*568点 屏幕模式:2x 分辨率:1136*640像素
  11. #define iPhone5or5cor5sorSE ([UIScreen mainScreen].bounds.size.height == 568.0)
  12. // iPhone6/6s/7 4.7英寸 屏幕宽高:375*667点 屏幕模式:2x 分辨率:1334*750像素
  13. #define iPhone6or6sor7 ([UIScreen mainScreen].bounds.size.height == 667.0)
  14. // iPhone6 Plus/6s Plus/7 Plus 5.5英寸 屏幕宽高:414*736点 屏幕模式:3x 分辨率:1920*1080像素
  15. #define iPhone6Plusor6sPlusor7Plus ([UIScreen mainScreen].bounds.size.height == 736.0)
  16. @interface IDBackView () {
  17. CAShapeLayer *_IDCardScanningWindowLayer;
  18. NSTimer *_timer;
  19. }
  20. @end
  21. @implementation IDBackView
  22. -(instancetype)initWithFrame:(CGRect)frame{
  23. if (self=[super initWithFrame:frame]) {
  24. self.backgroundColor = [UIColor clearColor];
  25. // 添加扫描窗口
  26. [self addScaningWindow];
  27. // 添加定时器
  28. [self addTimer];
  29. }
  30. return self;
  31. }
  32. #pragma mark - 添加扫描窗口
  33. -(void)addScaningWindow {
  34. // 中间包裹线
  35. _IDCardScanningWindowLayer = [CAShapeLayer layer];
  36. _IDCardScanningWindowLayer.position = self.layer.position;
  37. CGFloat width = iPhone5or5cor5sorSE? 240: (iPhone6or6sor7? 270: 300);
  38. _IDCardScanningWindowLayer.bounds = (CGRect){CGPointZero, {width, width * 1.574}};
  39. _IDCardScanningWindowLayer.cornerRadius = 15;
  40. _IDCardScanningWindowLayer.borderColor = [UIColor whiteColor].CGColor;
  41. _IDCardScanningWindowLayer.borderWidth = 1.5;
  42. [self.layer addSublayer:_IDCardScanningWindowLayer];
  43. // 最里层镂空
  44. UIBezierPath *transparentRoundedRectPath = [UIBezierPath bezierPathWithRoundedRect:_IDCardScanningWindowLayer.frame cornerRadius:_IDCardScanningWindowLayer.cornerRadius];
  45. // 最外层背景
  46. UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.frame];
  47. [path appendPath:transparentRoundedRectPath];
  48. [path setUsesEvenOddFillRule:YES];
  49. CAShapeLayer *fillLayer = [CAShapeLayer layer];
  50. fillLayer.path = path.CGPath;
  51. fillLayer.fillRule = kCAFillRuleEvenOdd;
  52. fillLayer.fillColor = [UIColor blackColor].CGColor;
  53. fillLayer.opacity = 0.6;
  54. [self.layer addSublayer:fillLayer];
  55. // 提示标签
  56. CGPoint center = self.center;
  57. center.x = CGRectGetMaxX(_IDCardScanningWindowLayer.frame) + 20;
  58. [self addTipLabelWithText:@"将银行卡置于此区域内扫描" center:center];
  59. }
  60. #pragma mark - 添加提示标签
  61. -(void )addTipLabelWithText:(NSString *)text center:(CGPoint)center {
  62. UILabel *tipLabel = [[UILabel alloc] init];
  63. tipLabel.text = text;
  64. tipLabel.textColor = [UIColor whiteColor];
  65. tipLabel.textAlignment = NSTextAlignmentCenter;
  66. tipLabel.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
  67. [tipLabel sizeToFit];
  68. tipLabel.center = center;
  69. [self addSubview:tipLabel];
  70. }
  71. #pragma mark - 添加定时器
  72. -(void)addTimer {
  73. _timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];
  74. [_timer fire];
  75. }
  76. -(void)timerFire:(id)notice {
  77. [self setNeedsDisplay];
  78. }
  79. -(void)dealloc {
  80. [_timer invalidate];
  81. }
  82. - (void)drawRect:(CGRect)rect {
  83. rect = _IDCardScanningWindowLayer.frame;
  84. // 人像提示框
  85. UIBezierPath *facePath = [UIBezierPath bezierPathWithRect:_facePathRect];
  86. facePath.lineWidth = 1.5;
  87. [[UIColor whiteColor] set];
  88. [facePath stroke];
  89. // 水平扫描线
  90. CGContextRef context = UIGraphicsGetCurrentContext();
  91. static CGFloat moveX = 0;
  92. static CGFloat distanceX = 0;
  93. CGContextBeginPath(context);
  94. CGContextSetLineWidth(context, 2);
  95. CGContextSetRGBStrokeColor(context,0.3,0.8,0.3,0.8);
  96. CGPoint p1, p2;// p1, p2 连成水平扫描线;
  97. moveX += distanceX;
  98. if (moveX >= CGRectGetWidth(rect) - 2) {
  99. distanceX = -2;
  100. } else if (moveX <= 2){
  101. distanceX = 2;
  102. }
  103. p1 = CGPointMake(CGRectGetMaxX(rect) - moveX, rect.origin.y);
  104. p2 = CGPointMake(CGRectGetMaxX(rect) - moveX, rect.origin.y + rect.size.height);
  105. CGContextMoveToPoint(context,p1.x, p1.y);
  106. CGContextAddLineToPoint(context, p2.x, p2.y);
  107. CGContextStrokePath(context);
  108. }
  109. @end