Skyler
Skyler

Reputation: 2864

XCode/Objective-C: Simultaneous animations for instances of UIView?

I have two IBOutlet variables connected to two instances of UIView in Interface Builder, called blankView and fadedBGView in my ViewController.

How can I set up this code so that the blankView instance will fade into fadedBGView, using the UIViewAnimationOptionTransitionCrossDissolve transition, and the two UIViews can simultaneously transform (move) from their current position (they have equal positions) to 0,0?

What's happening is the fade occurs, and then the fadedBGView view abruptly moves to 0,0.

In other words, I'd like to have the first UIView fade into the second, while simultaneously moving up the screen.

Hopefully this is clear enough to answer. Best...SL

[UIView transitionFromView:blankView
                    toView:fadedBGView
                    duration:1.0
                    options:UIViewAnimationOptionTransitionCrossDissolve
                    completion:^(BOOL finished) {
                    [blankView removeFromSuperview];
                    }];

[UIView commitAnimations];

CGRect frame = fadedBGView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
[UIView animateWithDuration:1.0 animations:^{
    NSLog(@"UIView Transition/Move Called");
    fadedBGView.frame = frame;
}];

Upvotes: 1

Views: 1396

Answers (2)

bneely
bneely

Reputation: 9093

iOS 4 and newer provides block-based animations, which your code is already using:

+[UIView animateWithDuration:animations:];
+[UIView animateWithDuration:animations:completion:];

Within the animation block, you can set multiple destination values. See Apple's UIView documentation for a reference to the animatable properties. So within one block, you can modify frames, and alpha (transparency) values of views. I'm not sure how the cross dissolve animation works, but if it's simply one view going from 0 to 1, and another view going from 1 to 0, that's easy to implement.

Upvotes: 1

hoha
hoha

Reputation: 4428

Instead of

UIViewAnimationOptionTransitionCrossDissolve

use

UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent

UIViewAnimationOptionAllowAnimatedContent option allows additional animations during transition. Make sure your fadedBGView has correct starting frame.

Upvotes: 1

Related Questions