UIView transitionFromView: toView: animation does not work.

This is my first stackoverflow post. I am a new iOS developer, and I am not a native speaker of English, so I will do my best to explain my problem.

Problem:

I added two views to the AppDelegate window, and I want to switch from one to the other using:

UIView transitionFromView:toView: 

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

I use a gesture recognizer to return to my MainScreenView. That's the problem. When I make a pinch gesture in scrollview, MainScreenView.view appears right before the animation, so the flip animation doesn't look right.

The code I use 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 use show / hide views instead of addSubview/removeFromSuperView because I tried to add and remove and got the application crash in a pinch gesture, just like with an unsuccessful animation. This is probably the same error, but I can not find the reason for this. Any help would be greatly appreciated.

Thanks.

Ok Using Adrian, here is 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]; } 
+6
source share
2 answers

Learn more about animations in iOS .

In your example, you forgot [UIView beginAnimations] .

0
source

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

Secondly, when using block method animation, make sure you use a container (possibly a UIView) that will be the parent of the two views that you want to switch. Without a container, you will animate the whole view. Make sure the container is the same size as the sub items 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];

0
source

Source: https://habr.com/ru/post/909406/


All Articles