class func animate(
withDuration duration: TimeInterval,
delay: TimeInterval,
options: UIView.AnimationOptions = [],
animations: @escaping () -> Void,
completion: ((Bool) -> Void)? = nil
)
duration
delay
options
animations
completion
class func animateKeyframes(
withDuration duration: TimeInterval,
delay: TimeInterval,
options: UIView.KeyframeAnimationOptions = [],
animations: @escaping () -> Void,
completion: ((Bool) -> Void)? = nil
)
class func addKeyframe(
withRelativeStartTime frameStartTime: Double,
relativeDuration frameDuration: Double,
animations: @escaping () -> Void
)
frameStartTime
frameDuration
animations
class ViewController: UIViewController {
@IBOutlet weak var leftView: UIView!
@IBOutlet weak var rightView: UIView!
@IBOutlet weak var spiderView: UIImageView!
@IBOutlet weak var spiderLineView: UIView!
@IBOutlet weak var spiderRotationView: UIImageView!
@IBOutlet weak var leftLayout: NSLayoutConstraint!
@IBOutlet weak var rightLayout: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonDidTab(_ sender: Any) {
UIView.animateKeyframes(withDuration: 4, delay: 0) {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 2/4) {
self.leftView.transform = CGAffineTransform(translationX: -50, y: 0)
self.rightView.transform = CGAffineTransform(translationX: 50, y: 0)
self.spiderView.transform = CGAffineTransform(translationX: 0, y: 100)
self.spiderView.alpha = 1
self.spiderLineView.transform = CGAffineTransform(translationX: 0, y: 100)
}
UIView.addKeyframe(withRelativeStartTime: 2/4, relativeDuration: 1) {
self.leftView.transform = .identity
self.rightView.transform = .identity
self.spiderView.transform = .identity
self.spiderView.alpha = 0
self.spiderLineView.transform = .identity
}
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 2/4) {
self.spiderRotationView.transform = CGAffineTransform(rotationAngle: 180)
}
UIView.addKeyframe(withRelativeStartTime: 2/4, relativeDuration: 1) {
self.spiderRotationView.transform = .identity
}
}
}
}