在更改UIImageView的图像时淡入淡出

而不是创build两个UIImageViews ,这似乎是合乎逻辑的,只需更改一个视图的image 。 如果我这样做,是否有两个图像之间淡入/淡出而不是瞬间切换?

编辑:从下面的@algal有一个更好的解决scheme。

另一种方法是使用预定义的CAAnimation转换:

 CATransition *transition = [CATransition animation]; transition.duration = 0.25; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; transition.type = kCATransitionFade; transition.delegate = self; [self.view.layer addAnimation:transition forKey:nil]; view1.hidden = YES; view2.hidden = NO; 

请参阅Apple提供的View Transitions示例项目: https : //developer.apple.com/library/content/samplecode/ViewTransitions/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007411

使用新的基于块的UIKitanimation方法可以简单得多。

假设下面的代码在视图控制器中,并且您想要交叉融合的UIImageView是self.view可寻址的子视图,通过属性self.imageView。 那么你需要的是:

 UIImage * toImage = [UIImage imageNamed:@"myname.png"]; [UIView transitionWithView:self.imageView duration:5.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.imageView.image = toImage; } completion:nil]; 

完成。

要在Swift中完成,就像这样:

 let toImage = UIImage(named:"myname.png") UIView.transitionWithView(self.imageView, duration:5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { self.imageView.image = toImage }, completion: nil) 

Swift 3.0

  let toImage = UIImage(named:"myname.png") UIView.transition(with: self.imageView, duration: 0.3, options: .transitionCrossDissolve, animations: { self.imageView.image = toImage }, completion: nil) 

是的,你说的是绝对正确的,那就是做到这一点的方法。 我写了这个方法,总是用这个来淡入我的图片。 我为此处理CALayer 。 您需要为此导入Core Animation。

 + (void)fadeInLayer:(CALayer *)l { CABasicAnimation *fadeInAnimate = [CABasicAnimation animationWithKeyPath:@"opacity"]; fadeInAnimate.duration = 0.5; fadeInAnimate.repeatCount = 1; fadeInAnimate.autoreverses = NO; fadeInAnimate.fromValue = [NSNumber numberWithFloat:0.0]; fadeInAnimate.toValue = [NSNumber numberWithFloat:1.0]; fadeInAnimate.removedOnCompletion = YES; [l addAnimation:fadeInAnimate forKey:@"animateOpacity"]; return; } 

你可以做相反的淡出图像。 消失后。 你只需从超级视图(这是UIImageView )中删除它。 [imageView removeFromSuperview]

您也可以将淡入function打包在一个子类中,以便您可以将其用作常见的UIImageView,如下例所示:

 IMMFadeImageView *fiv=[[IMMFadeImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)]; [self.view addSubview:fiv]; fiv.image=[UIImage imageNamed:@"initialImage.png"]; fiv.image=[UIImage imageNamed:@"fadeinImage.png"]; // fades in 

一个可能的实现如下。

注意:实际上在setImage:函数中实现淡入的方式可能会改变,并且可能是其他优秀示例中的其他解决方法之一 – 创build一个额外的即时UIImageView因为我是在这里做你的具体情况可能是不可接受的开销

IMMFadeImageView.h

 #import <UIKit/UIKit.h> @interface IMMFadeImageView : UIImageView @property (nonatomic,assign) float fadeDuration; @end 

IMMFadeImageView.m

 #import "IMMFadeImageView.h" @implementation IMMFadeImageView @synthesize fadeDuration; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.fadeDuration=1; } return self; } -(void)setImage:(UIImage *)newImage{ if(!self.image||self.fadeDuration<=0){ super.image=newImage; } else { UIImageView *iv=[[UIImageView alloc] initWithFrame:self.bounds]; iv.contentMode=self.contentMode; iv.image=super.image; iv.alpha=1; [self addSubview:iv]; super.image=newImage; [UIView animateWithDuration:self.fadeDuration delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ iv.alpha=0; } completion:^(BOOL finished) { [iv removeFromSuperview]; }]; } } 

上面的代码依赖于一些假设(包括在你的XCode项目中启用了ARC),仅仅是作为一个概念certificate,为了清晰和专注,通过省略重要的不相关的代码保持相关性。 请不要盲目复制粘贴。

我需要无限期重复的过渡。 这个尝试和错误很多,但我终于得到了我期待的最终结果。 这些是用于将图像animation添加到UITableViewCell中的UIImageView的代码片段。

这是相关的代码:

 @interface SomeViewController () @property(nonatomic, strong) NSMutableArray *imagesArray; @property(nonatomic, assign) NSInteger varietyImageAnimationIndex; @property(nonatomic, assign) BOOL varietyImagesAnimated; @end @implementation SomeViewController @synthesize imagesArray; @synthesize varietyImageAnimationIndex; @synthesize varietyImagesAnimated; ... // NOTE: Initialize the array of images in perhaps viewDidLoad method. -(void)animateImages { varietyImageAnimationIndex++; [UIView transitionWithView:varietyImageView duration:2.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ varietyImageView.image = [imagesArray objectAtIndex:varietyImageAnimationIndex % [imagesArray count]]; } completion:^(BOOL finished) { [self animateImages]; }]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... [cell.imageView setImage:[imagesArray objectAtIndex:0]]; [self setVarietyImageView:cell.imageView]; if (! varietyImagesAnimated) { varietyImagesAnimated = YES; [self animateImages]; } ... return cell; } 

对于Swift 3.0.1:

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

参考: https : //gist.github.com/licvido/bc22343cacfa3a8ccf88

这是我认为最短的做法。 创build一个UIViewanimation并将其提交到imageView。

 [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; [myImageView setAlpha:0.0]; [UIView commitAnimations]; 

通过使用highlightedImage属性,这可以变得更简单一些。 这里是Swift 3中的一个例子。首先设置正常和突出显示的图像:

 let imageView = UIImageView(image: UIImage(named: "image"), highlightedImage: UIImage(named: "highlightedImage")) 

而当你想改变这些animation:

 UIView.transition(with: imageView, duration: 0.3, options: .transitionCrossDissolve, animations: { self.imageView.isHighlighted = !self.imageView.isHighlighted}, completion: .none)