试图做一个物体的 360 度旋转
trying to do a 360 degree rotation of a object
我的 swift 代码试图对名为 box 的图像视图进行 360 度旋转。但是代码确实会旋转,因为我正在使用代码 self.box.transform = self.box.transform.rotated(by: CGFloat.pi * 2) 进行 360 度旋转。 Pi *2 是 360 度旋转,所以我认为它可能会混淆编译。就像轮换已经发生一样。代码应该绕圈一圈。
import UIKit
class ViewController: UIViewController {
var box = UIImageView()
var box1 = UIImageView()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.setNeedsLayout()
view.layoutIfNeeded()
self.box.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 1))
Rotate()
}
override func viewDidLoad() {
super.viewDidLoad()
box.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(box)
box.backgroundColor = .systemPink
box1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(box1)
box1.backgroundColor = .purple
NSLayoutConstraint.activate([
box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25),
box.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.25),
box.centerXAnchor.constraint(equalTo: view.centerXAnchor),
box.bottomAnchor.constraint(equalTo: view.centerYAnchor),
box1.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.05),
box1.widthAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.05),
box1.centerXAnchor.constraint(equalTo: view.centerXAnchor),
box1.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
// Do any additional setup after loading the view.
}
func Rotate(){
let rotation = UIViewPropertyAnimator(duration: 5, curve: .linear, animations: {
self.box.transform = self.box.transform.rotated(by: CGFloat.pi * 2)
})
rotation.startAnimation()
}
}
extension UIView{
func setAnchorPoint(anchorPoint: CGPoint) {
var newPoint = CGPoint(x: self.bounds.size.width * anchorPoint.x, y: self.bounds.size.height * anchorPoint.y)
var oldPoint = CGPoint(x: self.bounds.size.width * self.layer.anchorPoint.x, y: self.bounds.size.height * self.layer.anchorPoint.y)
newPoint = newPoint.applying(self.transform)
oldPoint = oldPoint.applying(self.transform)
var position : CGPoint = self.layer.position
position.x -= oldPoint.x
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
self.layer.position = position;
self.layer.anchorPoint = anchorPoint;
}
}
对于变换,Core Animation 计算实现旋转的最短路径。对于 360 度旋转,最短路径是什么都不做。有一个解决方法 解释了如何执行基本相同的动画。
我的 swift 代码试图对名为 box 的图像视图进行 360 度旋转。但是代码确实会旋转,因为我正在使用代码 self.box.transform = self.box.transform.rotated(by: CGFloat.pi * 2) 进行 360 度旋转。 Pi *2 是 360 度旋转,所以我认为它可能会混淆编译。就像轮换已经发生一样。代码应该绕圈一圈。
import UIKit
class ViewController: UIViewController {
var box = UIImageView()
var box1 = UIImageView()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.setNeedsLayout()
view.layoutIfNeeded()
self.box.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 1))
Rotate()
}
override func viewDidLoad() {
super.viewDidLoad()
box.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(box)
box.backgroundColor = .systemPink
box1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(box1)
box1.backgroundColor = .purple
NSLayoutConstraint.activate([
box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25),
box.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.25),
box.centerXAnchor.constraint(equalTo: view.centerXAnchor),
box.bottomAnchor.constraint(equalTo: view.centerYAnchor),
box1.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.05),
box1.widthAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.05),
box1.centerXAnchor.constraint(equalTo: view.centerXAnchor),
box1.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
// Do any additional setup after loading the view.
}
func Rotate(){
let rotation = UIViewPropertyAnimator(duration: 5, curve: .linear, animations: {
self.box.transform = self.box.transform.rotated(by: CGFloat.pi * 2)
})
rotation.startAnimation()
}
}
extension UIView{
func setAnchorPoint(anchorPoint: CGPoint) {
var newPoint = CGPoint(x: self.bounds.size.width * anchorPoint.x, y: self.bounds.size.height * anchorPoint.y)
var oldPoint = CGPoint(x: self.bounds.size.width * self.layer.anchorPoint.x, y: self.bounds.size.height * self.layer.anchorPoint.y)
newPoint = newPoint.applying(self.transform)
oldPoint = oldPoint.applying(self.transform)
var position : CGPoint = self.layer.position
position.x -= oldPoint.x
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
self.layer.position = position;
self.layer.anchorPoint = anchorPoint;
}
}
对于变换,Core Animation 计算实现旋转的最短路径。对于 360 度旋转,最短路径是什么都不做。有一个解决方法