美文网首页
手电筒的开和关解决

手电筒的开和关解决

作者: Tomboy_Anan | 来源:发表于2017-02-15 20:47 被阅读85次

1、首先需要引入AVFoundation.framework框架
2、在 .h文件中添加属性

添加#import <AVFoundation/AVFoundation.h>
@interface LightViewController : UIViewController
{
BOOL isLightOn;
AVCaptureDevice *device;
}

@property BOOL isLightOn;
@end

  1. 在 .m 文件中实现
    .m文件

import "LightViewController.h"

@interface LightViewController ()

@end
@implementation LightViewController

@synthesize isLightOn;

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    self.navigationItem.title = @"手电筒";
    }
    return self;
    }

4>>

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(80, 150, 100, 80)];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    //AVCaptureDevice代表抽象的硬件设备
    // 找到一个合适的AVCaptureDevice
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (![device hasTorch]) {//判断是否有闪光灯
    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前设备没有闪光灯,不能提供手电筒功能" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
    [alter show];
    }

    isLightOn = NO;

}

-(void)btnClicked
{
isLightOn = 1-isLightOn;
if (isLightOn) {
[self turnOnLed:YES];
}else{
[self turnOffLed:YES];
}
}

//打开手电筒
-(void) turnOnLed:(bool)update
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}

//关闭手电筒
-(void) turnOffLed:(bool)update
{
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOff];
[device unlockForConfiguration];
}

相关文章

  • 手电筒的开和关解决

    1、首先需要引入AVFoundation.framework框架2、在 .h文件中添加属性 添加#import ...

  • 开关

    开,关,开,关 只有五百零一次的寿命 开,关,开,关 五百零二次的时候 五脏俱损 白色按钮,悬在墙面 他按了又按 ...

  • 一个没有标题只想感叹一下的屁话

    发现手机壳开手电筒的时候会发荧光耶 然并卵…

  • 开四关

    “四关穴”的应用非常广泛,高血压、失眠、精神类疾病.......各种虚实寒热之证,在诸多针方当中必不可少。 ...

  • 开与关

    曾经有人说过这样一句话:世界对你关上了一扇门,它一定会在某个地方为你打开一扇窗。我要在后面加上一句话:你必须要懂得...

  • 开与关

    清早,起床妆扮,开门了 深夜,卸妆沐浴,关门了 开门的时候,确认是真开 关门的时候,就是真的关 关闭这扇大门,它叫...

  • 晨起开四关

    ——每天晨起,别忘了开四关。 开四关,调气血 试试「开四关」, 这真的是疏通气血, 舒畅情绪的好方法。 「四关四穴...

  • 消失的卡门青(二)

    打火机开,关,开,关,开,关......关? 胡小宇这个蠢货,连打火机都弄个劣质的,林青一阵骂骂咧咧,揪住胡小宇的...

  • 只因 历历在目,每个刻度都隽永

    瑟尔先生太忙了。 他在大脑里安装了14个抽屉, 分别承载不同的事项, 每个抽屉都有节奏开和关, 有时此开彼关,有时...

  • 通电程序

    1 . 开总电门,测试警告灯,开 5 灯,开皮托管加温,放襟翼 2 . 收襟翼,关5灯,关皮托管加温,关总电门

网友评论

      本文标题:手电筒的开和关解决

      本文链接:https://www.haomeiwen.com/subject/irnqwttx.html