如何立即停止动画并开始新动画?

时间:2018-09-12 06:45:43

标签: ios swift animation

在这种情况下,按下连接按钮,并持续进行动画处理,直到VPN连接为止。然后,我想停止动画并加载一组新图像,然后仅对其进行动画处理。

@IBOutlet weak var vpnButton: UIImageView!
var premiumImages: [UIImage] = []
var loadingImages: [UIImage] = []


@objc private func vpnStatusDidChange(_ notification: Notification) {
        let nevpnconn = notification.object as! NEVPNConnection
        let status = nevpnconn.status
        switch status {
        case NEVPNStatus.connecting:
            self.animateImages(imageView: self.vpnButton, images: loadingImages, duration: 1.0, isInfinite: true)
            self.vpnButton.image = UIImage(named: "Red-1")
}

负责动画处理的方法:

func animateImages(imageView: UIImageView, images: [UIImage], duration: Double, isInfinite: Bool=false) {
        imageView.stopAnimating()
        imageView.animationImages = images
        imageView.animationDuration = duration
        if !isInfinite {
            imageView.animationRepeatCount = 1
        } else {
            imageView.animationRepeatCount = 0
        }
        imageView.startAnimating()
    }

因此,每当我调用它时,它都会停止所有现有的动画,并用新的图像对其进行动画处理。

VPN连接后:

case NEVPNStatus.connected:
     self.vpnButton.highlightedImage = UIImage(named: "OrangePressed")
     self.animateImages(imageView: self.vpnButton, images: premiumImages, duration: 0.25)
     self.vpnButton.image = UIImage(named: "OrangePremium-3")

只有loadingImages具有适当的动画效果。第二组premiumImages不设置动画,仅显示最后一帧。

我怀疑stopAnimating()需要花费一些时间来完成,并且理想情况下,我需要在此处使用完成处理程序来知道何时启动imageView.startAnimating()

0 个答案:

没有答案