我在xcode上有一个手电筒项目,但是我无法弄清楚如何为它添加亮度控制......我有一些按钮,这些按钮隐藏在手电筒的开关部分,我使用图像视图显示预呈现的图像。我在下面发布我的代码,所有的帮助将不胜感激。
Viewcontroller.h-
is basically the initializations of the buttons, IBActions, IBOutlets and Properties
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize offButton,onview,onButton,offview,slider;
-(IBAction)torchon:(id)sender
{
onButton.hidden=YES;
offButton.hidden=NO;
onview.hidden=NO;
offview.hidden=YES;
AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([device isTorchAvailable]&&[device isTorchModeSupported:AVCaptureTorchModeOn])
{
BOOL sucessful=[device lockForConfiguration:nil];
if(sucessful)
{
[device setTorchModeOnWithLevel:slider.value error:NULL];
[device unlockForConfiguration];
}
}
}
-(IBAction)torchoff:(id)sender
{
AVCaptureDevice *flashlight=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([flashlight isTorchAvailable]&&[flashlight isTorchModeSupported:AVCaptureTorchModeOn])
{
BOOL sucessful=[flashlight lockForConfiguration:nil];
if(sucessful)
{
[flashlight setTorchMode:AVCaptureTorchModeOff];
[flashlight unlockForConfiguration];
}
}
onButton.hidden=NO;
offButton.hidden=YES;
onview.hidden=YES;
offview.hidden=NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
onButton.hidden=YES;
offButton.hidden=NO;
onview.hidden=NO;
offview.hidden=YES;
AVCaptureDevice *device =[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([device isTorchAvailable]&&[device isTorchModeSupported:AVCaptureTorchModeOn])
{
BOOL sucessful=[device lockForConfiguration:nil];
if(sucessful)
{
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
}
}
-(IBAction)slideing:(id)sender
{
slider.maximumValue = 1.0f;
slider.minimumValue = 0.0f;
[slider setContinuous:YES];
[slider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchModeOnWithLevel:slider.value error:NULL];
[device unlockForConfiguration];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated
}
@end
and nothing in appdelegate.m and appdelegate.h
i have created my objects like a slider, and button as well as the image views in a storyboard and have linked them to my code. I've used IBAction functions and have used interface builder a bit as well. As i said before, i cant figure out how to add a shake to switch on and off feature. Any help is appreciated!