如何使用核心animation在iPhone上的2个图像之间交叉淡入淡出

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

在SO上阅读类似的问题使我相信,可以通过animation化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; 

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

你几乎在那里。

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

请注意,animation独立于UIImageView的实际值/内容。 所以你需要

 self.imageView.image = image2; 

…为您的图像设置最终结果。

我发现这个地方 – 它很好。

 UIImage * toImage = [UIImage imageNamed:@"image.png"]; [UIView transitionWithView:self.view duration:0.33f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.imageview.image = toImage; } completion:NULL]; 
 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.addAnimation(animation, forKey: "ImageFade") self.image = image } } 
 - (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++; }]; 

}

迅速解决

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

我的build议是只使用两个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]; } 

编辑:

如果你正在寻找你的代码不能工作的原因,那是因为你只是将属性设置为新的图像,然后添加一个无用的animation。 CABasicAnimation具有fromValuefromValue需要设置,然后将该animation添加到图层,它应该做animation。 不要手动设置内容。