BBASMScanView.m
6.21 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// BBASMScanView.m
// SwanAppDemo
//
// Created by Ren,Tao on 2019/1/12.
// Copyright © 2019 baidu. All rights reserved.
//
#import "BBASMScanView.h"
#define LeftDistance 60
@interface BBASMScanView ()
@property (nonatomic, assign) NSInteger heightScale;
@property (nonatomic, assign) BOOL needStop;
@property (nonatomic, strong) UIImageView *lineImageView;
@end
@implementation BBASMScanView
-(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
self.heightScale = 1;
self.lineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scan_blue_line"]];
self.needStop = NO;
[self startAnimating];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat Left = LeftDistance / _heightScale;
CGSize sizeRetangle = CGSizeMake(self.frame.size.width - Left * 2, self.frame.size.width - Left * 2);
CGFloat YMinRetangle = self.frame.size.height / 2.0 - sizeRetangle.height/(2.0 * _heightScale);
_lineImageView.frame = CGRectMake(Left, YMinRetangle + 2, sizeRetangle.width - 4, 5);
[self addSubview:_lineImageView];
}
- (void)startAnimating {
if (_needStop) {
return;
}
CGFloat Left = LeftDistance / _heightScale;
CGSize sizeRetangle = CGSizeMake(self.frame.size.width - Left * 2, (self.frame.size.width - Left * 2) / _heightScale);
CGFloat YMinRetangle = self.frame.size.height / 2.0 - sizeRetangle.height/2.0;
CGFloat YMaxRetangle = YMinRetangle + sizeRetangle.height;
CGRect initFrame = CGRectMake(Left, YMinRetangle + 2, sizeRetangle.width - 4, 5);
_lineImageView.frame = initFrame;
_lineImageView.alpha = 1;
[UIView animateWithDuration:1.5 animations:^{
self.lineImageView.frame = CGRectMake(initFrame.origin.x, YMaxRetangle - 2, initFrame.size.width, initFrame.size.height);
} completion:^(BOOL finished) {
self.lineImageView.alpha = 0;
self.lineImageView.frame = initFrame;
[self performSelector:@selector(startAnimating) withObject:nil afterDelay:0.3];
}];
}
- (void)stopAnimating {
_needStop = YES;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[self drawScanRect];
}
- (void)drawScanRect {
//扫码区域Y轴最小坐标
CGFloat Left = LeftDistance / _heightScale;
CGSize sizeRetangle = CGSizeMake(self.frame.size.width - Left * 2, self.frame.size.width - Left * 2);
CGFloat YMinRetangle = self.frame.size.height / 2.0 - sizeRetangle.height/(2.0 * _heightScale);
CGFloat YMaxRetangle = YMinRetangle + sizeRetangle.height / _heightScale;
CGFloat XRetangleRight = self.frame.size.width - Left;
CGContextRef context = UIGraphicsGetCurrentContext();
//非扫码区域半透明
//设置非识别区域颜色
CGContextSetRGBFillColor(context, 0, 0, 0, 0.5);
//扫码区域上面填充
CGRect rect = CGRectMake(0, 0, self.frame.size.width, YMinRetangle);
CGContextFillRect(context, rect);
//扫码区域左边填充
rect = CGRectMake(0, YMinRetangle, Left, sizeRetangle.height/_heightScale);
CGContextFillRect(context, rect);
//扫码区域右边填充
rect = CGRectMake(XRetangleRight, YMinRetangle, Left,sizeRetangle.height/_heightScale);
CGContextFillRect(context, rect);
//扫码区域下面填充
rect = CGRectMake(0, YMaxRetangle, self.frame.size.width, self.frame.size.height - YMaxRetangle);
CGContextFillRect(context, rect);
//执行绘画
CGContextStrokePath(context);
//执行绘画
CGContextStrokePath(context);
//中间画矩形(正方形)
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:255 green:255 blue:255 alpha:1.00].CGColor);
CGContextSetLineWidth(context, 1);
CGContextAddRect(context, CGRectMake(Left, YMinRetangle, sizeRetangle.width, sizeRetangle.height/_heightScale));
CGContextStrokePath(context);
//画矩形框4格外围相框角
//相框角的宽度和高度
int wAngle = 15;
int hAngle = 15;
//4个角的 线的宽度
CGFloat linewidthAngle = 4;// 经验参数:6和4
//画扫码矩形以及周边半透明黑色坐标参数
CGFloat diffAngle = linewidthAngle/3;
//diffAngle = linewidthAngle / 2; //框外面4个角,与框有缝隙
//diffAngle = linewidthAngle/2; //框4个角 在线上加4个角效果
//diffAngle = 0;//与矩形框重合
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.110 green:0.659 blue:0.894 alpha:1.00].CGColor);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, linewidthAngle);
//
CGFloat leftX = Left - diffAngle;
CGFloat topY = YMinRetangle - diffAngle;
CGFloat rightX = XRetangleRight + diffAngle;
CGFloat bottomY = YMaxRetangle + diffAngle;
//左上角水平线
CGContextMoveToPoint(context, leftX-linewidthAngle/2, topY);
CGContextAddLineToPoint(context, leftX + wAngle, topY);
//左上角垂直线
CGContextMoveToPoint(context, leftX, topY-linewidthAngle/2);
CGContextAddLineToPoint(context, leftX, topY+hAngle);
//左下角水平线
CGContextMoveToPoint(context, leftX-linewidthAngle/2, bottomY);
CGContextAddLineToPoint(context, leftX + wAngle, bottomY);
//左下角垂直线
CGContextMoveToPoint(context, leftX, bottomY+linewidthAngle/2);
CGContextAddLineToPoint(context, leftX, bottomY - hAngle);
//右上角水平线
CGContextMoveToPoint(context, rightX+linewidthAngle/2, topY);
CGContextAddLineToPoint(context, rightX - wAngle, topY);
//右上角垂直线
CGContextMoveToPoint(context, rightX, topY-linewidthAngle/2);
CGContextAddLineToPoint(context, rightX, topY + hAngle);
//右下角水平线
CGContextMoveToPoint(context, rightX+linewidthAngle/2, bottomY);
CGContextAddLineToPoint(context, rightX - wAngle, bottomY);
//右下角垂直线
CGContextMoveToPoint(context, rightX, bottomY+linewidthAngle/2);
CGContextAddLineToPoint(context, rightX, bottomY - hAngle);
CGContextStrokePath(context);
}
@end