如何2个图像之间在iPhone上使用的Core Animation交叉衰减(How to cross

2019-07-22 00:28发布

我这样做是为了学习如何与核心动画动画的性能在iPhone上运行(不是学习如何交叉衰减图像本身)。

阅读类似的问题对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;

......我现在可以看到图像,但它不褪色的,它只是瞬间出现。

Answer 1:

你是几乎没有。

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;

...设定为图像的最终结果。



Answer 2:

我发现这个地方 - 它的伟大工程。

迅速

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];


Answer 3:

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
    }
}


Answer 4:

- (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++;
                }];

}



Answer 5:

解决方案在迅速

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");


Answer 6:

我的建议是只使用两个UIImageView在彼此的顶部秒。 在上面的人有此搜索,并在底部是图像2:

- (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];
}

编辑:

如果你正在寻找一个理由,为什么你的代码不能正常工作,这是因为你的财产只设置新的图像,然后添加一个无用的动画。 CABasicAnimationfromValuetoValue需要进行设置,那么动画添加到层,它应该做的动画。 不要手动设置的内容。



文章来源: How to crossfade between 2 images on iPhone using Core Animation