简体   繁体   English

使用Core Animation切换视图不起作用?

[英]Switching views using Core Animation not working?

Currently I am having a issue switching views using Core Animation. 目前,我在使用Core Animation切换视图时遇到问题。 I want to fade through black switching to my next view. 我想通过黑色切换到下一个视图来淡出。 Right now it does not do anything besides lose touch events from my original view. 现在,除了从我的原始视角来看失去联系事件之外,它什么也没做。 What am I doing wrong in the code below? 我在下面的代码中做错了什么?

Edit1 Code: Edit1代码:

    - (void)changeView1ToView2 {
    CABasicAnimation *fadeout= [CABasicAnimation animationWithKeyPath:@"opacity"];
    [fadeout setDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
    [fadeout setToValue:[NSNumber numberWithFloat:0]];
    [fadeout setDuration:0.5];
    [[self.view layer] addAnimation:fadeout forKey:@"alpha"];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
    [self.view addSubview:self.view2.view];
    self.view2.view.frame = [UIScreen mainScreen].bounds;
    [self.view2.view setAlpha:0];
    CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [fadein setToValue:[NSNumber numberWithFloat:1.0]];
    [fadein setDuration:0.5];
    [[self.view2.view layer] addAnimation:fadein forKey:@"alpha"];
}

Ok I added self, look at my new code above. 好的,我添加了self,看看上面的新代码。 view2 is a UIViewController, thats why I am doing .view after it. view2是一个UIViewController,这就是为什么我在它之后执行.view。 The app is only going to be available on iOS 5 or up so thats not a problem. 该应用程序仅将在iOS 5或更高版本上可用,所以这不是问题。 But what I am trying to achieve is switching views using Core Animation, and have each UIViewController manage their own views. 但是我想要实现的是使用Core Animation切换视图,并让每个UIViewController管理自己的视图。 I am just switching views using Core Animation instead of usual means. 我只是使用Core Animation而不是通常的方式来切换视图。

If you're looking to have only one root view on screen at one time (and by the looks of that call to [UIScreen mainScreen].bounds , you are), then I'd suggest swapping the views at the UIWindow level. 如果您希望一次只在屏幕上显示一个根视图(并且通过[UIScreen mainScreen].bounds调用看起来很[UIScreen mainScreen].bounds ),那么建议您在UIWindow级别交换视图。 Without animations, this would look something like this: 如果没有动画,它将看起来像这样:

// Assuming UIViewControllers called view1 and view2 as members of some
// (non-UIViewController) controller class (self, in this case)
//and that view1.view is in the application's window's subviews collection
[self.view1.view removeFromSuperview];
[[UIApplication sharedApplication].delegate.window addSubview:self.view2.view];

In terms of not seeing the views actually swap, you need to ensure that your animation preserves the changes you make during the animation. 就看不到视图实际交换而言,您需要确保动画保留在动画过程中所做的更改。 Specifically, you need to set the following: 具体来说,您需要设置以下内容:

myAnimation.fillMode = kCAFillModeForwards;
myAnimation.removedOnCompletion = NO;

Your methods can then be adjusted to take all this into account. 然后可以调整您的方法以考虑所有这些因素。 For example, your animationDidStop:finished: method might look like this: 例如,您的animationDidStop:finished:方法可能如下所示:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    [self.view1.view removeFromSuperview];

    [self.view2.view setAlpha:0];
    [[UIApplication sharedApplication].delegate.window addSubview:self.view2.view];

    CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [fadein setToValue:[NSNumber numberWithFloat:1.0]];
    [fadein setDuration:0.5];
    fadein.fillMode = kCAFillModeForwards;
    fadein.removedOnCompletion = NO;
    [[self.view2.view layer] addAnimation:fadein forKey:@"alpha"];
}

You may need to muck around with it a bit to ensure that everything is firing correctly. 您可能需要对其进行处理,以确保一切正常进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM