Swift:试图旋转绘制的形状?
问题描述:
我有麻烦试图旋转我得出以下代码形状:我尝试使用:Swift:试图旋转绘制的形状?
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)
}
答
使用此旋转你的三角形,您可以用使用CGAffineTransform
triangle.setAffineTransform(CGAffineTransform(rotationAngle: .pi))
或使用CATransform3D
,通过PI
triangle.transform = CATransform3DMakeRotation(.pi, 0, 0, 1)
,但这项工作需要在Z轴旋转还调整了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
}
}
希望这有助于
**提示**这是有道理的,一个** ** 3D变换不支持围绕一个未指定** **旋转axis :-) –
@NicolasMiari我以为我使用的是2D变换?我应该使用CATransform3DMakeRotation吗? – diet7up