如何在用户点击3D CAGradientLayer
时从左向右旋转,并在第二次点击后向后旋转(从右向左)?而且我还需要在动画的中间将渐变更改为不同的颜色(基本上是当视图旋转90度并从用户的眼睛中“隐藏”时)
由于某种原因,我的实现只在一个方向(从左到右)旋转视图。我曾尝试过使用负数PI
,但结果有点怪异。
我到目前为止所拥有的
func open() {
var flipTransform: CATransform3D
flipTransform = CATransform3DIdentity
flipTransform.m34 = 1.0 / -1000
flipTransform = CATransform3DRotate(flipTransform, .pi, 0.0, 1.0, 0.0)
UIView.animate(withDuration: 2.0, animations: {
self.tile.layer.transform = flipTransform
DispatchQueue.main.asyncAfter(deadline: .now() + self.duration / 2, execute: {
self.addGradientLayer(from: .red, to: .yellow)
})
})
}
func close() {
UIView.animate(withDuration: 2.0, animations: {
self.tile.layer.transform = CATransform3DIdentity
DispatchQueue.main.asyncAfter(deadline: .now() + self.duration / 2, execute: {
self.addGradientLayer(from: .black, to: .lightGray)
})
})
}
func addGradientLayer(from: UIColor, to: UIColor) {
tile.layer.sublayers?.filter { $0 is CAGradientLayer }.forEach { $0.removeFromSuperlayer() }
let gradientLayer = CAGradientLayer()
gradientLayer.frame = tile.bounds
gradientLayer.colors = [from.cgColor, to.cgColor]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
tile.layer.insertSublayer(gradientLayer, at: 0)
}