Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit d53f753

Browse files
authored
Add drop in replacement APIs section to the readme (#105)
1 parent af4b7e6 commit d53f753

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

README.md

+113
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,119 @@ Note: any animatable property can also be animated with MotionAnimator's explici
4747

4848
> Is a property missing from this list? [We welcome pull requests](https://github.com/material-motion/motion-animator-objc/edit/develop/src/MDMAnimatableKeyPaths.h)!
4949
50+
## MotionAnimator: a drop-in replacement
51+
52+
UIView's implicit animation APIs are also available on the MotionAnimator:
53+
54+
```swift
55+
// Animating implicitly with UIView APIs
56+
UIView.animate(withDuration: 1.0, animations: {
57+
view.alpha = 0.5
58+
})
59+
60+
// Equivalent MotionAnimator API
61+
MotionAnimator.animate(withDuration: 1.0, animations: {
62+
view.alpha = 0.5
63+
})
64+
```
65+
66+
But the MotionAnimator allows you to animate more properties — and on more iOS versions:
67+
68+
```swift
69+
UIView.animate(withDuration: 1.0, animations: {
70+
view.layer.cornerRadius = 10 // Only works on iOS 11 and up
71+
})
72+
73+
MotionAnimator.animate(withDuration: 1.0, animations: {
74+
view.layer.cornerRadius = 10 // Works on iOS 8 and up
75+
})
76+
```
77+
78+
MotionAnimator makes use of the [MotionInterchange](https://github.com/material-motion/motion-interchange-objc), a standardized format for representing animation traits. This makes it possible to tweak the traits of an animation without rewriting the code that ultimately creates the animation, useful for building tweaking tools and making motion "stylesheets".
79+
80+
```swift
81+
// Want to change a trait of your animation? You'll need to use a different function altogether
82+
// to do so:
83+
UIView.animate(withDuration: 1.0, animations: {
84+
view.alpha = 0.5
85+
})
86+
UIView.animate(withDuration: 1.0, delay: 0.5, options: [], animations: {
87+
view.alpha = 0.5
88+
}, completion: nil)
89+
90+
// But with the MotionInterchange, you can create and manipulate the traits of an animation
91+
// separately from its execution.
92+
let traits = MDMAnimationTraits(duration: 1.0)
93+
traits.delay = 0.5
94+
95+
let animator = MotionAnimator()
96+
animator.animate(with: traits, animations: {
97+
view.alpha = 0.5
98+
})
99+
```
100+
101+
The MotionAnimator can also be used to replace explicit Core Animation code with additive explicit animations:
102+
103+
```swift
104+
let from = 0
105+
let to = 10
106+
// Animating expicitly with Core Animation APIs
107+
let animation = CABasicAnimation(keyPath: "cornerRadius")
108+
animation.fromValue = (from - to)
109+
animation.toValue = 0
110+
animation.isAdditive = true
111+
animation.duration = 1.0
112+
view.layer.add(animation, forKey: animation.keyPath)
113+
view.layer.cornerRadius = to
114+
115+
// Equivalent implicit MotionAnimator API. cornerRadius will be animated additively by default.
116+
view.layer.cornerRadius = 0
117+
MotionAnimator.animate(withDuration: 1, animations: {
118+
view.layer.cornerRadius = 10
119+
})
120+
121+
// Equivalent explicit MotionAnimator API
122+
// Note that this API will also set the final animation value to the layer's model layer, similar
123+
// to how implicit animations work, and unlike the explicit pure Core Animation implementation
124+
// above.
125+
let animator = MotionAnimator()
126+
animator.animate(with: MDMAnimationTraits(duration: 1.0),
127+
between: [0, 10],
128+
layer: view.layer,
129+
keyPath: .cornerRadius)
130+
```
131+
132+
Springs on iOS require an initial velocity that's normalized by the displacement of the animation. MotionAnimator calculates this for you so that you can directly provide gesture recognizer velocity values:
133+
134+
```swift
135+
// Common variables
136+
let gestureYVelocity = gestureRecognizer.velocity(in: someContainerView).y
137+
let destinationY = 75
138+
139+
// Animating springs implicitly with UIView APIs
140+
let displacement = destinationY - view.position.y
141+
UIView.animate(withDuration: 1.0,
142+
delay: 0,
143+
usingSpringWithDamping: 1.0,
144+
initialSpringVelocity: gestureYVelocity / displacement,
145+
options: [],
146+
animations: {
147+
view.layer.position = CGPoint(x: view.position.x, y: destinationY)
148+
},
149+
completion: nil)
150+
151+
// Equivalent MotionAnimator API
152+
let animator = MotionAnimator()
153+
let traits = MDMAnimationTraits(duration: 1.0)
154+
traits.timingCurve = MDMSpringTimingCurveGenerator(duration: traits.duration,
155+
dampingRatio: 1.0,
156+
initialVelocity: gestureYVelocity)
157+
animator.animate(with: traits,
158+
between: [view.layer.position.y, destinationY],
159+
layer: view.layer,
160+
keyPath: .y)
161+
```
162+
50163
## Example apps/unit tests
51164

52165
Check out a local copy of the repo to access the Catalog application by running the following

0 commit comments

Comments
 (0)