使用百分比更改UIImageView图像时淡入/淡化

时间:2017-10-06 07:30:14

标签: ios swift uiimageview

我想将UIImageView图像从一个图像更改为另一个图像。 但我不希望它完全动画到最后。我只是想让它以百分比的方式进行溶解。结果应该是包含两个图像的新图像,就像我会在某个时刻停止动画并保留结果一样。

我知道这会从头到尾完成所有动画:

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;

但后来我刚改变了图像。我希望它以某个百分比保持“合并”。这有点可能吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

此扩展适用于我:

extension UIImage {
    static func mergeImage(imageA: UIImage, imageB:UIImage, percentage: CGFloat) -> UIImage{

        UIGraphicsBeginImageContextWithOptions(imageA.size, false, 0.0)

        imageA.draw(at: CGPoint(x: 0, y: 0), blendMode: .normal, alpha: 1-percentage)

        imageB.draw(at: CGPoint(x: 0, y: 0), blendMode: .normal, alpha: percentage)

        let mergedImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return mergedImage!;
    }
}