iphone - 如何使用 Core Animation 在 iPhone 上的 2 个图像之间交叉淡入淡出

标签 iphone core-animation

我这样做是为了学习如何在 iPhone 上使用 Core Animation 可动画属性(而不是学习如何交叉淡入淡出图像本身)。

阅读有关 SO 的类似问题让我相信可以通过对 UIImageView's layer.contents 属性进行动画处理来完成,如下所示:

UIImage *image1 = [UIImage imageNamed:@"someImage1.png"];
UIImage *image2 = [UIImage imageNamed:@"someImage2.png"];

self.imageView.image = image1;
[self.view addSubview:self.imageView];

CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 5.0;
self.imageView.layer.contents = image2;
[self.imageView.layer addAnimation:crossFade forKey:@"animateContents"];

我的细节是否有误,或者这是不可能的。

更新:上面的代码生成一个空白的 UIImageView。当我更改此行时:

self.imageView.layer.contents = image2.CGImage;

...我现在可以看到图像,但它不会淡入,只是立即出现。

最佳答案

你就快到了。

CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 5.0;
crossFade.fromValue = image1.CGImage;
crossFade.toValue = image2.CGImage;
[self.imageView.layer addAnimation:crossFade forKey:@"animateContents"];

请注意,动画独立于 UIImageView 的实际值/内容。因此您需要

self.imageView.image = image2;

...设置图像的最终结果。

关于iphone - 如何使用 Core Animation 在 iPhone 上的 2 个图像之间交叉淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1550206/

相关文章:

ios - 有关于MonoTouch的新的基于 block 的动画的好的教程吗?

iphone - 如果我将一个 UISwitch 控件添加到我的每个 TableView 单元格,我如何知道它属于哪个单元格?

iphone - 如何在 iPhone SDK 中播放 360º 视频

iOS:为什么覆盖 drawRect 求助于软件渲染?

iphone - 您能否仅使用 Core Animation 重新创建 iBooks 应用程序中看到的 3D 翻页效果,还是需要使用 OpenGL ES?

ios - 在iOS中,UIView动画和Core动画有什么区别?

ios - 如果字符串的长度大于标签中显示的行数,如何使添加在 UILabel 中看到更多功能?

objective-c - iphone出现闪屏时如何隐藏状态栏?

ios - 使用 JsonModel 解析 json

ios - 为什么更新 CALayer 会导致 CPU 使用率(其他进程)增加 60-70%?