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

标签 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")
}

最佳答案

要重新启动动画,您必须执行以下操作,请检查以下代码。

检查扩展

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

关于ios - 按下主页按钮并再次重新启动应用程序时动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45094328/

相关文章:

swift - 使用 UIBezierPath 点绘制矩形时如何圆角

swift - 如何不使深灰色变得透明,从图像中去除背景

iphone - UIView的-hitTest :withEvent: called three times?

ios - 在 Swift 中创建动画 gif 时出现问题

ios - 尝试通过 ios 应用程序通过 facebook 登录时出现错误

javascript - 从 webapp 收到消息时如何从 WKWebview 调用 javascript 函数

ios - 是否可以将 GestureRecognizer 添加到自定义 UIView subview 中的标签

ios - 如何将绘制的文本移动到 UIView 的顶部?

ios - 点击另一个按钮时隐藏 UIButton

ios - 如何将第一个值设置为表格颜色