swiftanimationtransformcgfloat

trying to do a 360 degree rotation of a object


My swift code is attempting to do a 360 degree rotation on a image view named box. The code does rotate however because I am doing a 360 degree rotation with code self.box.transform = self.box.transform.rotated(by: CGFloat.pi * 2). Pi *2 is a 360 degree rotation so I think somehow its possibly confusing the compile. Like the rotation has already occured. The code should take one lap around the circle.

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

Solution

  • For transformations, Core Animation calculates the shortest path to achieve the rotation. For a 360 degree rotation, the shortest path is to do nothing. There is a workaround here that explains how to perform essentially the same animation.