Answers:
编辑:下面的@algal有更好的解决方案。
另一种方法是使用预定义的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
使用新的基于块的UIKit animation
方法可以更加简单。
假设以下代码位于视图控制器中,并且您要交叉溶解的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)
斯威夫特3、4和5
let toImage = UIImage(named:"myname.png")
UIView.transition(with: self.imageView,
duration: 0.3,
options: .transitionCrossDissolve,
animations: {
self.imageView.image = toImage
},
completion: nil)
对于Swift 3.0.1:
UIView.transition(with: self.imageView,
duration:0.5,
options: .transitionCrossDissolve,
animations: { self.imageView.image = newImage },
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;
}
您可以执行相反操作以淡出图像。之后淡出。您只需将其从Superview中删除(即UIImageView
)。[imageView removeFromSuperview]
。
您还可以将淡入功能包装在子类中,以便随后将其用作公共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:
函数中实际实现淡入的方式可能会改变,并且可能是此问题的其他答案中描述的其他出色示例之一- 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),仅用作概念证明,并且出于清晰和重点的考虑,通过省略重要的不相关代码来保持相关性。请不要盲目地复制粘贴。
我需要过渡才能无限期地重复。为此花费了很多的尝试和错误,但是我终于得到了想要的最终结果。这些是用于将图像动画添加到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;
}
在UIView.transition()
使用.transitionCrossDissolve
选项并遇到问题之后(我试图对一个UIImageView中的图像进行动画处理,并且过渡是在没有动画的情况下立即发生的),我发现您只需要再添加一个选项即可让您对视图内更改的属性进行动画处理(Swift 4.2):
UIView.transition(with: self.imageView,
duration: 1,
options: [.allowAnimatedContent, .transitionCrossDissolve],
animations: { self.imageView.image = newImage },
completion: nil)
另外:如果您在imageView上有任何子视图,它也会被重绘,并且可能会阻止动画。例如,我的子视图的子视图上带有模糊效果,在这种情况下,动画不起作用。因此,我只是更改了视图层次结构,并将模糊移至其自己的视图,并将其放在imageView上。
通过使用该highlightedImage
属性,可以使其更加简单。这是Swift 3中的示例。首先设置正常图像和突出显示的图像:
let imageView = UIImageView(image: UIImage(named: "image"), highlightedImage: UIImage(named: "highlightedImage"))
当您想在动画之间进行切换时:
UIView.transition(with: imageView, duration: 0.3, options: .transitionCrossDissolve, animations: { self.imageView.isHighlighted = !self.imageView.isHighlighted}, completion: .none)