如何应用模糊交叉淡化UIImageView的图像

时间:2015-04-22 22:59:03

标签: ios uiimageview caanimation uivisualeffectview uiblureffect

我试图交叉淡入新图像。这是一个添加了UIVisualEffectView的UIImageView:

self.bgBlurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:self.bgBlurEffect];

self.visualEffectView.frame = self.bgImageView.bounds;
[self.bgImageView addSubview:self.visualEffectView];

以下是我尝试淡入新图片的方式:

UIImage *toImage = [UIImage imageNamed:imgName];

CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 2.5;
crossFade.delegate = self;
crossFade.fromValue = (id)self.bgImageView.image.CGImage;
crossFade.toValue = (id)toImage.CGImage;
[self.bgImageView.layer addAnimation:crossFade forKey:@"animateContents"];
self.bgImageView.image = toImage;

这实际上可以在模拟器中使用,但在设备(iPhone 6)上,图像完全变为灰色2.5秒,然后新图像立即出现。关于如何解决这个问题的任何想法?

编辑:这不起作用:

[UIView transitionWithView:self.bgImageView
                  duration:2.5
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    self.bgImageView.image = toImage;
                } completion:nil];

1 个答案:

答案 0 :(得分:0)

也许这种方法适用于淡入淡出动画:

self.bgImageView.image = toImage;

CATransition *fadeTransition = [CATransition animation];
fadeTransition.duration = 2.5;
fadeTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
fadeTransition.type = kCATransitionFade;

[self.bgImageView.layer addAnimation:fadeTransition forKey:nil];