Forked from casademora/gist:c4b19d8503767479c0e05a836de4aa4c
Last active
August 26, 2016 02:29
-
-
Save GuanshanLiu/de4c4c92db031a3b765ccf10186ceb36 to your computer and use it in GitHub Desktop.
Animation behaviors demo from Shanghai Cocoaheads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// BehaviorsDemo | |
// | |
// Created by Saul Mora on 8/25/16. | |
// Copyright © 2016 Magical Panda. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet var animations: [MoveInBehavior]? | |
// @IBOutlet var redBlock: UIView! | |
// lazy var moveIn: MoveInBehavior = { | |
// return MoveInBehavior(view: self.redBlock) | |
// }() | |
// override func viewDidLoad() | |
// { | |
// super.viewDidLoad() | |
// | |
// } | |
// | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
animations?.forEach { $0.prepare() } | |
} | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
animations?.forEach { $0.start() } | |
// let finalCenterPosition = redBlock.center | |
// let animations = { self.redBlock.center = finalCenterPosition } | |
// | |
// let screen = UIScreen.mainScreen() | |
// redBlock.center = CGPoint(x: redBlock.center.x, y: screen.bounds.height) | |
// UIView.animateWithDuration(2, animations: animations) | |
} | |
} | |
class Behavior: NSObject | |
{ | |
@IBOutlet var view: UIView! | |
@IBInspectable var duration: Double = 2 | |
@IBInspectable var delay: Double = 0 | |
@IBInspectable var springDamping: CGFloat = 0 | |
@IBInspectable var initialSpringVelocity: CGFloat = 0 | |
@IBOutlet var nextAnimations: [Behavior]! | |
var animations: Void -> Void = {} | |
@IBAction func prepare() | |
{ | |
nextAnimations?.forEach { $0.prepare() } | |
} | |
func animationsDidComplete(finished: Bool) | |
{ | |
nextAnimations?.forEach { | |
$0.prepare() | |
$0.start() | |
} | |
} | |
@IBAction func start() | |
{ | |
UIView.animateWithDuration(duration, | |
delay: delay, | |
usingSpringWithDamping: springDamping, | |
initialSpringVelocity: initialSpringVelocity, | |
options: [], | |
animations: animations, | |
completion: animationsDidComplete) | |
} | |
} | |
class FadeOutBehavior: Behavior | |
{ | |
override func prepare() | |
{ | |
view.alpha = 1 | |
animations = { self.view.alpha = 0 } | |
} | |
override func start() | |
{ | |
UIView.animateWithDuration(duration, animations: animations) | |
} | |
} | |
class MoveInBehavior: Behavior | |
{ | |
@IBAction override func prepare() | |
{ | |
super.prepare() | |
let screen = UIScreen.mainScreen() | |
let finalPosition = view.center | |
view.center = CGPoint(x: view.center.x, y: screen.bounds.height) | |
animations = { self.view.center = finalPosition } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment