0

有人可以告诉我如何在更改初始帧时为交叉溶解过渡设置动画吗?

我的代码:

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 会适应更新的帧。

谢谢你。

4

1 回答 1

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 }
}
于 2018-05-15T14:10:44.840 回答