当按下主页按钮并且应用程序再次重新启动时,动画不起作用

时间:2017-07-14 03:55:26

标签: ios swift uiview

当我按下主页按钮然后重新启动应用程序时,我的动画停止了运行。设置按钮停止旋转,闪烁标签刚刚消失。这是我的动画代码:

眨眼动画:

extension UILabel {

    func startBlink() {
        UIView.animate(withDuration: 0.8,
                       delay:0.0,
                       options:[.autoreverse, .repeat],
                       animations: {
                        self.alpha = 0

        }, completion: nil)
    }
}

旋转动画:

extension UIButton {

    func startRotating() {

        UIView.animate(withDuration: 4.0, delay: 0.0, options:[.autoreverse, .repeat,UIViewAnimationOptions.allowUserInteraction], animations: {

            self.transform = CGAffineTransform(rotationAngle: CGFloat.pi)


        }, completion: nil)                                    
    }    
}

我在哪里运行:

override func viewDidLoad() {
    super.viewDidLoad()

    settingsButton.layer.cornerRadius = 0.5 * settingsButton.bounds.size.width
    settingsButton.clipsToBounds = true
    settingsButton.imageView?.contentMode = .scaleAspectFit



    NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)


}

func appMovedToForeground() {
    tapToPlayLabel.startBlink()
    settingsButton.startRotating()
    print("DID")
}

3 个答案:

答案 0 :(得分:2)

要重新开始动画,你必须在下面进行操作,请查看以下代码。

检查扩展程序

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tapToPlayLabel: UILabel!
    @IBOutlet weak var settingsButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        settingsButton.layer.cornerRadius = settingsButton.frame.size.width/2
        settingsButton.clipsToBounds = true
        //settingsButton.imageView?.contentMode = .scaleAspectFit

        settingsButton.startRotating()
        tapToPlayLabel.startBlink()

        NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
    }

    func appMovedToForeground() {
        self.tapToPlayLabel.startBlink()
        self.settingsButton.startRotating()
    }
}

extension UILabel {
    func startBlink() {
        self.alpha = 1
        UIView.animate(withDuration: 0.8,
                       delay:0.0,
                       options:[.autoreverse, .repeat],
                       animations: {
                        self.alpha = 0

        }, completion: nil)
    }
}

extension UIButton {

    func startRotating() {
        self.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
        UIView.animate(withDuration: 4.0, delay: 0.0, options:[.autoreverse, .repeat,UIViewAnimationOptions.allowUserInteraction], animations: {
            self.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
        }, completion: nil)
    }
}

<强>输出

enter image description here

答案 1 :(得分:0)

我认为您需要稍微延迟运行您的动画,因为当应用程序移动到前台时,已经有应用程序的委托执行。

答案 2 :(得分:0)

或者您可以在UILabel上添加CALayerAnimation

extension UILabel {

    func startBlink() {

        let scaleAnimation = CAKeyframeAnimation(keyPath: "transform")
        scaleAnimation.delegate = self as? CAAnimationDelegate
        let transform: CATransform3D = CATransform3DMakeScale(1.5, 1.5, 1)
        scaleAnimation.values = [NSValue(caTransform3D: CATransform3DIdentity), NSValue(caTransform3D: transform), NSValue(caTransform3D: CATransform3DIdentity)]
        scaleAnimation.duration = 0.5
        scaleAnimation.repeatCount = 100000000
        self.layer.add(scaleAnimation as? CAAnimation ?? CAAnimation(), forKey: "scaleText")
    }
    func startRotating() {

        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.fromValue = 0
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 2
        rotation.isCumulative = true
        rotation.repeatCount = .greatestFiniteMagnitude
        self.layer.add(rotation, forKey: "rotationAnimation")

    }
}