【问题标题】:CATransform3DMakeRotation anchorpoint and position in SwiftCATransform3D在Swift中制作旋转锚点和位置
【发布时间】: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()
}

【问题讨论】:

标签: ios swift calayer catransform3d


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2019-01-14
    相关资源
    最近更新 更多