43

我正在尝试使用 CABasicAnimation 自动重复我的图像旋转动画。我试图在网上搜索如何设置此类属性,但无法找到。CA动画真的没有这样的属性吗?我知道您可以为 repeatCount 属性设置一些巨大的值(此处),但是,嘿,为什么 UIView animateWithDuration 有一个选项 UIViewAnimationOptionRepeat 以及为它硬编码的值是什么?

4

2 回答 2

142

不,这是您根据文档应该这样做的方式。

将此属性设置为HUGE_VALF将导致动画永远重复。


斯威夫特更新:

HUGE_VALF 没有暴露给 Swift。但是,我从这个页面的理解是 HUGE_VALF 的目的是无穷大(实际上,INFINITY被定义为HUGE_VALF)。由于 Swift 的FloatingPointType协议提供了一个static var infinity,你可以简单地写

myAnimation.repeatCount = .infinity
于 2011-08-16T17:51:12.667 回答
7

对于 swift 3.0 及以上版本

              let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
          rotationAnimation.fromValue = 0
          rotationAnimation.toValue = CGFloat.pi * 2
          rotationAnimation.duration = 1
          rotationAnimation.repeatCount = .infinity
          holderView.btnRefresh.layer.add(rotationAnimation, forKey: "spinAnimation")
于 2017-04-19T10:41:00.167 回答