制作一个连续动画的按钮,可用 - Swift

时间:2016-01-24 11:56:45

标签: 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)

动画效果很好,但我无法找到使其可点击的方法。

1 个答案:

答案 0 :(得分:1)

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

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

(我的斯威夫特有点生疏,但无论如何这都是想法。)