UIView transitionFromView: toView: animation not working.
Asked Answered
S

2

6

It's my first post on stackoverflow. I'm a iOS developer newbie and I'm not a native English speaker, so I will do my best to explain my problem.

Problem:

I have added two views to my AppDelegate window and I want to flip from one to the other using:

 UIView transitionFromView:toView:

The first view (MainScreenView) has its own ViewController. On the MainScreenView .xib file I have a button with an action that calls the method "goShow" implemented in my AppDelegate. In that method I use UIView transitionFromView:toView: to transition to the second view. So far everything is working fine. My second view (a scrollview) is declared programmatically in my AppDelegate and has a bunch of pictures inside it (picturesViewController) and on top of those, has a UIPinchGestureRecognizer.

I'm using a gesture recognizer to flip back to my MainScreenView. That is where the problem is. When I do a pinch gesture on the scrollview the MainScreenView.view appears immediately, before the animation, so the flip animation looks wrong.

The code I'm using is:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

mainScreen = [[MainScreenViewController alloc] initWithNibName:@"MainScreenViewController" bundle: [NSBundle mainBundle]];

CGRect frame = self.window.bounds;
int pageCount = 10;
scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.contentSize = CGSizeMake(320*pageCount, 480);
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = FALSE;
scrollView.showsVerticalScrollIndicator = FALSE;
scrollView.delegate = self;

[...] 'While' adding pictures to de scrollView

UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(goBackToMain)] autorelease];
[scrollView addGestureRecognizer:twoFingerPinch];

[self.window addSubview: scrollView];
[scrollView setHidden:TRUE];
[self.window addSubview: mainScreen.view];

[self.window makeKeyAndVisible];
return YES;
}

-(void) goShow{

[UIView transitionFromView:mainScreen.view
                    toView:scrollView 
                  duration:0.5
                   options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews                    
                completion:NULL];

[UIView commitAnimations];    
}

-(void) goBackToMain {

[UIView transitionFromView:scrollView
                    toView:mainScreen.view 
                  duration:0.5
                   options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews                     
                completion:NULL]; 

[UIView commitAnimations];
}

I'm using show/hide views instead of addSubview/removeFromSuperView because I tried the add and remove and got an app crash in the pinch gesture, exactly in same step that is failing the animation. Probably it is the same error, but I'm unable to find the reason for this. Any help would be appreciated.

Thanks.

Ok. With Adrian's help, here's the UIPinchGesture code that solved my problem:

[...]

UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(goBackToMain:)] autorelease];
[scrollView addGestureRecognizer:twoFingerPinch];


-(void)goBackToMain:(UIPinchGestureRecognizer *)recognizer {

if (recognizer.state == UIGestureRecognizerStateEnded)
{
    [UIView transitionFromView:scrollView
                        toView:mainScreen.view 
                      duration:0.4
                       options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews                     
                    completion:nil]; 

    [UIView commitAnimations];
}
Selfsustaining answered 26/2, 2012 at 19:25 Comment(1)
That was a lot better explained than most native English speakers manage. +1.Shiloh
N
0

Read more about animations in iOS.

In your example you forgot [UIView beginAnimations].

Nesbitt answered 26/2, 2012 at 19:52 Comment(3)
I have read Apple's documentation on this, but it doesn't seem to help solve my problem. I'm targeting this application for iOS4(.3), and [UIView beginAnimations] has been deprecated. Apple now recommends the use of the blocks API, and in fact my 'goShow' method works perfectly. The apparently similar 'goBackToMain' doesn't. The difference between both is that the 'goBackToMain' is being called by a UIPinchGestureRecognizer. I'm not sure that the problem is in the gesture recognizer code and what exactly is the problem, but I can't think of any other reason for this bug.Selfsustaining
if you're using a pinch recognizer, try to put the gesture as a parameter, so you can get the state of the gesture and make the code happen on a specific event (UIGestureRecognizerStateBegan, ... etc). Anyway if you don't use blocks with the animations you should still use [UIView begnAnimations]. Here is an example with the gesture thing: UIPinchGestureRecognizer twoFingerPinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(goBackToMain:)] autorelease]; and the method should be "-(void)goBackToMain:(UIGestureRecognizer) gesture"Nesbitt
Thanks Adrian. That was what I was missing, using the state of the gesture. I will post my final code, so that others with the same problem can try this solution.Selfsustaining
L
1

First, you cannot mix the old method beginAnimation commitAnimation combination with the new block method transitionFromView.

Second, when using block method animation, make sure you use a container (probably a UIView) that will be the parent of the two views you want to switch. Without the container you will be animating the whole view instead. Make sure the container have the same size as the subviews that will switch.

Example:


    [container addSubView:frontView];
    [container addSubView:backView];

    [self.view addSubView:container];

    [UIView transitionFromView:backView toView:frontView duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];

Laurustinus answered 11/10, 2013 at 11:7 Comment(0)
N
0

Read more about animations in iOS.

In your example you forgot [UIView beginAnimations].

Nesbitt answered 26/2, 2012 at 19:52 Comment(3)
I have read Apple's documentation on this, but it doesn't seem to help solve my problem. I'm targeting this application for iOS4(.3), and [UIView beginAnimations] has been deprecated. Apple now recommends the use of the blocks API, and in fact my 'goShow' method works perfectly. The apparently similar 'goBackToMain' doesn't. The difference between both is that the 'goBackToMain' is being called by a UIPinchGestureRecognizer. I'm not sure that the problem is in the gesture recognizer code and what exactly is the problem, but I can't think of any other reason for this bug.Selfsustaining
if you're using a pinch recognizer, try to put the gesture as a parameter, so you can get the state of the gesture and make the code happen on a specific event (UIGestureRecognizerStateBegan, ... etc). Anyway if you don't use blocks with the animations you should still use [UIView begnAnimations]. Here is an example with the gesture thing: UIPinchGestureRecognizer twoFingerPinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(goBackToMain:)] autorelease]; and the method should be "-(void)goBackToMain:(UIGestureRecognizer) gesture"Nesbitt
Thanks Adrian. That was what I was missing, using the state of the gesture. I will post my final code, so that others with the same problem can try this solution.Selfsustaining

© 2022 - 2024 — McMap. All rights reserved.