斯威夫特:在框架的同时褪色两个图像

时间:2018-05-15 14:04:11

标签: swift4 uiviewanimation caanimation catransition ios-animations

有人能告诉我如何在查看初始帧的同时为交叉溶解过渡设置动画?

我的代码:

self.image = initialImage

UIView.transition(with: _self, duration: 10.0, options: [.transitionCrossDissolve, .allowUserInteraction], animations: {
     self.image = newImage
})

也尝试过:

let transition = CATransition()
transition.duration = 10
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionFade
transition.delegate = self

self?.layer.add(transition, forKey: nil)

在动画运行时更改帧大小会使初始图像保持初始帧大小,而newImage会适应更新的帧。

谢谢。

1 个答案:

答案 0 :(得分:1)

为了更简单的方法,创建两个图像视图实例并为第一个实体的alpha设置动画,同时更改它们的框架。

let firstImageView = UIImageView(image: UIImage(named: "first"))
firstImageView.frame = CGRect(x: 100, y: 100, width: 200, height: 200)

let secondImageView = UIImageView(image: UIImage(named: "second"))
secondImageView.frame = firstImageView.frame

//the second image view should obscure the first one, that's why it's the first in the array
let imageViews = [secondImageView, firstImageView]

imageViews.forEach { view.addSubview($0) }

let newFrame = CGRect(x: 50, y: 50, width: 100, height: 100)

UIView.animate(withDuration: 10) {
    firstImageView.alpha = 0

    imageViews.forEach { $0.frame = newFrame }
}