请检查以下代码,objectToMove是从Storyboard中测试的UIView。在此使用了您提供的路径代码,但我添加了更多的半径。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var objectToMove: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let orbit = CAKeyframeAnimation(keyPath: "position")
var affineTransform = CGAffineTransformMakeRotation(0.0)
affineTransform = CGAffineTransformRotate(affineTransform, CGFloat(M_PI))
let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100 - (100/2),y: 100 - (100/2)), radius: CGFloat(100), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
orbit.path = circlePath.CGPath
orbit.duration = 4
orbit.additive = true
orbit.repeatCount = 100
orbit.calculationMode = kCAAnimationPaced
orbit.rotationMode = kCAAnimationRotateAuto
objectToMove.layer .addAnimation(orbit, forKey: "orbit")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
clockwise: false
替代clockwise: true
- Reinier Melian