开始/停止图像视图旋转动画

2024-04-26

我有一个开始/停止按钮和一个我想要旋转的图像视图。

当我按下按钮时,我希望图像开始旋转,当我再次按下按钮时,图像应该停止旋转。我目前正在使用UIView动画,但我还没有找到停止视图动画的方法。

我希望图像旋转,但是当动画停止时,图像不应返回到起始位置,而应继续动画。

var isTapped = true

    @IBAction func startStopButtonTapped(_ sender: Any) {
       ruotate()
       isTapped = !isTapped
    }

    func ruotate() {
        if isTapped {
            UIView.animate(withDuration: 5, delay: 0, options: .repeat, animations: { () -> Void in
            self.imageWood.transform =     self.imageWood.transform.rotated(by: CGFloat(M_PI_2))
            }, completion: { finished in
            ruotate()
            })
    }  }

这是我的代码,但它不像我那样工作。


斯威夫特3.x

开始动画

let rotationAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotationAnimation.toValue = NSNumber(value: .pi * 2.0)
    rotationAnimation.duration = 0.5;
    rotationAnimation.isCumulative = true;
    rotationAnimation.repeatCount = .infinity;
    self.imageWood?.layer.add(rotationAnimation, forKey: "rotationAnimation")

停止动画

self.imageWood?.layer.removeAnimation(forKey: "rotationAnimation")

斯威夫特2.x

开始动画

let rotationAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotationAnimation.toValue = NSNumber(double: M_PI * 2.0)
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = true;
    rotationAnimation.repeatCount = .infinity;
    self.imageWood?.layer.addAnimation(rotationAnimation, forKey: "rotationAnimation")

停止动画

self.imageWood?.layer.removeAnimation(forKey: "rotationAnimation")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

开始/停止图像视图旋转动画 的相关文章

随机推荐