在UIView和UIScrollView之间进行翻转动画的问题

时间:2009-10-07 08:08:33

标签: iphone animation uiscrollview flip

我是iPhone开发的新手,我正在尝试在UIView和另一个包含常规UIView和UIScrollView的UIView之间进行翻转动画,滚动视图又有几个UIViews作为子视图。

在动画开始之前,滚动视图需要偏移到特定点以显示特定的子视图(用户在滚动视图中跳转到特定的“章节”)。

它的动画效果很好,但问题是它“有时”(可能每隔三次)用滚动视图的一个子视图启动动画,而不是用原始的UIView('overlayView'开始动画下面的代码)。我怀疑这与我在设置动画之前设置滚动视图的偏移量这一事实有关。

这就是我目前的做法:

   // get the MPMoviePlayer window that has the views to animate between as subviews
   UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];

    // tell the controller of the scroll view to set the scroll view offset
    [instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]];

    // Animate transition to instruction view
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO];

    // the views to animate between
    [moviePlayerWindow sendSubviewToBack: overlayView];
    [moviePlayerWindow bringSubviewToFront: instructionControlsController.view];

    [UIView commitAnimations];

控制器中的setInstructionImageWithNumber方法如下所示:

- (void) setInstructionImageWithNumber:(int)number
{
    if (number < kNumberOfPages)
        [scrollView setContentOffset: CGPointMake((number * kImageWidth), 0) animated:NO];
}

我可能做错了什么的想法以及为什么我会在动画看起来很好的时候得到这种行为,有时甚至不行?

1 个答案:

答案 0 :(得分:2)

如果您在startAnimations之前为运行循环提供更新视图的机会,会发生什么?您可能需要执行此操作以使视图有机会“赶上”并在动画开始之前进行准确更新。

UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
[instructionControlsController setInstructionImageWithNumber:[self chapterAtTime:currentTime]];

//Before continuing the animation, let the views update
[self performSelector:@selector(continueTheAnimation:) withObject:nil afterDelay:0.0];

。 。

- (void)continueTheAnimation:(void*)context {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: moviePlayerWindow cache:NO];
    [moviePlayerWindow sendSubviewToBack: overlayView];
    [moviePlayerWindow bringSubviewToFront: instructionControlsController.view];
    [UIView commitAnimations];
}