How to paraphrase 2 images on iPhone using Core Animation

I am doing this to learn how to work with the Core Animation properties on iPhone (not to learn how to cross-photograph images as such).

Reading such questions in SO makes me think that this can be done by animating the .contents property of the UIImageView level , eg:

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

I received the item incorrectly or is not possible.

Update: The above code creates an empty UIImageView. When I change this line:

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

... Now I see the image, but it does not disappear, it just appears instantly.

+44
iphone core-animation
Oct 11 '09 at 8:52
source share
6 answers

You were almost there.

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

Please note that the animation does not depend on the actual values ​​/ contents of the UIImageView. Therefore you need

 self.imageView.image = image2; 

... to set the final result for your image.

+78
Oct. 15 '09 at 3:31
source share

I found this somewhere - it works great.

 UIImage * toImage = [UIImage imageNamed:@"image.png"]; [UIView transitionWithView:self.view duration:0.33f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.imageview.image = toImage; } completion:NULL]; 
+60
Apr 27 2018-12-12T00:
source share
 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 } } 
+18
May 08 '12 at 20:16
source share
 - (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++; }]; 

}

+1
Sep 30 '14 at 5:01
source share

Solution in quick

 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"); 
+1
Apr 05 '15 at 22:53
source share

My suggestion was to simply use two UIImageView on top of each other. At the top there is image1, and below - image2:

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

This will fade between the two images when you call it. If you want to just make one fade and delete the first image after completion:

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

Edit:

If you are looking for the reason why your code is not working, it is because you just set the property for the new image and then add useless animation. CABasicAnimation has the fromValue and toValue parameters that need to be set, then add this animation to the layer and it should make the animation. Do not install content manually.

0
Oct 11 '09 at 13:49
source share



All Articles