我这样做是为了学习如何在 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/