所以我制作了一个自定义的UIButton并将其添加到代码中并在interfacebuiler中建立了连接。我想按钮作为开关工作,我该如何正确地做到这一点?我是iphone开发的初学者,这是我在暑假期间为本课程开设的一个学校项目,为下学期提供一个良好的开端。
所以,如果有人能帮助我理解如何以正确的方式做到这一点,并可能在代码中写评论。谢谢你的帮助。 大卫H.
这是我的代码:
//
// FlashlightViewController.h
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface FlashlightViewController : UIViewController {
AVCaptureSession *torchSession;
IBOutlet UIButton *button;
}
-(IBAction)pressButton:(id) sender;
@property (nonatomic, retain) AVCaptureSession *torchSession;
@property (nonatomic, retain) IBOutlet UIButton *button;
@end
这是.m文件
//
// FlashlightViewController.m
//
#import "FlashlightViewController.h"
@implementation FlashlightViewController
@synthesize torchSession;
@synthesize button;
- (void)viewDidLoad {
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
if (flashInput){
[session addInput:flashInput];
}
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
[output release];
[session commitConfiguration];
[session startRunning];
}
[self setTorchSession:session];
[session release];
[super viewDidLoad];
}
- (void)viewDidUnload {
self.button = nil;
}
- (void)dealloc {
[TorchSession release];
[button release];
[super dealloc];
}
-(IBAction)pressButton : (id) sender{
}
@end
答案 0 :(得分:1)
实际上你的问题是什么?有一个内置的UISwitch对象。如果你想创建一个自定义的,那么你可以跟踪一个bool标志并在按钮处理程序中切换它。
答案 1 :(得分:1)
在@interface
部分(.h)中,您还需要IBOutlet IBAction pressButton;
。然后,在Interface Builder的检查器(出口部分)中,选择文件的所有者,并将pressButton:
连接到UIButton的Touch Up Inside
操作。
要切换手电筒状态,请将BOOL torchAlreadyOn;
添加到@interface
部分(.h)。然后,将您的viewDidLoad
自定义代码移至pressButton
方法。然后,在pressButton
方法的末尾添加:
if (torchAlreadyOn) {
torchAlreadyOn = NO;
}
else {
torchAlreadyOn = YES;
}
然后,在将火炬状态设置为on的任何地方,将其括在if ... else语句中,该语句检查BOOL
:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (torchAlreadyOn) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
}
else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
}
[device unlockForConfiguration];
AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
if (flashInput){
if (!torchAlreadyOn) {
[session addInput:flashInput];
}
else {
[session removeInput:flashInput];
}
}
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
if (!torchAlreadyOn) {
[session addOutput:output];
}
else {
[session removeOutput:output];
}
[output release];
[session commitConfiguration];
[session startRunning];
}
[self setTorchSession:session];
[session release];