如何重复fadeIn(持续时间:NSTimeInterval方法无限期?

时间:2015-10-28 04:42:51

标签: swift

我有这个扩展名,我正在使用它作为一个闪光效果。它消失了,只有一次。

  1. 我该如何重复?
  2. 如何在用户输入之前重复此操作? IBAction低于扩展名。

    extension UIView {
    func fadeIn(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 0.0, completion: ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
    UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.alpha = 1.0
        }, completion: completion)  }
    
    func fadeOut(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 0.0, completion: (Bool) -> Void = {(finished: Bool) -> Void in}) {
    UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.alpha = 0.0
        }, completion: completion)
    }
    }
    
  3. 单独IBAction

    @IBAction func SendAge(sender: AnyObject) {
    
    
        let newAge = String(inputField.text!)
    
        if ((inputField.text?.isEmpty) != false) {
    
            self.sentence.blink()
            self.sentence.text = "Enter a number!"
            self.sentence.textColor = UIColor.redColor()
        }
        else {
            sentence.text = "Oh, you're \(newAge)"
            return()
        }
    
    }
    

2 个答案:

答案 0 :(得分:2)

这是一种非常简单的方法:

extension UIView {

    func blink(duration: NSTimeInterval = 1.0) {
        self.alpha = 1
        UIView.animateWithDuration(duration, delay: 0,
            options: [.CurveEaseIn, .Autoreverse, .Repeat], animations: {
                self.alpha = 0
            }, completion: nil)
    }

}

要取消它,只需将视图的alpha设置为1。

答案 1 :(得分:0)

您应该使用NSTimer scheduledTimerWithTimeInterval(interval: NSTimeInterval, target:, selector, userInfo:, repeats: bool)上可以设置为true的方法,只要计时器正在运行,这个方法就会运行。