Lightweight value interpolation with easing functions for Apple platforms.
ValueAnimator transitions a value from from to to over time and reports each frame through a callback.
You can animate a single property or multiple properties simultaneously.
letanimator=ValueAnimator.animate("rotation",
from:0,
to:360,
duration:1.0,
easing:EaseCircular.easeIn(),
onChanged:{ property, value inprint("property \(property): \(value.value)")})
animator.resume()letanimator=ValueAnimator.animate(
props:["h","w"],
from:[20,30],
to:[5,150],
duration:1.4,
easing:EaseSine.easeInOut(),
onChanged:{ property, value inif property =="h"{letwidth=self.rect1.bounds.width
self.rect1.frame =CGRect(x:24, y:140, width: width, height: value.cg)}else{letheight=self.rect1.bounds.height
self.rect1.frame =CGRect(x:24, y:140, width: value.cg, height: height)}},
option:ValueAnimator.OptionBuilder().setYoyo(true).setRepeatCount(2).build())
animator.resume()letanimator=ValueAnimator.animate(
props:["alpha"],
from:[0.0],
to:[1.0],
duration:0.5,
onEnd:{print("animation completed")})
animator.resume()By default, changeCallback and endCallback are dispatched on the main thread.
If you need callbacks on the animation engine thread, set callbackOnMainThread = false.
letanimator=ValueAnimator.animate("opacity",
from:0,
to:1,
duration:1.0,
easing:EaseCircular.easeIn(),
onChanged:{ _, _ in
// called on ValueAnimator engine thread
},)
animator.callbackOnMainThread =false
animator.resume()ValueAnimator.finishAll()ValueAnimator.disposeAll()Task{letisAnimating=awaitValueAnimator.hasAnimation("opacity")print(isAnimating)}To run the example project, clone the repo, and run open ValueAnimator.xcworkspace. You can see ValueAnimatorExample project.
- iOS 13+
- tvOS 13+
- macOS 10.15+
- watchOS 6+
- Open Xcode
- Go to File > Add Package Dependencies...
- Paste this repository URL:
https://github.com/brownsoo/ValueAnimator
- Select the
ValueAnimatorpackage and add it to your target.
Warning
CocoaPods support is now legacy and no longer actively updated. Please use Swift Package Manager for the latest updates.
Previously, you could add:
pod'ValueAnimator'brownsoo, @medium
ValueAnimator is available under the MIT license. See the LICENSE file for more info.
