Slide 1

Slide 1 text

Building Better Transitions By eric allam

Slide 2

Slide 2 text

1986

Slide 3

Slide 3 text

Mimic Physical Reality in an interesting way

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Transitions are high stakes

Slide 6

Slide 6 text

principles of animation + iOS transition system

Slide 7

Slide 7 text

Stretch and Squash. Timing. Anticipation. Staging. Follow-through and Overlapping Action. Straight Ahead Action and Pose-to-Pose Action. Slow in and Slow out. Arcs. Exaggeration. Secondary Action. Appeal.

Slide 8

Slide 8 text

Timing Arcs & Appeal Staging

Slide 9

Slide 9 text

Timing

Slide 10

Slide 10 text

UIViewAnimationOptionCurve*

Slide 11

Slide 11 text

Timing is: The velocity speed of an action

Slide 12

Slide 12 text

Not too slow or too fast

Slide 13

Slide 13 text

Too fast leads to strobing

Slide 14

Slide 14 text

not just animateWithDuration:

Slide 15

Slide 15 text

Speed = duration / distance

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Default Transition 0.5 seconds Spring Animation usingSpringWithDamping:10 initialSpringVelocity:0 Full Screen

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Custom Modal Transition

Slide 21

Slide 21 text

- (NSTimeInterval)transitionDuration: (id)transitionContext - (void)animateTransition: (id)transitionContext

Slide 22

Slide 22 text

transitionDuration: Tells the transition system how long the transition should take

Slide 23

Slide 23 text

animateTransition: This is where you perform the transition's animation

Slide 24

Slide 24 text

UIViewControllerContextTransitioning Gives you access to both the "from" and "to" controllers, as well as the "container" view

Slide 25

Slide 25 text

UIView *fromView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view; UIView *toView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view; UIView *container = [transitionContext containerView];

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Add toView to containerView

Slide 28

Slide 28 text

Set toView's initial state

Slide 29

Slide 29 text

Animate to toView's final state

Slide 30

Slide 30 text

Complete Transition

Slide 31

Slide 31 text

[transitionContext completeTransition:YES];

Slide 32

Slide 32 text

Example

Slide 33

Slide 33 text

In animateTransition: toView.bounds = CGRectMake(0, 0, 280, 180); toView.center = CGPointMake(container.center.x, -90); [container addSubview:toView];

Slide 34

Slide 34 text

[UIView animateWithDuration:duration animations:^{ toView.center = container.center; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }];

Slide 35

Slide 35 text

200ms <= duration <= 500 ms

Slide 36

Slide 36 text

Slow in and Slow Out

Slide 37

Slide 37 text

Ease In and Ease Out

Slide 38

Slide 38 text

Follow through

Slide 39

Slide 39 text

Arcs

Slide 40

Slide 40 text

Appeal

Slide 41

Slide 41 text

Enter the 3rd Dimension

Slide 42

Slide 42 text

CATransform3D

Slide 43

Slide 43 text

CGAffineTransform + z axis

Slide 44

Slide 44 text

transform.m34 = - 1.0 / 500.0;

Slide 45

Slide 45 text

In animateTransition: UIView *container = [transitionContext containerView]; CATransform3D perspective = CATransform3DIdentity; perspective.m34 = - 1.0 / 500.0; container.layer.sublayerTransform = perspective;

Slide 46

Slide 46 text

Rotate toView around x-axis toView.layer.anchorPoint = CGPointMake(0.5, 0); toView.layer.position = adjustedPosition; toView.layer.transform = CATransform3DMakeRotation(M_PI_4, 1.0, 0, 0);

Slide 47

Slide 47 text

[UIView animateWithDuration:duration animations:^{ toView.layer.transform = CATransform3DIdentity; toView.layer.position = centerPosition; } completion:^(BOOL finished) { container.layer.sublayerTransform = CATransform3DIdentity; toView.layer.transform = CATransform3DIdentity; toView.layer.anchorPoint = CGPointMake(0.5, 0.5); toView.layer.position = container.center; [transitionContext completeTransition:YES]; }];

Slide 48

Slide 48 text

Guidelines for 3D transitions · Reset anchor points, transform, and positions on completion · Add perspective using the container view's sublayerTransform · Spring animations generally don't look right

Slide 49

Slide 49 text

Staging Present main idea clearly

Slide 50

Slide 50 text

Presenting self.startingPoint = self.sharedView.center; self.startingBounds = self.sharedView.bounds; self.sharedView.center = [toView convertPoint:self.startingPoint fromView:fromView]; [toView addSubview:self.sharedView];

Slide 51

Slide 51 text

Animate [UIView animateWithDuration:duration animations:^{ toView.center = container.center; self.sharedView.center = CGPointMake(140, 90); self.sharedView.bounds = CGRectMake(0, 0, 100, 100); } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }];

Slide 52

Slide 52 text

Dismissal animation [UIView animateWithDuration:duration animations:^{ fromView.center = CGPointMake(container.center.x, -90); self.sharedView.center = [fromView convertPoint:self.startingPoint fromView:toView]; self.sharedView.bounds = self.startingBounds; }

Slide 53

Slide 53 text

Dismissal completion completion:^(BOOL finished) { self.sharedView.center = [toView convertPoint:self.sharedView.center fromView:fromView]; [toView addSubview:self.sharedView]; [transitionContext completeTransition:YES]; }];

Slide 54

Slide 54 text

1. Store off shared views center/ bounds

Slide 55

Slide 55 text

2. Convert to the presented view's coordinate space and add to hierarchy

Slide 56

Slide 56 text

3. Animate

Slide 57

Slide 57 text

4. On dismissal, convert back to presenting view's coordinate space and hierarchy

Slide 58

Slide 58 text

Bring your transitions to life

Slide 59

Slide 59 text

Anticipation

Slide 60

Slide 60 text

Overlapping Action

Slide 61

Slide 61 text

Stretch and Squash

Slide 62

Slide 62 text

Illusion of Life

Slide 63

Slide 63 text

Principles of Traditional Animation applied to 3D Computer animation

Slide 64

Slide 64 text

Fingers

Slide 65

Slide 65 text

Animated transitions are selfish jerks

Slide 66

Slide 66 text

[UIApplication beginIgnoringInteractionEvents]

Slide 67

Slide 67 text

Learn this one simple trick...

Slide 68

Slide 68 text

Turn animated transitions into interactive transitions

Slide 69

Slide 69 text

[UIApplication beginIgnoringInteractionEvents]

Slide 70

Slide 70 text

Take all the code in animateTransition:

Slide 71

Slide 71 text

And put it in startInteractiveTransition:

Slide 72

Slide 72 text

- (void)startInteractiveTransition: (id ) transitionContext;

Slide 73

Slide 73 text

Normal interactive transition: Gesture -> Animation

Slide 74

Slide 74 text

Interactive animated transition: Animation -> Gesture? - > Animation?

Slide 75

Slide 75 text

UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)]; [container addGestureRecognizer:gesture]; gesture.delegate = self;

Slide 76

Slide 76 text

Allow interaction [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ toView.center = container.center; };

Slide 77

Slide 77 text

UIViewAnimationOptionAllowUserInter action

Slide 78

Slide 78 text

Use the finished param completion:^(BOOL finished) { if (finished){ [gesture.view removeGestureRecognizer:gesture]; [transitionContext finishInteractiveTransition]; [transitionContext completeTransition:YES]; } }]

Slide 79

Slide 79 text

Cancel animations when pan begins CALayer *layer = toView.layer.presentationLayer; toView.center = layer.position; [toView.layer removeAllAnimations];

Slide 80

Slide 80 text

Move view as pan changes CGPoint translation = [gesture translationInView:container]; CGPoint viewCenter = toView.center; viewCenter.x += translation.x; viewCenter.y += translation.y; toView.center = viewCenter; [gesture setTranslation:CGPointZero inView:[self.context containerView]];

Slide 81

Slide 81 text

Finish or cancel when pan ends if (shouldFinish) { [self.context finishInteractiveTransition]; // Animate to center }else{ [self.context cancelInteractiveTransition]; // Animate off screen }

Slide 82

Slide 82 text

Complete transition BOOL completed = ![self.context transitionWasCancelled]; [self.context completeTransition:completed];

Slide 83

Slide 83 text

UIViewAnimateionCurveEaseInOut

Slide 84

Slide 84 text

Animation as physically degrading continuations of gestures?

Slide 85

Slide 85 text

Animations don't have a "starting velocity" input

Slide 86

Slide 86 text

spring animation initialSpringVelocity:?

Slide 87

Slide 87 text

"For smooth start to the animation, match this value to the view’s velocity as it was prior to attachment"

Slide 88

Slide 88 text

initalSpringVelocity:panVelocity

Slide 89

Slide 89 text

pan velocity / animation distance

Slide 90

Slide 90 text

UIKit Dynamics Facebook's POP Manually

Slide 91

Slide 91 text

http://www.objc.io/issue-12/interactive- animations.html

Slide 92

Slide 92 text

1. Move code to startInteractiveTransition:

Slide 93

Slide 93 text

2. Allow user interaction

Slide 94

Slide 94 text

3. Remove animations when gesture begins

Slide 95

Slide 95 text

4. Finish or cancel when gesture ends

Slide 96

Slide 96 text

5. Animate to final position

Slide 97

Slide 97 text

6. Complete the transition

Slide 98

Slide 98 text

Navigation transitions

Slide 99

Slide 99 text

Update percent complete - updateInteractiveTransition:(CGFloat)percentComplete

Slide 100

Slide 100 text

Use display link

Slide 101

Slide 101 text

Create right before animation starts self.startTime = CACurrentMediaTime(); self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)]; [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];

Slide 102

Slide 102 text

Calculate and update percent complete - (void)tick:(CADisplayLink *)link { CGFloat complete = (link.timestamp - self.startTime) / duration; [self.context updateInteractiveTransition:complete]; }

Slide 103

Slide 103 text

Invalidate when animation ends or gesture begins [self.displayLink invalidate]

Slide 104

Slide 104 text

Build better transitions by using the principles of animation, and always interactive transitions

Slide 105

Slide 105 text

Thank you @eallam

Slide 106

Slide 106 text

Credits The 12 Principles GIFs by Vincenzo Lodigiani the12principles.tumblr.com Banana Fingers Photo: flic.kr/p/9KUF1r "Transitional Interfaces" by Pasquale D'Silva "Animated UI Transitions and Perception of Time"