1

希望有人可以帮助解决这个问题。我正在尝试使用 UISlider 更改 CA 子层中的值。已经研究了几天,但似乎无法弄清楚。相机运行良好,滑块正在移动,但某处缺少连接..!

附言。对此相对较新,因此将不胜感激分步建议:-)

谢谢!

.h 文件:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <QuartzCore/QuartzCore.h>

@interface AVViewController : UIViewController{
IBOutlet UISlider *mySlider;
}

@property (nonatomic, retain) IBOutlet UISlider*mySlider;

- (IBAction)adjustValue:(id)sender;
@end

来自@implementation 行的.m 文件:

@implementation AVViewController

@synthesize mySlider;

- (IBAction)adjustValue:(UISlider *)sender {
float value = [(UISlider *)sender value];
if (value <0) value = 0;
if (value >0) value = 1;
mySlider.value = value;
}

- (void)viewDidLoad
{
[super viewDidLoad];

AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetHigh];

NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *frontCamera = nil;
    for (AVCaptureDevice *device in devices) {
        if (device.position == AVCaptureDevicePositionFront) {
            frontCamera = device;
            break;
        } 
}

NSError *error = nil;    
AVCaptureDevice *input = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&error];
    [session addInput:input];

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:CGRectMake(15, 15, 290 , 350)];
previewLayer.cornerRadius = 100.0;
[rootLayer insertSublayer:previewLayer atIndex:0];

CALayer *sublayer = [CALayer layer];
sublayer.frame = CGRectMake (15,15,previewLayer.bounds.size.width,previewLayer.bounds.size.height);
//VALUES TO BE ADJUSTED WITH UI SLIDER
sublayer.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:mySlider.value].CGColor;
[self.view.layer addSublayer:sublayer];

[session startRunning];

}
4

1 回答 1

0
  • 您需要将滑块的值更改事件连接到您的 IBAction。
  • 您还需要将实例保存到您的子层,以便您可以从viewDidLoad.

补充viewDidLoad

   // Call adjustValue: when the slider value changes
   [slider addTarget:self action:@selector(adjustValue:) forControlEvents:UIControlEventValueChanged];

   // create and configure sub layer
   ...
   self.mysublayer = sublayer;

然后在IBAction你可以修改你的层:

- (IBAction)adjustValue:(UISlider *)sender {
    float value = [sender value];
    // Modify layer here.
    self.mysublayer.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:value].CGColor;

}

希望这可以帮助!

于 2013-06-04T01:50:34.197 回答