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

时间:2009-10-11 08:52:51

标签: iphone core-animation

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

在SO上阅读类似的问题让我相信它可以通过动画 UIImageView的 图层 .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;

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

6 个答案:

答案 0 :(得分:78)

你快到了。

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;

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

答案 1 :(得分:62)

我在某个地方找到了它 - 效果很好。

夫特

UIView.transition(with: imageView, duration: 0.33, options: .transitionCrossDissolve, animations: {
      imageView.image = image
}, completion: nil)

Objc

UIImage * toImage = [UIImage imageNamed:@"image.png"];
[UIView transitionWithView:self.view
                  duration:0.33f
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    self.imageview.image = toImage;
                } completion:NULL];

答案 2 :(得分:20)

extension UIImageView
{
    func mySetImage(image:UIImage)
    {
        guard let currentImage = self.image where currentImage != image else { return }
        let animation = CATransition()
        animation.duration = 0.3
        animation.type = kCATransitionFade
        layer.add(animation, forKey: "ImageFade")
        self.image = image
    }
}

答案 3 :(得分:1)

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBarHidden=false;
UIImage *img=[UIImage imageNamed:@"images.jpeg"];
imgview=[[UIImageView alloc]init];
imgview.frame=CGRectMake(30,90, img.size.width, img.size.height);
[self.view addSubview:imgview];
[self animateImages];
}
- (void)animateImages
{
static int count = 0;
NSArray *animationImages = @[[UIImage imageNamed:@"images.jpeg"], [UIImage imageNamed:@"images (1).jpeg"]];
UIImage *image = [animationImages objectAtIndex:(count % [animationImages count])];

[UIView transitionWithView:imgview
                  duration:2.0f // animation duration
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    imgview.image = image;
                } completion:^(BOOL finished) {
                    [self animateImages];
                    count++;
                }];

}

答案 4 :(得分:1)

快速解决方案

let image1:UIImage = UIImage(named: "someImage1")!;
let image2:UIImage = UIImage(named: "someImage2")!;
let crossFade:CABasicAnimation = CABasicAnimation(keyPath: "contents");
crossFade.duration = 5.0;
crossFade.fromValue = image1.CGImage;
crossFade.toValue = image2.CGImage;
imageView.layer.addAnimation(crossFade, forKey:"animateContents");

答案 5 :(得分:0)

我的建议是在彼此之上使用两个UIImageView。顶部的那个有image1,底部是image2:

- (void) crossfade {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.5];
    imageView1.alpha = !imageView1.alpha;
    [UIView commitAnimations];
}

每当你调用它时,两个图像之间会消失。如果你想只做一次淡化并在完成后删除第一张图像:

- (void) crossfade {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationDelegate:imageView1];
    [UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
    imageView1.alpha = 0
    [UIView commitAnimations];
}

修改

如果您正在寻找代码不起作用的原因,那是因为您只是将属性设置为新图像,然后添加无用的动画。 CABasicAnimation需要设置fromValuetoValue,然后将该动画添加到图层,它应该执行动画。不要手动设置内容。