gpt4 book ai didi

ios - Swift 中的 CATransform3DMakeRotation anchor 和位置

转载 作者:行者123 更新时间:2023-11-30 13:00:21 27 4
gpt4 key购买 nike

嗨,我正在尝试旋转三角形 CAShapeLayer。 NorthPole CAShapeLayer 在自定义 UIView 类中定义。我在类中有两个函数,一个用于设置图层属性,一个用于设置旋转动画。旋转动画工作正常,但旋转完成后它会恢复到原始位置。我希望图层在旋转动画后保持给定的角度 (positionTo = 90.0)。

我试图设置变换后的位置,以在动画后保留正确的位置。我也尝试从presentation() 方法中获取位置,但这没有帮助。我读过很多关于框架、边界、 anchor 的文章,但我似乎仍然无法理解为什么这种变换旋转不起作用。

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()
}

最佳答案

为了解决这个问题,在自定义 UIView 类中,我必须使用以下内容重写layoutSubviews():

  super.layoutSubviews()
let northPoleTransform: CATransform3D = northPole.transform
northPole.transform = CATransform3DIdentity
setupNorthPole(northPole)
northPole.transform = northPoleTransform

关于ios - Swift 中的 CATransform3DMakeRotation anchor 和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com