Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit 801c061

Browse files
author
Adam Bell
authored
Merge pull request #357 from knickmack/feature/324-readme-swift-usage
Add Swift usage examples to README.md
2 parents a15dd84 + 8897429 commit 801c061

File tree

1 file changed

+141
-1
lines changed

1 file changed

+141
-1
lines changed

README.md

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![pop](https://github.com/facebook/pop/blob/master/Images/pop.gif?raw=true)
22

3-
Pop is an extensible animation engine for iOS, tvOS, and OS X. In addition to basic static animations, it supports spring and decay dynamic animations, making it useful for building realistic, physics-based interactions. The API allows quick integration with existing Objective-C codebases and enables the animation of any property on any object. It's a mature and well-tested framework that drives all the animations and transitions in [Paper](http://www.facebook.com/paper).
3+
Pop is an extensible animation engine for iOS, tvOS, and OS X. In addition to basic static animations, it supports spring and decay dynamic animations, making it useful for building realistic, physics-based interactions. The API allows quick integration with existing Objective-C or Swift codebases and enables the animation of any property on any object. It's a mature and well-tested framework that drives all the animations and transitions in [Paper](http://www.facebook.com/paper).
44

55
[![Build Status](https://travis-ci.org/facebook/pop.svg)](https://travis-ci.org/facebook/pop)
66

@@ -34,6 +34,8 @@ Alternatively, you can add the project to your workspace and adopt the provided
3434

3535
Pop adopts the Core Animation explicit animation programming model. Use by including the following import:
3636

37+
#### Objective-C
38+
3739
```objective-c
3840
#import <pop/POP.h>
3941
```
@@ -44,24 +46,50 @@ or if you're using the embedded framework:
4446
@import pop;
4547
```
4648

49+
#### Swift
50+
51+
```swift
52+
import pop
53+
```
54+
4755
### Start, Stop & Update
4856

4957
To start an animation, add it to the object you wish to animate:
5058

59+
#### Objective-C
60+
5161
```objective-c
5262
POPSpringAnimation *anim = [POPSpringAnimation animation];
5363
...
5464
[layer pop_addAnimation:anim forKey:@"myKey"];
5565
```
5666
67+
#### Swift
68+
69+
```swift
70+
let anim = POPSpringAnimation()
71+
...
72+
layer.pop_add(anim, forKey: "myKey")
73+
```
74+
5775
To stop an animation, remove it from the object referencing the key specified on start:
5876

77+
#### Objective-C
78+
5979
```objective-c
6080
[layer pop_removeAnimationForKey:@"myKey"];
6181
```
6282
83+
#### Swift
84+
85+
```swift
86+
layer.pop_removeAnimation(forKey: "myKey")
87+
```
88+
6389
The key can also be used to query for the existence of an animation. Updating the toValue of a running animation can provide the most seamless way to change course:
6490

91+
#### Objective-C
92+
6593
```objective-c
6694
anim = [layer pop_animationForKey:@"myKey"];
6795
if (anim) {
@@ -73,6 +101,18 @@ if (anim) {
73101
}
74102
```
75103
104+
#### Swift
105+
106+
```swift
107+
if let anim = layer.pop_animation(forKey: "myKey") as? POPSpringAnimation {
108+
/* update to value to new destination */
109+
anim.toValue = 42.0
110+
} else {
111+
/* create and start a new animation */
112+
....
113+
}
114+
```
115+
76116
While a layer was used in the above examples, the Pop interface is implemented as a category addition on NSObject. Any NSObject or subclass can be animated.
77117

78118
### Types
@@ -81,41 +121,92 @@ There are four concrete animation types: spring, decay, basic and custom.
81121

82122
Spring animations can be used to give objects a delightful bounce. In this example, we use a spring animation to animate a layer's bounds from its current value to (0, 0, 400, 400):
83123

124+
#### Objective-C
125+
84126
```objective-c
85127
POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];
86128
anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 400, 400)];
87129
[layer pop_addAnimation:anim forKey:@"size"];
88130
```
131+
132+
#### Swift
133+
134+
```swift
135+
if let anim = POPSpringAnimation(propertyNamed: kPOPLayerBounds) {
136+
anim.toValue = NSValue(cgRect: CGRect(x: 0, y: 0, width: 400, height: 400))
137+
layer.pop_add(anim, forKey: "size")
138+
}
139+
```
140+
89141
Decay animations can be used to gradually slow an object to a halt. In this example, we decay a layer's positionX from it's current value and velocity 1000pts per second:
90142

143+
#### Objective-C
144+
91145
```objective-c
92146
POPDecayAnimation *anim = [POPDecayAnimation animationWithPropertyNamed:kPOPLayerPositionX];
93147
anim.velocity = @(1000.);
94148
[layer pop_addAnimation:anim forKey:@"slide"];
95149
```
96150
151+
#### Swift
152+
153+
```swift
154+
if let anim = POPDecayAnimation(propertyNamed: kPOPLayerPositionX) {
155+
anim.velocity = 1000.0
156+
layer.pop_add(anim, forKey: "slide")
157+
}
158+
```
159+
97160
Basic animations can be used to interpolate values over a specified time period. To use an ease-in ease-out animation to animate a view's alpha from 0.0 to 1.0 over the default duration:
161+
162+
#### Objective-C
163+
98164
```objective-c
99165
POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
100166
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
101167
anim.fromValue = @(0.0);
102168
anim.toValue = @(1.0);
103169
[view pop_addAnimation:anim forKey:@"fade"];
104170
```
171+
172+
#### Swift
173+
174+
```swift
175+
if let anim = POPBasicAnimation(propertyNamed: kPOPViewAlpha) {
176+
anim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
177+
anim.fromValue = 0.0
178+
anim.toValue = 1.0
179+
view.pop_add(anim, forKey: "fade")
180+
}
181+
```
182+
105183
`POPCustomAnimation` makes creating custom animations and transitions easier by handling CADisplayLink and associated time-step management. See header for more details.
106184

107185

108186
### Properties
109187

110188
The property animated is specified by the `POPAnimatableProperty` class. In this example we create a spring animation and explicitly set the animatable property corresponding to `-[CALayer bounds]`:
111189

190+
#### Objective-C
191+
112192
```objective-c
113193
POPSpringAnimation *anim = [POPSpringAnimation animation];
114194
anim.property = [POPAnimatableProperty propertyWithName:kPOPLayerBounds];
115195
```
116196
197+
#### Swift
198+
199+
```swift
200+
let anim = POPSpringAnimation()
201+
if let property = POPAnimatableProperty.property(withName: kPOPLayerBounds) as? POPAnimatableProperty {
202+
anim.property = property
203+
}
204+
```
205+
117206
The framework provides many common layer and view animatable properties out of box. You can animate a custom property by creating a new instance of the class. In this example, we declare a custom volume property:
118207

208+
#### Objective-C
209+
119210
```objective-c
120211
prop = [POPAnimatableProperty propertyWithName:@"com.foo.radio.volume" initializer:^(POPMutableAnimatableProperty *prop) {
121212
// read value
@@ -133,6 +224,36 @@ prop = [POPAnimatableProperty propertyWithName:@"com.foo.radio.volume" initializ
133224
anim.property = prop;
134225
```
135226
227+
#### Swift
228+
229+
```swift
230+
if let prop = POPAnimatableProperty.property(withName: "com.foo.radio.volume", initializer: { prop in
231+
guard let prop = prop else {
232+
return
233+
}
234+
// read value
235+
prop.readBlock = { obj, values in
236+
guard let obj = obj as? Volumeable, let values = values else {
237+
return
238+
}
239+
240+
values[0] = obj.volume
241+
}
242+
// write value
243+
prop.writeBlock = { obj, values in
244+
guard var obj = obj as? Volumeable, let values = values else {
245+
return
246+
}
247+
248+
obj.volume = values[0]
249+
}
250+
// dynamics threshold
251+
prop.threshold = 0.01
252+
}) as? POPAnimatableProperty {
253+
anim.property = prop
254+
}
255+
```
256+
136257
For a complete listing of provided animatable properties, as well more information on declaring custom properties see `POPAnimatableProperty.h`.
137258

138259

@@ -142,18 +263,37 @@ Here are a few tips when debugging. Pop obeys the Simulator's Toggle Slow Animat
142263

143264
Consider naming your animations. This will allow you to more easily identify them when referencing them, either via logging or in the debugger:
144265

266+
#### Objective-C
267+
145268
```objective-c
146269
anim.name = @"springOpen";
147270
```
148271

272+
#### Swift
273+
274+
```swift
275+
anim.name = "springOpen"
276+
```
277+
149278
Each animation comes with an associated tracer. The tracer allows you to record all animation-related events, in a fast and efficient manner, allowing you to query and analyze them after animation completion. The below example starts the tracer and configures it to log all events on animation completion:
150279

280+
#### Objective-C
281+
151282
```objective-c
152283
POPAnimationTracer *tracer = anim.tracer;
153284
tracer.shouldLogAndResetOnCompletion = YES;
154285
[tracer start];
155286
```
156287

288+
#### Swift
289+
290+
```swift
291+
if let tracer = anim.tracer {
292+
tracer.shouldLogAndResetOnCompletion = true
293+
tracer.start()
294+
}
295+
```
296+
157297
See `POPAnimationTracer.h` for more details.
158298

159299
## Testing

0 commit comments

Comments
 (0)