手电筒点亮或者关闭事件


    // 提示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];

    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值