带有录制按钮swift的flash动画问题

带有录制按钮swift的flash动画问题,swift,animation,uibutton,Swift,Animation,Uibutton,我试图做一个记录按钮,所以当用户点击它,它的开始记录,我想应用flash动画的按钮,我找到了帖子。我将该代码转换为swift,但它不起作用,这是我的swift代码: var buttonFlashing = false @IBAction func record(sender: AnyObject) { println("Button Tapped") if !buttonFlashing { startFlashingbutton() } else {

我试图做一个记录按钮,所以当用户点击它,它的开始记录,我想应用flash动画的按钮,我找到了帖子。我将该代码转换为swift,但它不起作用,这是我的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
    })
}
当我第一次按下按钮并出现动画时,它正在打印
“按钮点击”
,但当我再次按下按钮时
“按钮点击”
不会打印到控制台中。我不能停止动画


我找不到这里有什么问题。

您的代码的问题是alpha设置为零。因此,当alpha为零或隐藏时,操作系统将禁用交互。动画将应用于按钮的层,并且已将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
})