我想在图层上的图像上实现3D漫游,但它不起作用。 我发现动画在开始时立即停止了函数调用。所以我认为这就是动画不起作用的原因。
以下是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
// Do any additional setup after loading the view.
let doorLayer = CALayer()
doorLayer.frame = CGRect(x: 0, y: 0, width: 128, height: 256)
doorLayer.position = CGPoint(x: 150 - 64, y: 150)
doorLayer.anchorPoint = CGPoint(x: 0, y: 0.5)
doorLayer.contents = UIImage(named: "door")?.cgImage
self.view.layer.addSublayer(doorLayer)
var perspective = CATransform3DIdentity
perspective.m34 = -1.0 / 500.0
self.view.layer.sublayerTransform = perspective
let animation = CABasicAnimation()
animation.keyPath = "transform.rotation.y"
animation.toValue = -Double.pi/2
animation.duration = 2.0
animation.repeatCount = 5
animation.autoreverses = true
animation.delegate = self
doorLayer.add(animation, forKey: "rotationAnimation")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
print(#function)
}
视图显示时,消息立即显示。 我该怎么办?