Skip to content

Instantly share code, notes, and snippets.

@casademora
Last active August 26, 2016 02:39
Show Gist options
  • Save casademora/c4b19d8503767479c0e05a836de4aa4c to your computer and use it in GitHub Desktop.
Save casademora/c4b19d8503767479c0e05a836de4aa4c to your computer and use it in GitHub Desktop.
Animation behaviors demo from Shanghai Cocoaheads
//
// 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 viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
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: CGFloat = 2
@IBInspectable var delay: CGFloat = 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(NSTimeInterval(duration),
delay: NSTimeInterval(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(NSTimeInterval(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 }
}
}
@GuanshanLiu
Copy link

I have two suggestions.

  1. Use Double instead of CGFloat, then we do not need convert it to NSTimeInterval.

public typealias NSTimeInterval = Double

What I found strange today if I use NSTimeInterval, it will not show in storyboard. :(

  1. Use @IBOutlet var animations: [MoveInBehavior]? in this scenario. So Xcode can reminder us to add ?. My argument should be in this scenario designers may change their mind later, and do not want animations. With Optional, even if there is no Behavior, the app can still run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment