O documento discute animações no iOS, abordando tópicos como:
1) Por que usar animações e como elas orientam e chamam a atenção do usuário;
2) Como as animações UIView funcionam por baixo dos panos usando CAMediaTimingFunction e CAAnimation;
3) Problemas comuns em encadear animações e formas de resolvê-los, como usar wrappers ou keyframe animations.
45. Globalcode – Open4education
CALayer
Set da propriedade dentro de um bloco
UIView muda o CALayer
Model layer é afetado imediatamente
Presentation layer faz a mudança gradual
53. Globalcode – Open4education
Encadeamento de animações
Não há uma forma simples e elegante
Keyframe animations
Não têm suporte a spring
Não têm suporte a curvas
Você precisa lidar com os delays
59. Globalcode – Open4education
Encadeamento de animações
Wrapper de UIView animations
Fica com a responsabilidade de concatenar blocos de animação
Propriedades não animáveis, controlar progresso de animações, …
https://github.com/nubank/NUAnimationKit
72. Globalcode – Open4education
appa
Implementação
Não tem resolução suficiente (~50 ms)
(Precisamos de ~16 ms)
NSTimer?
[NSTimer timerWithTimeInterval:1/60.f
target:self
selector:@selector(updateProgress)
userInfo:nil
repeats:YES];
Because of the various input sources a typical
run loop manages, the effective resolution of
the time interval for a timer is limited to on the
order of 50-100 milliseconds
74. Globalcode – Open4education
Propriedades não-animáveis
Implementação
A CADisplayLink object is a timer object that allows your application to synchronize
its drawing to the refresh rate of the display…
CADisplayLink
75. Globalcode – Open4education
Propriedades não-animáveis
Implementação
A CADisplayLink object is a timer object that allows your application to synchronize
its drawing to the
CADisplayLink
refresh rate of the display…
96. Globalcode – Open4education
AutoLayout é ótimo para views estáticas
Cuidado pra não brigar com ele nas animações!
Se alguma coisa mudar, ele desfaz sua animação!
Animações com AutoLayout
104. Globalcode – Open4education
O que aconteceu?
Clicar no textfield causa um layout pass
Animações com AutoLayout
[self.view setNeedsLayout];
105. Globalcode – Open4education
O que aconteceu?
Clicar no textfield causa um layout pass
Autolayout não sabia das suas mudanças…
Logo, ele descarta
Animações com AutoLayout
[self.view setNeedsLayout];
107. Globalcode – Open4education
Animações com AutoLayout
O sistema seta os frames
(Layout inicial)
self.leftView.bounds = ...
self.leftView.center = ...
109. Globalcode – Open4education
UIView Animation block
Animações com AutoLayout
O sistema invalida o layout
(Tap no UITextField)
[self.view setNeedsLayout];
110. Globalcode – Open4education
UIView Animation block
Animações com AutoLayout
O sistema seta os frames
(Segundo layout)
self.leftView.bounds = ...
self.leftView.center = ...
113. Globalcode – Open4education
Duas formas de lidar com isso:
Não mudar o frame dos objetos
Usar CGAffineTransform
Animações com AutoLayout
114. Globalcode – Open4education
Duas formas de lidar com isso:
Não mudar o frame dos objetos
Usar CGAffineTransform
Mudar constraints
Animações com AutoLayout
117. Globalcode – Open4education
CGAffineTransform
Aplicado por cima do bounds e center
self.leftView.center = CGPointMake(self.leftView.center.x + 100,
self.leftView.center.y);
[UIView animateWithDuration:0.5
animations:^{
}];
118. Globalcode – Open4education
[UIView animateWithDuration:0.5
animations:^{
}];
Aplicado por cima do bounds e center
CGAffineTransform
self.leftView.transform = CGAffineTransformMakeTranslation(100, 0);
120. Globalcode – Open4education
Duas formas de lidar com isso:
Não mudar o frame dos objetos
Usar CGAffineTransform
Animações com AutoLayout
Mudar constraints
122. Globalcode – Open4education
Atualiza as constraints dentro do bloco
Mudar constraints
[UIView animateWithDuration:0.5
animations:^{
self.leftConstraint.constant += 100;
}];
123. Globalcode – Open4education
Atualiza as constraints dentro do bloco
Mudar constraints
[UIView animateWithDuration:0.5
animations:^{
self.leftConstraint.constant += 100;
}];
Animação não acontece
pois o set do frame é feito
fora do bloco!
126. Globalcode – Open4education
Animações com AutoLayout
O sistema seta os frames
(Layout inicial)
self.leftView.bounds = ...
self.leftView.center = ...
128. Globalcode – Open4education
Animações com AutoLayout
O sistema invalida o layout…
mas não seta os novos frames
UIView Animation block
[self.view setNeedsLayout];
129. Globalcode – Open4education
Animações com AutoLayout
UIView Animation block
self.leftView.bounds = ...
self.leftView.center = ...
O sistema seta os frames
(Segundo layout)
132. Globalcode – Open4education
Atualiza as constraints dentro do bloco
Chama
Isso força um novo layout pass
Mudar constraints
[self.view layoutIfNeeded];
133. Globalcode – Open4education
Atualiza as constraints dentro do bloco
Mudar constraints
[UIView animateWithDuration:0.5
animations:^{
self.leftConstraint.constant += 100;
}];
[self.view layoutIfNeeded];
134. Globalcode – Open4education
Atualiza as constraints dentro do bloco
Mudar constraints
[UIView animateWithDuration:0.5
animations:^{
self.leftConstraint.constant += 100;
}];
[self.view layoutIfNeeded];
136. Globalcode – Open4education
Novidades no iOS 10
Nova arquitetura de animações
[UIView animateWithDuration:
delay:
options:
animations:^{
}
completion:^(BOOL finished) {
}];
Métodos de classe
137. Globalcode – Open4education
Novidades no iOS 10
Nova arquitetura de animações
@protocol UIViewAnimating <NSObject>
@protocol UIViewImplicitlyAnimating <UIViewAnimating>
@protocol UITimingCurveProvider <NSCoding, NSCopying>
Protocolos
138. Globalcode – Open4education
Novidades no iOS 10
Nova arquitetura de animações
UIViewPropertyAnimator
@protocol UIViewAnimating <NSObject>
@protocol UIViewImplicitlyAnimating <UIViewAnimating>
@protocol UITimingCurveProvider <NSCoding, NSCopying>
139. Globalcode – Open4education
Novidades no iOS 10
Nova arquitetura de animações
UITimingCurveProvider
@protocol UIViewAnimating <NSObject>
@protocol UIViewImplicitlyAnimating <UIViewAnimating>
@protocol UITimingCurveProvider <NSCoding, NSCopying>
UIViewPropertyAnimator
140. Globalcode – Open4education
Novidades no iOS 10
Nova arquitetura de animações
UITimingCurveProvider
@protocol UIViewAnimating <NSObject>
@protocol UIViewImplicitlyAnimating <UIViewAnimating>
@protocol UITimingCurveProvider <NSCoding, NSCopying>
UIViewPropertyAnimator
Cubic / Spring
151. Globalcode – Open4education
Novidades no iOS 10
UIViewPropertyAnimator
self.animator =
[[UIViewPropertyAnimator alloc] initWithDuration:...
curve:...
animations:^{
...
}];
[self.animator addAnimations:^{
} delayFactor:0.5];
Mudanças são aditivas
O sistema cria uma transição entre um passo de animação e outro.