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]; }