Skip to content

Instantly share code, notes, and snippets.

@erica
Created April 30, 2015 15:12
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 erica/f960c9f45663fcb2fc13 to your computer and use it in GitHub Desktop.
Save erica/f960c9f45663fcb2fc13 to your computer and use it in GitHub Desktop.
Auto Closure
import UIKit
import XCPlayground
// Enable simulator to view results in playground
extension UIView {
class func animate(duration : NSTimeInterval, @autoclosure _ animations : () -> Void) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(duration)
animations()
UIView.commitAnimations()
}
}
// This does not work
//extension UIView {
// class func animate(duration : NSTimeInterval, @autoclosure _ animations : () -> Void) {
// // This does not work: Invalid use of non-escaping function in escaping context
// // Cannot invoke with argument list of NSTimeInterval, @autoclosure () -> Void
// UIView.animateWithDuration(duration, animations: animations) // error
// }
//}
let view = UIView(frame:CGRectMake(0, 0, 200, 180))
view.backgroundColor = .redColor()
XCPShowView("View", view)
UIView.animate(2.0, view.backgroundColor = .blueColor())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment