gpt4 book ai didi

ios - Swift:尝试旋转绘制的形状?

转载 作者:行者123 更新时间:2023-11-28 08:05:29 26 4
gpt4 key购买 nike

我在尝试旋转我在以下代码中绘制的形状时遇到问题:我尝试使用:

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

但这给了我一个错误“'CATransform3D' 类型的值没有成员'rotated'”。我不确定如何旋转形状。

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,在Z轴上旋转pi

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

希望对你有帮助

关于ios - Swift:尝试旋转绘制的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45293265/

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