添加CALayers以查看视图加载

时间:2013-01-18 19:56:10

标签: ios objective-c core-animation

我有一个带有按钮的视图,当按下该按钮时,会触发一系列核心动画链。最初我这样做是为了让按钮既可以设置CALayers,又可以将它们添加到背景图层,并将动画添加到它们中,但是我加载的动画越多,它所用的动画就越长“加载“,可以这么说,虽然所有的图层仍然出现,动画运行得很好。

我想将图层添加到背景图层但保留动画,以便在按下按钮时触发它们。这些图层在视图的.h文件中创建,如下所示:

CALayer *layerOne;
CALayer *layerTwo;

等,然后添加与按下按钮的功能,如下所示:

layerOne = [CALayer layer];
[layerOne setBounds:CGRectMake(0, 0, 350, 419)];
[layerOne setPosition:CGPointMake(250, 630)];
[layerOne setContents:(id)[[UIImage imageNamed:@"imageOne.png"] CGImage]];
[previewLayer addSublayer:layerOne];

如果它与函数保持联系,这一切都可以正常工作。我已经尝试将上面的第二段代码移动到viewDidLoad函数中,在创建了previewLayer并将其添加到视图后,该层显示得很好,但动画不起作用。动画属于与按钮绑定的功能,但是如果在viewDidLoad下设置图层而不是在同一个函数中,我猜想没有正确添加到图层。有谁知道我可以使这项工作的方式?

编辑:这是我的viewDidLoad方法:

- (void)viewDidLoad
{
[super viewDidLoad];

background.layer.zPosition = .5;
buttonLayer.layer.zPosition = .7;
finishButton.hidden = TRUE;
session = [[AVCaptureSession alloc] init];
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

NSError *error;

AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self cameraWithPosition:AVCaptureDevicePositionFront] error:&error];


if (videoInput)
{
    [session addInput:videoInput];
}

AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *audioError = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&audioError];

if (audioInput)
{
    [session addInput:audioInput];
}


Float64 TotalSeconds = 131;         //Total seconds
int32_t preferredTimeScale = 30;    //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale);
movieFileOutput.maxRecordedDuration = maxDuration;

movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;

if ([session canAddOutput:movieFileOutput])
    [session addOutput:movieFileOutput];

[session setSessionPreset:AVCaptureSessionPreset640x480];

[self cameraSetOutputProperties];

previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.frame=background.bounds;
previewLayer.orientation = AVCaptureVideoOrientationLandscapeLeft;

[background.layer addSublayer:previewLayer];

[session startRunning];

[self setupAudioSession];

}

按下按钮:

-(IBAction)start:(id)sender
{
startButton.hidden = TRUE;

if (self.switchingTimer == nil) {
    self.switchingTimer = [NSTimer scheduledTimerWithTimeInterval:x target:self selector:@selector(switchView) userInfo:nil repeats:YES];
}

[self playAudio];

NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
self.outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
    NSError *error;
    if ([fileManager removeItemAtPath:outputPath error:&error] == NO)
    {
        //Error - handle if required
    }
}
//Start recording
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    [self padAnimations];
else
    [self phoneAnimations];
}

开始按钮既开始视频录制又触发padAnimations或phoneAnimations方法,具体取决于设备。定时器设置为在x秒后结束视频录制。这两种方法是设置CALayers和动画的地方,动画设置为在按下按钮和视频录制开始后的特定时间开始,因此必须将它们绑定到按钮按钮。触发所有这一切的开始按钮也会在按下按钮时被触发隐藏,并且只有在所有图层都加载到视图中后才会隐藏,这就是我知道加载时间增加的原因。

0 个答案:

没有答案