ios - 使按钮连续动画,可用 - Swift

标签 ios swift

我希望我的按钮持续动画直到用户触摸它,这是代码

func animate() {
    UIView.animateWithDuration(1, animations: { () -> Void in
        self.animation.transform = CGAffineTransformMakeScale(0.8,0.8)
    })
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        self.animation.transform = CGAffineTransformMakeScale(1,1)
    })
}

使用 NSTimer

timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("animate"), userInfo: nil, repeats: true)

动画效果很好,但我找不到让它可点击的方法。

最佳答案

您需要使用一种较长形式的 animateWithDuration,它采用选项参数并指定 UIViewAnimationOptionAllowUserInteraction 选项。像这样:

UIView.animateWithDuration(1, 
  delay: 0,
  options: .AllowUserInteraction,
  animations: 
  { () -> Void in
    self.animation.transform = CGAffineTransformMakeScale(0.8,0.8)
  }
  completion: nil
  );

(我的 Swift 有点生疏,但无论如何就是这个想法。)

关于ios - 使按钮连续动画,可用 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34975461/

相关文章:

swift - 在 Swift 中传播参数验证错误的最佳方法是什么?

swift - 使用 Sprite Kit 播放背景音乐

ios - 简化的屏幕截图 : record video of only what appears within the layers of a UIView?

objective-c - iPad应用程序横向模式下的坐标对应于纵向模式

ios - 从 1 表 w.r.t 表 2 过滤数据的 Realm 子查询

ios - Swift:将一个对象数组和另一个对象数组保存到 NSUserDefaults 中

ios - 为什么我的标签在从 UITextField 更新 UILabel 时留下了 1 个字符?

ios - Swift 4 旋转回跳

macos - 有没有办法在 Swift 中以编程方式设置 NSCollectionView?

swift - 来自 Codility GenomicRangeQuery in Swift 4.2 的这个前缀 Sum Coding Challenge 的解释