这是我在stackoverflow上的第一篇文章。我是iOS开发新手,不是母语为英语的人,所以我会尽力解释我的问题。
问题:
我已经将两个视图添加到我的AppDelegate窗口中,并且我想使用以下方法从一个视图翻转到另一个视图:
UIView transitionFromView:toView:
第一个视图(MainScreenView)有自己的
ViewController
。在MainScreenView.xib文件中,我有一个带有动作的按钮,调用在我的AppDelegate中实现的"goShow"方法。在那个方法中,我使用UIView transitionFromView:toView:
来过渡到第二个视图。到目前为止,一切正常。我的第二个视图(一个滚动视图)在我的AppDelegate中以编程方式声明,并且里面有许多图片(picturesViewController),并且在这些图片的顶部有一个
UIPinchGestureRecognizer
。我使用手势识别器来翻转回到我的MainScreenView。问题就在这里。当我在滚动视图上进行捏合手势时,
MainScreenView.view
立即出现,动画之前,因此翻转动画看起来不正确。我正在使用的代码是:
-(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];
}
我使用show/hide视图代替addSubview/removeFromSuperView,因为我尝试了添加和删除并在捏合手势中崩溃,恰好在动画失败的同一步骤中。可能是相同的错误,但我无法找到原因。任何帮助将不胜感激。
谢谢。
好的。在Adrian的帮助下,这是解决我的问题的UIPinchGesture代码:
[...]
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];
}