123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #import "LHSIDCardScaningView.h"
- #define iPhone5or5cor5sorSE ([UIScreen mainScreen].bounds.size.height == 568.0)
- #define iPhone6or6sor7 ([UIScreen mainScreen].bounds.size.height == 667.0)
- #define iPhone6Plusor6sPlusor7Plus ([UIScreen mainScreen].bounds.size.height == 736.0)
- @interface LHSIDCardScaningView () {
- CAShapeLayer *_IDCardScanningWindowLayer;
- NSTimer *_timer;
- }
- @end
- @implementation LHSIDCardScaningView
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.backgroundColor = [UIColor clearColor];
-
-
- [self addScaningWindow];
-
-
- [self addTimer];
- }
-
- return self;
- }
- #pragma mark - 添加扫描窗口
- -(void)addScaningWindow {
-
- _IDCardScanningWindowLayer = [CAShapeLayer layer];
- _IDCardScanningWindowLayer.position = self.layer.position;
- CGFloat width = iPhone5or5cor5sorSE? 240: (iPhone6or6sor7? 270: 300);
- _IDCardScanningWindowLayer.bounds = (CGRect){CGPointZero, {width, width * 1.574}};
- _IDCardScanningWindowLayer.cornerRadius = 15;
- _IDCardScanningWindowLayer.borderColor = [UIColor whiteColor].CGColor;
- _IDCardScanningWindowLayer.borderWidth = 1.5;
- [self.layer addSublayer:_IDCardScanningWindowLayer];
-
-
- UIBezierPath *transparentRoundedRectPath = [UIBezierPath bezierPathWithRoundedRect:_IDCardScanningWindowLayer.frame cornerRadius:_IDCardScanningWindowLayer.cornerRadius];
-
-
- UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.frame];
- [path appendPath:transparentRoundedRectPath];
- [path setUsesEvenOddFillRule:YES];
-
- CAShapeLayer *fillLayer = [CAShapeLayer layer];
- fillLayer.path = path.CGPath;
- fillLayer.fillRule = kCAFillRuleEvenOdd;
- fillLayer.fillColor = [UIColor blackColor].CGColor;
- fillLayer.opacity = 0.6;
-
- [self.layer addSublayer:fillLayer];
-
-
- }
- -(void)loadImage
- {
-
- CGRect rect = _IDCardScanningWindowLayer.frame;
-
-
- CGPoint center = self.center;
- center.x = CGRectGetMaxX(_IDCardScanningWindowLayer.frame) + 20;
- if(_cardType == 0)
- {
- [self addTipLabelWithText:@"将身份证人像面置于此区域内,头像对准,扫描" center:center];
-
- CGFloat facePathWidth = iPhone5or5cor5sorSE? 125: (iPhone6or6sor7? 150: 180);
- CGFloat facePathHeight = facePathWidth * 0.89;
- self.facePathRect = (CGRect){CGRectGetMaxX(rect) - facePathWidth - 35,CGRectGetMaxY(rect) - facePathHeight - 25,facePathWidth,facePathHeight};
-
- UIImageView *headIV = [[UIImageView alloc] initWithFrame:_facePathRect];
- headIV.image = [UIImage imageNamed:@"idcard_first_head"];
- headIV.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
- headIV.contentMode = UIViewContentModeScaleAspectFill;
- [self addSubview:headIV];
- }
- else
- {
- [self addTipLabelWithText:@"将身份证背面置于此区域内,国徽对准,扫描" center:center];
-
- CGFloat facePathWidth = iPhone5or5cor5sorSE? 80: (iPhone6or6sor7? 110: 160);
- CGFloat facePathHeight = facePathWidth * 0.89;
- self.facePathRect = (CGRect){CGRectGetMaxX(rect) - 30 - facePathWidth, CGRectGetMinY(rect) + 30,facePathWidth,facePathHeight};
-
- UIImageView *headIV = [[UIImageView alloc] initWithFrame:_facePathRect];
- headIV.image = [UIImage imageNamed:@"idcard_first_head_gh"];
- headIV.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
- headIV.contentMode = UIViewContentModeScaleAspectFill;
- [self addSubview:headIV];
- }
- }
- #pragma mark - 添加提示标签
- -(void )addTipLabelWithText:(NSString *)text center:(CGPoint)center {
- UILabel *tipLabel = [[UILabel alloc] init];
-
- tipLabel.text = text;
- tipLabel.textColor = [UIColor whiteColor];
- tipLabel.textAlignment = NSTextAlignmentCenter;
-
- tipLabel.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
- [tipLabel sizeToFit];
-
- tipLabel.center = center;
-
- [self addSubview:tipLabel];
- }
- #pragma mark - 添加定时器
- -(void)addTimer {
- _timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];
- [_timer fire];
- }
- -(void)timerFire:(id)notice {
- [self setNeedsDisplay];
- }
- -(void)dealloc {
- [_timer invalidate];
- }
- - (void)drawRect:(CGRect)rect {
- rect = _IDCardScanningWindowLayer.frame;
-
- UIBezierPath *facePath = [UIBezierPath bezierPathWithRect:_facePathRect];
- facePath.lineWidth = 1.5;
- [[UIColor whiteColor] set];
- [facePath stroke];
-
-
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- static CGFloat moveX = 0;
- static CGFloat distanceX = 0;
-
- CGContextBeginPath(context);
- CGContextSetLineWidth(context, 2);
- CGContextSetRGBStrokeColor(context,0.3,0.8,0.3,0.8);
- CGPoint p1, p2;
-
- moveX += distanceX;
- if (moveX >= CGRectGetWidth(rect) - 2) {
- distanceX = -2;
- } else if (moveX <= 2){
- distanceX = 2;
- }
-
- p1 = CGPointMake(CGRectGetMaxX(rect) - moveX, rect.origin.y);
- p2 = CGPointMake(CGRectGetMaxX(rect) - moveX, rect.origin.y + rect.size.height);
-
- CGContextMoveToPoint(context,p1.x, p1.y);
- CGContextAddLineToPoint(context, p2.x, p2.y);
-
-
-
- CGContextStrokePath(context);
- }
- @end
|