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
Why Use an Extra Let Statement Here
How to Switch to Swift 4.0 in Xcode 9.3
Swift: Differencebetween a Typealias and an Associatedtype with a Value in a Protocol
Textfield in Swiftui Loses Focus When I Enter a Character
Why Can't I Use Self in a Func Swift
Google Sign-In via Firebase: Gidsignindelegate Does Not Conform to Viewcontroller
Why Is Deinit Not Called Until Uiview Is Added to Parent Again
How to Change Its Own Button Image on Tap in Swift
Swiftui Multiline Text Background Color
Resulting Mtltexture Lighter Than Cgimage
How to Repeat Animation Forever in Swift (Huge_Valf)
Behaviour of Protocols with Self
Xcframework Issue, a Library with the Identifier "Ios-Armv7_Arm64" Already Exists
How to Properly Calculate 1 Second with Deltatime in Swift
Why Specializing a Generic Function Explicitly Is Not Allowed
Swift: Convert String to Hex Color Code
Xcode Generated Coredata Files Cannot Be Processed by Copy Bundle Resources Build Phase