【问题标题】:CABasicAnimation unlimited repeat without HUGE_VALF?CABasicAnimation 无 HUGE_VALF 无限重复?
【发布时间】:2011-10-28 07:05:56
【问题描述】:

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

【问题讨论】:

  • 值为1e50,在math.h
  • 在 XCode 中 ⌘-click 去定义一些东西。

标签: iphone objective-c animation repeat cabasicanimation


【解决方案1】:

不,根据the documentation,这是你应该这样做的方式。

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


Swift 更新:

HUGE_VALF 不会暴露给 Swift。但是,我对this page 的理解是,HUGE_VALF 旨在为无穷大(实际上,INFINITY 被定义为HUGE_VALF)。由于 Swift 的FloatingPointType 协议提供了static var infinity,所以你可以简单地写

myAnimation.repeatCount = .infinity

【讨论】:

  • 在 iOS 7 及更高版本中,它会在一定间隔后停止。但不确定是什么原因。
  • Float.infinity 在 Swift 中。引用的文档提到了HUGE_VALF,但没有说明如何在 Swift 中表达。
【解决方案2】:

适用于 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")

【讨论】:

    猜你喜欢
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2011-01-07
    • 2017-08-23
    相关资源
    最近更新 更多