斯威夫特:试图旋转绘制的形状?

时间:2017-07-25 03:11:48

标签: ios swift rotation core-graphics core-animation

我无法尝试旋转我在以下代码中绘制的形状:我尝试使用:

triangle.transform = triangle.transform.rotated(by: 3.14159)

但是这给了我一个错误"' CATransform3D'没有会员'旋转'"。我不确定如何旋转形状。

override func viewDidLoad() {
    super.viewDidLoad()
    let trianglePath = UIBezierPath()
    trianglePath.move(to: CGPoint(x: UIScreen.main.bounds.size.width * 0.5, y: UIScreen.main.bounds.height * 0.5))
    trianglePath.addLine(to: CGPoint(x: (UIScreen.main.bounds.size.width * 0.5) + 100, y: (UIScreen.main.bounds.height * 0.5) - 50))
    trianglePath.addLine(to: CGPoint(x: (UIScreen.main.bounds.size.width * 0.5) + 100, y: (UIScreen.main.bounds.height * 0.5) + 50))
    trianglePath.close()

    let triangle = CAShapeLayer()
    triangle.path = trianglePath.cgPath
    triangle.fillColor = UIColor.gray.cgColor
    self.view.layer.addSublayer(triangle)
}

1 个答案:

答案 0 :(得分:0)

使用它来旋转三角形,您可以使用CGAffineTransform

 triangle.setAffineTransform(CGAffineTransform(rotationAngle: .pi))

或使用CATransform3D,通过pi

在Z轴上旋转
 triangle.transform = CATransform3DMakeRotation(.pi, 0, 0, 1)

但是对于这项工作,您还需要调整CAShapeLayer框架

 triangle.frame = self.view.bounds //this do the trick

显示

后的动画
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    UIView.animate(withDuration: 0.1) { 
          triangle.setAffineTransform(CGAffineTransform(rotationAngle: .pi)) //this 
          triangle.transform = CATransform3DMakeRotation(.pi, 0, 0, 1) //or this
    }
}

希望这有帮助