1

Currently I am having a issue switching views using 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:

    - (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. view2 is a UIViewController, thats why I am doing .view after it. The app is only going to be available on iOS 5 or up so thats not a problem. But what I am trying to achieve is switching views using Core Animation, and have each UIViewController manage their own views. I am just switching views using Core Animation instead of usual means.

3
  • Is view2 a member of self, in this case? Should it be a property, ala self.view2…? This is probably unrelated to your issue, but if you have a property for this member, you should use the accessors wherever possible. - (void) dealloc and any init methods being the exception. Commented Oct 16, 2011 at 0:29
  • In addition, I'm assuming that view2 is actually a UIViewController subclass? It looks like it, based on the syntax. If you're targeting iOS4 or earlier, it's not a good idea to have multiple view controllers sharing the same UIView. Under iOS5, you can have multiple view controllers sharing parts of the view hierarchy, but you should be using the view controller container methods on self to add them, assuming self is also a UIViewController. Commented Oct 16, 2011 at 0:38
  • Got it - thanks, that's a lot clearer. I've added an answer below which might help. Let me know if I need to clarify! Commented Oct 16, 2011 at 1:41

1 Answer 1

1

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. 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:

- (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.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't really want to switch views at the UIWindow level. I want to keep it at the normal level. Also I want to fade out view1, THEN change views to view to then fade that in from an alpha of 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.