iPhone页面翻页过渡动画

5

我正在尝试在窗口中使用UIImageView实现页面翻页过渡效果。以下代码位于我的主初始化方法中:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationDelay:delay];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:@selector(animCompleteHandler:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:splashImage cache:YES];

splashImage.frame = CGRectMake(-320, 0, 10, 10);
//[splashImage removeFromSuperview];

[UIView commitAnimations];

这张图片可以动态改变位置和大小,但不能卷曲。如果取消注释removeFromSuperView,则会立即消失。有任何想法吗?

更新:

已更改代码,使用Lars的非常简洁的方式触发动画并包括动画和回调...

[UIView animateWithDuration:1.5
                      delay:delay
                    options: UIViewAnimationTransitionCurlUp 
                 animations:^{splashImage.alpha = 0;}
                 completion:^(BOOL finished){[splashImage removeFromSuperview];}
 ];

很遗憾,页面翻页效果不会发生。但是它会渐变。

我不确定这是否与语法或者SplashImageUIWindow对象中的UIImageView类有关。也许它需要在UIView中才能创建转换效果。

2个回答

8
尝试类似以下内容的方式:
[UIView transitionWithView:splashImage 
        duration:1.5 
        options: UIViewAnimationOptionTransitionCurlUp 
        animations^{
            splashImage.frame = CGRectMake(-320, 0, 10, 10);
        } 
        completion:^(BOOL finished){
            [splashImage removeFromSuperview];
            //animCompleteHandlerCode..
        }
];

尚未测试,可能存在语法错误,但可以尝试!

或者这个更好:

[UIView animateWithDuration:1.5
        delay:delay
        options: UIViewAnimationOptionTransitionCurlUp 
        animations^{
            splashImage.frame = CGRectMake(-320, 0, 10, 10);
        } 
        completion:^(BOOL finished){
            [splashImage removeFromSuperview];
             //animCompleteHandlerCode..
        }
];

我还没有尝试过,但是你的代码让我很感兴趣!^ 是什么意思?这是一种嵌入函数的方式吗? - Lee Probert
1
这些是 iOS 4.0 新增的代码块。我鼓励你去了解一下代码块,这是 iOS 的重要补充。可以访问这个链接:http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html如果我理解你的代码正确的话,你想要做的是在获取页面内容的同时移动和缩放它? - LarsJK
好的,移动和缩放并不是很重要...我只是想触发过渡。使用那个转换只是为了测试它是否正常工作。我只需要让启动画面翻页消失。为了将其作为过渡触发,我需要对视图进行一些操作。使用“隐藏”会起作用吗?无论如何,我现在正在运行一些测试。感谢您的反馈。 - Lee Probert
是的,可能是这样。只需传入nil而不是动画块。然后将splashImage设置为hidden或alpha为0.0。在过渡方法之前或之后。 - LarsJK
如果不知道的话,单词“animations”后面缺少一个冒号“:”。应该是:animations:^{ - ICL1901

1

@Larsaronen:感谢您提供的示例,正是我所需要的!我只想要一个简单的页面翻页效果,当图像首次显示时,我使用了 alpha 值为 1.0,将完成回调设置为 nil:

// set a page curl animation for the specified UIImageView control
- (void) setAnimationPageCurl:(UIImageView *)imageView {

    [UIView transitionWithView:imageView 
                      duration:1.5 
                       options:UIViewAnimationOptionTransitionCurlDown 
                    animations:^ { imageView.alpha = 1.0; } 
                    completion:nil];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接