How to Repeat Animation Forever in Swift (Huge_Valf)

how to repeat animation forever in Swift (HUGE_VALF)?

Set the repeatCount to Float.infinity. This compiles and works.

In all probability, HUGE_VALF was a legacy value in any case.

Still, it's a bit of a surprise that these numeric constant names are not seen by Swift. I did try importing <stdlib.h> in the bridging header but it didn't help.


But please see now Martin R's answer. Since the time when I posted my answer, Apple has stated their preferred answer explicitly: use .greatestFiniteMagnitude. It is almost the same thing as .infinity but not quite!

CABasicAnimation unlimited repeat without HUGE_VALF?

No, this is the way you're supposed to do it according to the documentation.

Setting this property to HUGE_VALF will cause the animation to repeat forever.


Update for Swift:

HUGE_VALF is not exposed to Swift. However, my understanding from this page is that HUGE_VALF is intended to be infinity (in fact, INFINITY is defined as HUGE_VALF). Since Swift's FloatingPointType protocol provides a static var infinity, you can simply write

myAnimation.repeatCount = .infinity

UIView.animateWithDuration swift loop animation

No need to do the completion block approach, just use the animation options argument:

updated for Swift 3.0

UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse], animations: {

coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)

}, completion: nil)

If for any reason you want to stop the animation later, just use:

coloredSquare.layer.removeAllAnimations()

How to repeat same animation while working with CABasicAnimation

animation.repeatCount = .greatestFiniteMagnitude will for all practical purposes repeat it forever.

Programming an animation to loop forever in Swift

First, set the repeatCount to HUGE_VALF / greatestFiniteMagnitude:

Setting this property to HUGE_VALF will cause the animation to repeat forever.

Second, call LoginField.layer.removeAllAnimations() or LoginField.layer.removeAnimationForKey("position")when you want to stop the animation.

Edit: Apparently Swift does not know HUGE_VALF, setting the repeatCount to Float.infinity should work.

Repeating Action Continuously In SwiftUI

Animation.basic is deprecated. Basic animations are now named after their curve types: like linear, etc:

var foreverAnimation: Animation {
Animation.linear(duration: 0.3)
.repeatForever()
}

Source:
https://forums.swift.org/t/swiftui-animation-basic-duration-curve-deprecated/27076



Related Topics



Leave a reply



Submit