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

时间:2015-09-23 07:06:12

标签: swift ios8 xcode6.4

在我的故事板中,我有一个按钮,我想无限次地闪烁它直到程序被杀死。这就是我到目前为止所做的,这段代码使按钮只能动画一次。

@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.")
        })

    }

任何帮助都将不胜感激....

6 个答案:

答案 0 :(得分:2)

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

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

    }

这样一来,当动画结束时,你再次呼叫它,再次,再次......

更新

请参阅Glenn's answer以了解正确处理方法。此解决方案可能会导致堆栈溢出问题。

答案 1 :(得分:2)

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

答案 2 :(得分:2)

确保您的按钮与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/

答案 3 :(得分:0)

好吧,有点神秘,基于我承认的Glenn's答案,但这是在iOS 13,Swift 5上测试的另一个答案。

class _MyhomepageState extends State<Myhomepage> {
  int counter = 0;

  Color _getCounterColor() {
    if (counter > 0) return Colors.blue;
    else if (counter < 0) return Colors.red;
    else return Colors.green;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        centerTitle: true,
      ),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          backgroundColor: Colors.grey[850],
          onPressed: () {
            setState(() {
              counter++;
            });
          }),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text(
                "Increase",
              ),
              color: Colors.green,
              onPressed: () {
                setState(() {
                  counter++;
                });
              },
            ),
            Text("The count of press button:"),
            Text(
              "$counter",
              style: Theme.of(context).textTheme.display2.copyWith(color: _getCounterColor()),
            ),
            RaisedButton(
              child: Text(
                "Decrease",
              ),
              color: Colors.red,
              onPressed: () {
                setState(() {
                  counter--;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

设置blinkStatus = false以使其开始闪烁,而blinkStatus = nil以使其停止。显然,按钮必须是您要闪烁的UIButton距离。

答案 4 :(得分:0)

这是我的方法:

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()
}

答案 5 :(得分:0)

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
    }
}