如何在UIImageView中animation变化的图像?
我有一个图像的UIImageView。 现在我有一个全新的图像(graphics文件),并希望显示在这个UIImageView。 如果我刚刚设置
myImageView.image = newImage;
新的图像立即可见。 不能animation。
我希望它很好地淡入新的形象。 我想也许有一个更好的解决scheme,而不仅仅是创build一个新的UIImageView和混合animation?
我不确定是否可以使用淡入淡出效果对UIViews进行animation处理,因为看来所有支持的视图过渡都是在UIViewAnimationTransition
枚举中定义的。 使用CoreAnimation可以实现衰落效果。 此方法的示例示例:
#import <QuartzCore/QuartzCore.h> ... imageView.image = [UIImage imageNamed:(i % 2) ? @"3.jpg" : @"4.jpg"]; CATransition *transition = [CATransition animation]; transition.duration = 1.0f; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; transition.type = kCATransitionFade; [imageView.layer addAnimation:transition forKey:nil];
[UIView transitionWithView:textFieldimageView duration:0.2f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ imageView.image = newImage; } completion:nil];
是另一种可能性
这里是Swift 3中的正确答案:
UIView.transition(with: imageView, duration: 1, options: .transitionCrossDissolve, animations: { self.imageView.image = toImage }, completion: nil)
这里是Swift中的一个例子,它将首先交叉解散一个新的图像,然后添加一个弹性animation:
var selected: Bool { willSet(selected) { let expandTransform:CGAffineTransform = CGAffineTransformMakeScale(1.15, 1.15); if (!self.selected && selected) { UIView.transitionWithView(self.imageView, duration:0.1, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { self.imageView.image = SNStockCellSelectionAccessoryViewImage(selected) self.imageView.transform = expandTransform }, completion: {(finished: Bool) in UIView.animateWithDuration(0.4, delay:0.0, usingSpringWithDamping:0.40, initialSpringVelocity:0.2, options:UIViewAnimationOptions.CurveEaseOut, animations: { self.imageView.transform = CGAffineTransformInvert(expandTransform) }, completion:nil) }) } } } var imageView:UIImageView
如果imageView
作为子视图正确添加到视图,则在selected = false
到selected = true
之间切换应该用有弹性的animation交换图像。 SNStockCellSelectionAccessoryViewImage
只是基于当前的select状态返回一个不同的图像,如下所示:
private let SNStockCellSelectionAccessoryViewPlusIconSelected:UIImage = UIImage(named:"PlusIconSelected")! private let SNStockCellSelectionAccessoryViewPlusIcon:UIImage = UIImage(named:"PlusIcon")! private func SNStockCellSelectionAccessoryViewImage(selected:Bool) -> UIImage { return selected ? SNStockCellSelectionAccessoryViewPlusIconSelected : SNStockCellSelectionAccessoryViewPlusIcon }
下面的GIF例子有点慢了,实际的animation发生得更快:
码:
var fadeAnim:CABasicAnimation = CABasicAnimation(keyPath: "contents"); fadeAnim.fromValue = firstImage; fadeAnim.toValue = secondImage; fadeAnim.duration = 0.8; //smoothest value imageView.layer.addAnimation(fadeAnim, forKey: "contents"); imageView.image = secondImage;
例:
有趣的,更详细的解决scheme 🙁 切换水龙头 )
let fadeAnim:CABasicAnimation = CABasicAnimation(keyPath: "contents"); switch imageView.image { case firstImage?: fadeAnim.fromValue = firstImage; fadeAnim.toValue = secondImage; imageView.image = secondImage; default: fadeAnim.fromValue = secondImage; fadeAnim.toValue = firstImage; imageView.image = firstImage; } fadeAnim.duration = 0.8; imageView.layer.addAnimation(fadeAnim, forKey: "contents");
尝试这个:
_imageView.image = image; [_imageView.layer addAnimation:[CATransition animation] forKey:kCATransition];
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"contents"]; fadeAnim.fromValue = (__bridge id)imageView.image.CGImage; fadeAnim.toValue = (__bridge id)[UIImage imageNamed:@"newImage.png"].CGImage; fadeAnim.duration = 2.0; [imageView.layer addAnimation:fadeAnim forKey:@"contents"]; imageView.layer.contents = (__bridge id)[UIImage imageNamed:@"newImage.png"].CGImage;
为什么不试试这个。
NSArray *animationFrames = [NSArray arrayWithObjects: [UIImage imageWithName:@"image1.png"], [UIImage imageWithName:@"image2.png"], nil]; UIImageView *animatedImageView = [[UIImageView alloc] init]; animatedImageView.animationImages = animationsFrame; [animatedImageView setAnimationRepeatCount:1]; [animatedImageView startAnimating];
这里有几个不同的方法: UIAnimations我的回忆这听起来像你的挑战。
编辑:我太懒了:)
在这篇文章中,我指的是这个方法:
[newView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen! [UIView beginAnimations:@"animateTableView" context:nil]; [UIView setAnimationDuration:0.4]; [newView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen! [UIView commitAnimations];
但不是animation框架,你animation的alpha:
[newView setAlpha:0.0]; // set it to zero so it is all gone. [UIView beginAnimations:@"animateTableView" context:nil]; [UIView setAnimationDuration:0.4]; [newView setAlpha:0.5]; //this will change the newView alpha from its previous zero value to 0.5f [UIView commitAnimations];
从iOS 5开始,这个更容易:
[newView setAlpha:0.0]; [UIView animateWithDuration:0.4 animations:^{ [newView setAlpha:0.5]; }];
随着Swift 3
extension UIImageView{ var imageWithFade:UIImage?{ get{ return self.image } set{ UIView.transition(with: self, duration: 0.5, options: .transitionCrossDissolve, animations: { self.image = newValue }, completion: nil) } } }
用法:
myImageView.imageWithFade = myImage