Skip to content

Instantly share code, notes, and snippets.

@reddavis
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save reddavis/fa62974e05b407af9e3c to your computer and use it in GitHub Desktop.

Select an option

Save reddavis/fa62974e05b407af9e3c to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController
{
var animationDuration: NSTimeInterval = 1.0
var springDamping: CGFloat = 0.5
var tweakerController: TweakerViewController?
let square = UIView(frame: CGRectMake(135.0, 50.0, 50.0, 50.0))
override func viewDidLoad()
{
super.viewDidLoad()
self.edgesForExtendedLayout = UIRectEdge.None
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Tweak!", style: UIBarButtonItemStyle.Plain, target: self, action: "showTweakerButtonTapped:")
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Animate!", style: UIBarButtonItemStyle.Plain, target: self, action: "animateButtonTapped:")
self.square.backgroundColor = UIColor.redColor()
self.view.addSubview(self.square)
self.tweakerController = TweakerViewController()
self.tweakerController?.build({ (make: TweakerMaker) in
make.slider().title("Duration").minValue(0.25).maxValue(2.0).value(self.animationDuration).valueChanged({ (value) -> Void in
self.animationDuration = NSTimeInterval(value)
})
make.slider().title("Spring Damping").minValue(0.0).maxValue(1.0).value(self.springDamping).valueChanged({ (value) -> Void in
self.springDamping = CGFloat(value)
})
make.switchControl().title("Green?").off().valueChanged({ (on) -> Void in
self.square.backgroundColor = on ? UIColor.greenColor() : UIColor.redColor()
})
})
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
// MARK: Actions
func showTweakerButtonTapped(sender: AnyObject)
{
var navigationController = UINavigationController(rootViewController: self.tweakerController!)
self.presentViewController(navigationController, animated: true, completion: nil)
}
func animateButtonTapped(sender: AnyObject)
{
var originalFrame = self.square.frame
var newFrame = self.square.frame
newFrame.origin.y = 200.0
UIView.animateWithDuration(self.animationDuration, delay: 0.0, usingSpringWithDamping: self.springDamping, initialSpringVelocity: 0.0, options: .CurveEaseInOut, animations: ({
self.square.frame = newFrame
}), completion: {(complete: Bool) -> Void in
UIView.animateWithDuration(self.animationDuration, delay: 0.0, usingSpringWithDamping: self.springDamping, initialSpringVelocity: 0.0, options: .CurveEaseInOut, animations: ({
self.square.frame = originalFrame
}), completion: {(complete: Bool) -> Void in
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment