带有快速记录按钮的flash动画问题

时间:2015-07-08 09:41:24

标签: swift animation uibutton

我正在尝试创建一个记录按钮,所以当用户点击它并开始录制时我想在该按钮上应用flash动画并找到THIS帖子。我将该代码转换为swift但它不起作用,这是我的快速代码:

var buttonFlashing = false
@IBAction func record(sender: AnyObject) {

    println("Button Tapped")
    if !buttonFlashing {
        startFlashingbutton()
    } else {
        stopFlashingbutton()
    }
}

func startFlashingbutton() {

    buttonFlashing = true
    recordButton.alpha = 1

    UIView.animateWithDuration(0.5 , delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction, animations: {

        self.recordButton.alpha = 0

        }, completion: {Bool in
    })
}

func stopFlashingbutton() {

    buttonFlashing = false

    UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.BeginFromCurrentState, animations: {

        self.recordButton.alpha = 1

        }, completion: {Bool in
    })
}

当我按下按钮并进行动画时第一次打印"Button Tapped"但是当我再次按下按钮时"Button Tapped"没有打印到控制台。我无法停止动画。

我找不到这里的错误。

1 个答案:

答案 0 :(得分:3)

您的代码存在的问题是alpha设置为零。因此,当alpha为零或被隐藏时,OS会禁用交互。动画应用于按钮的图层,并且已将alpha设置为最终值。因此,您可以将alpha值设置为低至0.1,以便启用用户交互,这应该没问题。

您可以执行以下任一选项,

将alpha设置为0.1

UIView.animateWithDuration(0.5 , delay: 0.0, options: 
  [
    UIViewAnimationOptions.CurveEaseInOut, 
    UIViewAnimationOptions.Autoreverse,
    UIViewAnimationOptions.Repeat, 
    UIViewAnimationOptions.AllowUserInteraction
  ], 
  animations: {
    self.recordButton.alpha = 0.1        
  }, completion: {Bool in
})

或者,然后将图层的颜色设置为clearColor,这在您的情况下似乎更合理。

UIView.animateWithDuration(0.5 , delay: 0.0, options: 
  [
    UIViewAnimationOptions.CurveEaseInOut, 
    UIViewAnimationOptions.Autoreverse,
    UIViewAnimationOptions.Repeat, 
    UIViewAnimationOptions.AllowUserInteraction
  ], 
  animations: {
    self.recordButton.layer.backgroundColor = UIColor.clearColor().CGColor       
  }, completion: {Bool in
})