点击动画延迟手势

Tap gesture delay with animation

我有几个从点击手势识别器调用的 UIView 扩展。为什么背景颜色扩展必须先完成动画才能识别其他点击? alpha 扩展加上我用来设置视图位置动画的另一个扩展工作得很好——这意味着我可以按我想要的速度快速点击,它们会立即响应。

@IBAction func taps(sender: UITapGestureRecognizer) {
   if !isAnimating {
      currentLabel.fadeOut()
      backgroundColor.fadeBackgroundOut()
      isAnimating = true   
   } else {
      currentLabel.fadeIn()
      backgroundColor.fadeBackgroundIn(curBackground)
      isAnimating = false
   }
}

func fadeOut() {
   UIView.animateWithDuration(0.5,
      delay: 0.0,
      options: UIViewAnimationOptions.CurveEaseOut,
      animations: { self.alpha = 0.0 },
      completion: nil)
}

func fadeBackgroundIn(aColor: UIColor) {
   UIView.animateWithDuration(1.0,
      delay: 0,
      options: UIViewAnimationOptions.CurveEaseIn,
      animations: { self.backgroundColor = aColor },
      completion: nil)
}

这很简单,如果您更改 "self" 的 alpha,即视图为零,您必须了解它会停止接收触摸事件。

什么是好的解决方案是 "self" 即视图的背景颜色透明,而不是改变 "self" 即视图的 alpha 值,这样你的视图将不可见并且你得到手势识别器事件。

替代解决方案是,在视图上添加透明按钮,处理按钮点击操作。

将您的扩展程序更新为:

func fadeOut() {
   UIView.animateWithDuration(0.5,
      delay: 0.0,
      options: [UIViewAnimationOptions.CurveEaseIn , UIViewAnimationOptions.AllowUserInteraction],
      animations: { self.alpha = 0.0 },
      completion: nil)
}

func fadeBackgroundIn(aColor: UIColor) {
    UIView.animateWithDuration(1.0,
        delay: 0,
        options: [UIViewAnimationOptions.CurveEaseIn , UIViewAnimationOptions.AllowUserInteraction],
        animations: { self.backgroundColor = aColor },
        completion: nil)
}