1

所以我制作了一个自定义 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
4

2 回答 2

1

你的问题究竟是什么?有一个内置的UISwitch对象。如果你想创建一个自定义的,那么你可以跟踪一个 bool 标志并在按钮处理程序中切换它。

于 2010-07-17T12:30:15.683 回答
1

在您的@interface部分 (.h) 中,您还需要IBOutlet IBAction pressButton;. 然后,在 Interface Builder 的检查器(出口部分)中,选择 File's Owner,并连接pressButton:到 UIButton 的Touch Up Inside操作。

要切换手电筒状态,请添加BOOL torchAlreadyOn;@interface(.h) 部分。然后,将您的viewDidLoad自定义代码移至该pressButton方法。然后,在pressButton方法的最后,添加:

if (torchAlreadyOn) {
    torchAlreadyOn = NO;
}
else {
    torchAlreadyOn = YES;
}

然后,在您将手电筒状态设置为打开的任何地方,将其包含在 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];
于 2010-07-18T07:28:46.747 回答