// 提示Label
UILabel *promptLabel = [[UILabel alloc] init];
promptLabel.backgroundColor = [UIColor clearColor];
CGFloat promptLabelX = 0;
CGFloat promptLabelY = CGRectGetMaxY(scanContent_layer.frame) + 30;
CGFloat promptLabelW = self.frame.size.width;
CGFloat promptLabelH = 25;
promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH);
promptLabel.textAlignment = NSTextAlignmentCenter;
promptLabel.font = [UIFont boldSystemFontOfSize:13.0];
promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描";
[self addSubview:promptLabel];
UIView *bottomView = [[UIView alloc] init];
bottomView.frame = CGRectMake(0, self.frame.size.height - 100, self.frame.size.width, 100);
bottomView.layer.backgroundColor = [RGB(58, 58, 58) colorWithAlphaComponent:scanBorderOutsideViewAlpha].CGColor;
[self addSubview:bottomView];
_torchImageView = [[UIImageView alloc] init];
_torchImageView.image = [UIImage imageNamed:@"电筒"];
_torchImageView.frame = CGRectMake((self.frame.size.width-40)/2, 6, 40, 44);
[bottomView addSubview:_torchImageView];
UILabel *torchLabel = [[UILabel alloc] init];
torchLabel.backgroundColor = [UIColor clearColor];
torchLabel.frame = CGRectMake(0, 50, self.frame.size.width, 40);
torchLabel.textAlignment = NSTextAlignmentCenter;
torchLabel.font = [UIFont boldSystemFontOfSize:14.0];
torchLabel.textColor = HEX(@"#999999");
torchLabel.text = @"手电筒";
[bottomView addSubview:torchLabel];
// 添加闪光灯按钮
UIButton *light_button = [[UIButton alloc] init];
light_button.frame = bottomView.bounds;
// CGFloat light_buttonX = 0;
// CGFloat light_buttonY = CGRectGetMaxY(promptLabel.frame) + scanContent_X * 0.5;
// CGFloat light_buttonW = self.frame.size.width;
// CGFloat light_buttonH = 25;
// light_button.frame = CGRectMake(light_buttonX, light_buttonY, light_buttonW, light_buttonH);
// [light_button setTitle:@"打开照明灯" forState:UIControlStateNormal];
// [light_button setTitle:@"关闭照明灯" forState:UIControlStateSelected];
// [light_button setTitleColor:promptLabel.textColor forState:(UIControlStateNormal)];
// light_button.titleLabel.font = [UIFont systemFontOfSize:17];
[light_button addTarget:self action:@selector(light_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:light_button];
#pragma mark - - - 照明灯的点击事件
- (void)light_buttonAction:(UIButton *)button {
if (button.selected == NO) { // 点击打开照明灯
[self turnOnLight:YES];
button.selected = YES;
_torchImageView.image = [UIImage imageNamed:@"点亮"];
} else { // 点击关闭照明灯
[self turnOnLight:NO];
button.selected = NO;
_torchImageView.image = [UIImage imageNamed:@"电筒"];
}
}
- (void)turnOnLight:(BOOL)on {
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([_device hasTorch]) {
[_device lockForConfiguration:nil];
if (on) {
[_device setTorchMode:AVCaptureTorchModeOn];
} else {
[_device setTorchMode: AVCaptureTorchModeOff];
}
[_device unlockForConfiguration];
}
}