【发布时间】:2016-10-08 21:44:10
【问题描述】:
您好,我正在尝试旋转三角形 CAShapeLayer。北极 CAShapeLayer 在自定义 UIView 类中定义。我在类中有两个函数,一个用于设置图层属性,一个用于设置旋转动画。旋转动画效果很好,但旋转完成后它会恢复到原始位置。我希望图层在旋转动画后保持在给定的角度(positionTo = 90.0)。
我正在尝试设置转换后的位置以在动画后保留正确的位置。我也尝试从presentation() 方法中获取位置,但这没有帮助。我已经阅读了很多关于框架、边界、锚点的文章,但我似乎仍然无法理解为什么这种转换旋转不起作用。
private func setupNorthPole(shapeLayer: CAShapeLayer) {
shapeLayer.frame = CGRect(x: self.bounds.width / 2 - triangleWidth / 2, y: self.bounds.height / 2 - triangleHeight, width: triangleWidth, height: triangleHeight)//self.bounds
let polePath = UIBezierPath()
polePath.move(to: CGPoint(x: 0, y: triangleHeight))
polePath.addLine(to: CGPoint(x: triangleWidth / 2, y: 0))
polePath.addLine(to: CGPoint(x: triangleWidth, y: triangleHeight))
shapeLayer.path = polePath.cgPath
shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
动画功能:
private func animate() {
let positionTo = CGFloat(DegreesToRadians(value: degrees))
let rotate = CABasicAnimation(keyPath: "transform.rotation.z")
rotate.fromValue = 0.0
rotate.toValue = positionTo //CGFloat(M_PI * 2.0)
rotate.duration = 0.5
northPole.add(rotate, forKey: nil)
let present = northPole.presentation()!
print("presentation position x " + "\(present.position.x)")
print("presentation position y " + "\(present.position.y)")
CATransaction.begin()
northPole.transform = CATransform3DMakeRotation(positionTo, 0.0, 0.0, 1.0)
northPole.position = CGPoint(x: self.bounds.width / 2, y: self.bounds.height / 2)
CATransaction.commit()
}
【问题讨论】:
-
好的,抱歉我之前的回答不好。我只是建议你放弃
present,放弃begin,放弃commit,并阅读我的书以了解如何使用CABasicAnimation 进行旋转:apeth.com/iOSBook/ch17.html#_using_a_cabasicanimation
标签: ios swift calayer catransform3d