Skip to content

Instantly share code, notes, and snippets.

@runys
Last active March 27, 2017 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runys/005f259bf81e0a9d5a93b8680edc6d12 to your computer and use it in GitHub Desktop.
Save runys/005f259bf81e0a9d5a93b8680edc6d12 to your computer and use it in GitHub Desktop.
How to create simple animations using iOS 10 and Swift 3. (Xcode Playground)
import UIKit
/*:
#### Animações básicas
- Ref: [Getting Started](https://www.raywenderlich.com/146603/ios-animation-tutorial-getting-started-2)
- Ref: [Custom View Controller Presentation Transitions](https://www.raywenderlich.com/146692/ios-animation-tutorial-custom-view-controller-presentation-transitions-2)
Para animar componentes do UIKit podemos utilizar o método de animação básico
*/
UIView.animate(withDuration: 0) {
// Insira as modificações aqui
}
//: Podemos utilizar a versão com uma closure de complitude
UIView.animate(withDuration: 0, animations: {
// Insira as modificações aqui
}) { (hasCompleted) in
if hasCompleted {
// Insira o que acontecerá após a animação estar completa
}
}
//: Podemos utilizar a versão com mais opções de customização, permitindo a inserção de um delay para iniciar as animações
UIView.animate(withDuration: 0, delay: 0, options: [], animations: {
// Insira as modificações aqui
}) { (hasCompleted) in
if hasCompleted {
// Insira o que acontecerá após a animação estar completa
}
}
/*:
Para executar uma animação basta definir, no bloco de animação, o estado final de alguma propriedade animável do componente.
Quais as propriedades animáveis de um componente?
- Posição e tamanho
- bounds
- frame
- center
- Aparência
- backgroundColor
- alpha
- Transformações
- transform (rotação, escala e posição)
*/
let square = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100 ))
UIView.animate(withDuration: 10) {
square.bounds = CGRect(x: 10, y: 10, width: 10, height: 10)
square.center = CGPoint(x: 20, y: 20)
square.backgroundColor = UIColor.red
square.alpha = 0.5
square.transform = CGAffineTransform(rotationAngle: 0)
square.transform = CGAffineTransform(translationX: 100, y: 100)
square.transform = CGAffineTransform(scaleX: 2, y: 2)
}
/*:
Podemos adicionar opções para customizar a forma como a animação irá se comportar.
Quais as opções de animação?
- Repetição
- .repeat
- .autoreverse
- Easing
- .curveLinear
- .curveEaseIn
- .curveEaseOut
- .curveEaseInOut
*/
UIView.animate(withDuration: 3, delay: 2, options: [.autoreverse, .curveEaseOut], animations: {
square.backgroundColor = UIColor.blue
}) { (hasCompleted) in
if hasCompleted {
print("Agora o quadrado está azul!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment