如何连续闪烁一个对象直到应用程序被杀死?

How to blink an object continuously till the app gets killed?

在我的情节提要中,我有一个按钮,我想无限次地闪烁它直到程序运行 killed.Here 是我目前所做的,这段代码使按钮只动画一次。

@IBOutlet weak var blinker: UIButton!

 func Blink(){
        blinker.alpha = 0.0
        UIButton.animateWithDuration(1, animations: {
            self.blinker.alpha = 1.0
            }, completion: {
                (value: Bool) in
                println(">>> Animation done.")
        })

    }

如有任何帮助,我们将不胜感激....

如果您不打算使用 CABasicAnimationCAAnimation 的其他变体,最好的方法是递归执行。例如:

func Blink(){
        blinker.alpha = 0.0
        UIButton.animateWithDuration(1, animations: {
            self.blinker.alpha = 1.0
            }, completion: {
                (value: Bool) in
                println(">>> Animation done.")
                Blink()
        })

    }

这样当动画完成时,您将再次调用它...

更新

请参考正确的处理方式。此解决方案可能会导致堆栈溢出问题。

虽然以上答案可行,但您可以使用 .autoreverse|.repeat 选项。那将是一种更清洁的方式。

确保您的 ViewController.

有一个与插座相连的按钮

将此添加到 Viewcontroller 的 ViewDidLoad 中:

    timer = NSTimer(timeInterval: 1.0, target: self, selector: "blink", userInfo: nil, repeats: true)
    NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

然后做一个函数

func blink () {
        if blinkStatus == false {
            blinkingButton.tintColor = UIColor.lightGrayColor()
            blinkStatus = true
        } else {
            blinkingButton.tintColor = UIColor.blackColor()
            blinkStatus = false
        }
    }

这应该可以解决问题。我试过了,它奏效了。它基于:https://www.weheartswift.com/nstimer-in-swift/

好吧有点神秘,我承认有点基于 Glenn's 答案,但这是在 iOS 13、swift 5.

上测试的另一个答案
var blinkStatus:Bool?

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in
        if self.blinkStatus != nil {
          self.button.alpha = self.blinkStatus! ? 1:0
          self.blinkStatus = !self.blinkStatus!
        } else {
          self.button.alpha = 1
          timer.invalidate()
        }

设置 blinkStatus = false 开始闪烁,设置 blinkStatus = nil 停止闪烁。显然按钮需要是你想要闪烁的 UIButton 距离。

我是这样做的:

var flashDuration: TimeInterval = 0.75

override func viewDidLoad() {
    super.viewDidLoad()

    startFlashingButton()
}

func startFlashingButton() {

    flashingButton.alpha = 0.0

    UIButton.animate(withDuration: flashDuration, animations: {

        self.flashingButton.alpha = 1.0

        }, completion: { (_) in

            self.repeatFlash()
    })
}

func repeatFlash() {

    UIButton.animate(withDuration: self.flashDuration, animations: {

        self.flashingButton.alpha = 0.0

        }, completion: { (_) in

            if self.flashDuration == 0.75 {

                self.startFlashingButton()
            }
    })
}

这会一直持续到应用程序被终止,但如果您想手动停止它,您只需更改 flashDuration = 0.0

func someReasonToStopFlash() {

    flashDuration = 0.0
}

并重新开始:

func yourReasonToStartItAgain() {

    flashDuration = 0.75
    startFlashingButton()
}
extension UIImageView{
    func startBlink1() {
        UIView.animate(withDuration: 0.8,
                       delay:0.1,
                       options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat],
                       animations: { self.alpha = 0.1 },
                       completion: { [self]
                        (value: Bool) in
                        //println(">>> Animation done.")
                        startBlink2()
                       })
    }

    func startBlink2() {
        UIView.animate(withDuration: 0.8,
                       delay:0.1,
                       options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat],
                       animations: { self.alpha = 1.0 },
                       completion: { [self]
                        (value: Bool) in
                        startBlink1()
                       })
    }
    
    func stopBlink() {
        layer.removeAllAnimations()
        alpha = 1
    }
}