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
//
// QRView.m
// QRWeiXinDemo
//
// Created by lovelydd on 15/4/25.
// Copyright (c) 2015年 lovelydd. All rights reserved.
//
#import "QRView.h"
#import "QRUtil.h"
static NSTimeInterval kQrLineanimateDuration = 0.02;
@implementation QRView {
UIImageView *qrLine;
CGFloat qrLineY;
// QRMenu *qrMenu;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (!qrLine) {
[self initQRLine];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kQrLineanimateDuration target:self selector:@selector(show) userInfo:nil repeats:YES];
[timer fire];
}
// if (!qrMenu) {
// [self initQrMenu];
// }
}
- (void)initQRLine {
CGRect screenBounds = [QRUtil screenBounds];
qrLine = [[UIImageView alloc] initWithFrame:CGRectMake(screenBounds.size.width / 2 - self.transparentArea.width / 2, screenBounds.size.height / 2 - self.transparentArea.height / 2, self.transparentArea.width, 2)];
qrLine.image = [UIImage imageNamed:@"qr_scan_line"];
qrLine.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:qrLine];
qrLineY = qrLine.frame.origin.y;
}
//
//- (void)initQrMenu {
//
// CGFloat height = 100;
// CGFloat width = [QRUtil screenBounds].size.width;
// qrMenu = [[QRMenu alloc] initWithFrame:CGRectMake(0, [QRUtil screenBounds].size.height - height, width, height)];
// qrMenu.backgroundColor = [UIColor grayColor];
// [self addSubview:qrMenu];
//
// __weak typeof(self)weakSelf = self;
//
// qrMenu.didSelectedBlock = ^(QRItem *item){
//
// NSLog(@"点击的是%u",item.type);
//
// if ([weakSelf.delegate respondsToSelector:@selector(scanTypeConfig:)] ) {
//
// [weakSelf.delegate scanTypeConfig:item];
// }
// };
//}
- (void)show {
[UIView animateWithDuration:kQrLineanimateDuration animations:^{
CGRect rect = qrLine.frame;
rect.origin.y = qrLineY;
qrLine.frame = rect;
} completion:^(BOOL finished) {
CGFloat maxBorder = self.frame.size.height / 2 + self.transparentArea.height / 2 - 4;
if (qrLineY > maxBorder) {
qrLineY = self.frame.size.height / 2 - self.transparentArea.height /2;
}
qrLineY++;
}];
}
- (void)drawRect:(CGRect)rect {
//整个二维码扫描界面的颜色
CGSize screenSize =[QRUtil screenBounds].size;
CGRect screenDrawRect =CGRectMake(0, 0, screenSize.width,screenSize.height);
//中间清空的矩形框
CGRect clearDrawRect = CGRectMake(screenDrawRect.size.width / 2 - self.transparentArea.width / 2,
screenDrawRect.size.height / 2 - self.transparentArea.height / 2,
self.transparentArea.width,self.transparentArea.height);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self addScreenFillRect:ctx rect:screenDrawRect];
[self addCenterClearRect:ctx rect:clearDrawRect];
[self addWhiteRect:ctx rect:clearDrawRect];
[self addCornerLineWithContext:ctx rect:clearDrawRect];
}
- (void)addScreenFillRect:(CGContextRef)ctx rect:(CGRect)rect {
CGContextSetRGBFillColor(ctx, 40 / 255.0,40 / 255.0,40 / 255.0,0.5);
CGContextFillRect(ctx, rect); //draw the transparent layer
}
- (void)addCenterClearRect :(CGContextRef)ctx rect:(CGRect)rect {
CGContextClearRect(ctx, rect); //clear the center rect of the layer
}
- (void)addWhiteRect:(CGContextRef)ctx rect:(CGRect)rect {
CGContextStrokeRect(ctx, rect);
CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1);
CGContextSetLineWidth(ctx, 0.8);
CGContextAddRect(ctx, rect);
CGContextStrokePath(ctx);
}
- (void)addCornerLineWithContext:(CGContextRef)ctx rect:(CGRect)rect{
//画四个边角
CGContextSetLineWidth(ctx, 2);
CGContextSetRGBStrokeColor(ctx, 89 /255.0, 172/255.0, 220/255.0, 1);
//左上角
CGPoint poinsTopLeftA[] = {
CGPointMake(rect.origin.x+0.7, rect.origin.y),
CGPointMake(rect.origin.x+0.7 , rect.origin.y + 15)
};
CGPoint poinsTopLeftB[] = {CGPointMake(rect.origin.x, rect.origin.y +0.7),CGPointMake(rect.origin.x + 15, rect.origin.y+0.7)};
[self addLine:poinsTopLeftA pointB:poinsTopLeftB ctx:ctx];
//左下角
CGPoint poinsBottomLeftA[] = {CGPointMake(rect.origin.x+ 0.7, rect.origin.y + rect.size.height - 15),CGPointMake(rect.origin.x +0.7,rect.origin.y + rect.size.height)};
CGPoint poinsBottomLeftB[] = {CGPointMake(rect.origin.x , rect.origin.y + rect.size.height - 0.7) ,CGPointMake(rect.origin.x+0.7 +15, rect.origin.y + rect.size.height - 0.7)};
[self addLine:poinsBottomLeftA pointB:poinsBottomLeftB ctx:ctx];
//右上角
CGPoint poinsTopRightA[] = {CGPointMake(rect.origin.x+ rect.size.width - 15, rect.origin.y+0.7),CGPointMake(rect.origin.x + rect.size.width,rect.origin.y +0.7 )};
CGPoint poinsTopRightB[] = {CGPointMake(rect.origin.x+ rect.size.width-0.7, rect.origin.y),CGPointMake(rect.origin.x + rect.size.width-0.7,rect.origin.y + 15 +0.7 )};
[self addLine:poinsTopRightA pointB:poinsTopRightB ctx:ctx];
CGPoint poinsBottomRightA[] = {CGPointMake(rect.origin.x+ rect.size.width -0.7 , rect.origin.y+rect.size.height+ -15),CGPointMake(rect.origin.x-0.7 + rect.size.width,rect.origin.y +rect.size.height )};
CGPoint poinsBottomRightB[] = {CGPointMake(rect.origin.x+ rect.size.width - 15 , rect.origin.y + rect.size.height-0.7),CGPointMake(rect.origin.x + rect.size.width,rect.origin.y + rect.size.height - 0.7 )};
[self addLine:poinsBottomRightA pointB:poinsBottomRightB ctx:ctx];
CGContextStrokePath(ctx);
}
- (void)addLine:(CGPoint[])pointA pointB:(CGPoint[])pointB ctx:(CGContextRef)ctx {
CGContextAddLines(ctx, pointA, 2);
CGContextAddLines(ctx, pointB, 2);
}
@end