小编Nix*_*aAK的帖子

使用animateKeyframesWithDuration在屏幕上创建一个连续旋转的方块

我尝试使用下面的代码在屏幕上创建一个连续旋转的方块.但我不知道为什么转速在变化.如何更改代码以使旋转速度不变?我尝试了不同UIViewKeyframeAnimationOptions,但似乎没有一个工作.

override func viewDidLoad() {
    super.viewDidLoad()

    let square = UIView()
    square.frame = CGRect(x: 55, y: 300, width: 40, height: 40)
    square.backgroundColor = UIColor.redColor()
    self.view.addSubview(square)

    let duration = 1.0
    let delay = 0.0
    let options = UIViewKeyframeAnimationOptions.Repeat
        UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {
        let fullRotation = CGFloat(M_PI * 2)

        UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/3, animations: {
            square.transform = CGAffineTransformMakeRotation(1/3 * fullRotation)
        })                        
        UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 1/3, animations: {
            square.transform = CGAffineTransformMakeRotation(2/3 * fullRotation)
        })                        
        UIView.addKeyframeWithRelativeStartTime(2/3, relativeDuration: 1/3, animations: {
            square.transform = …
Run Code Online (Sandbox Code Playgroud)

animation rotation repeat ios swift

8
推荐指数
2
解决办法
3190
查看次数

标签 统计

animation ×1

ios ×1

repeat ×1

rotation ×1

swift ×1