1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// HMScannerMaskView.m
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/3.
// Copyright © 2016年 itheima. All rights reserved.
//
#import "HMScannerMaskView.h"
@implementation HMScannerMaskView
+ (instancetype)maskViewWithFrame:(CGRect)frame cropRect:(CGRect)cropRect {
HMScannerMaskView *maskView = [[self alloc] initWithFrame:frame];
maskView.backgroundColor = [UIColor clearColor];
maskView.cropRect = cropRect;
return maskView;
}
- (void)setCropRect:(CGRect)cropRect {
_cropRect = cropRect;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor colorWithWhite:0.0 alpha:0.4] setFill];
CGContextFillRect(ctx, rect);
CGContextClearRect(ctx, self.cropRect);
[[UIColor colorWithWhite:0.95 alpha:1.0] setStroke];
CGContextStrokeRectWithWidth(ctx, CGRectInset(_cropRect, 1, 1), 1);
}
@end