Skip to content

Instantly share code, notes, and snippets.

@oksep
Created June 20, 2016 10:19
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 oksep/32ea6a6bcd37bd74b2614099d63f1aae to your computer and use it in GitHub Desktop.
Save oksep/32ea6a6bcd37bd74b2614099d63f1aae to your computer and use it in GitHub Desktop.
iOS UIView custom interaction
extension UIView {
func addOnClickListener(target: AnyObject, action: Selector) {
let gr = UITapGestureRecognizer(target: target, action: action)
gr.numberOfTapsRequired = 1
userInteractionEnabled = true
addGestureRecognizer(gr)
}
}
class UIViewEffect : UIView {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
backgroundColor = UIColor.groupTableViewBackgroundColor()
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
UIView.animateWithDuration(0.15, animations: { () -> Void in
self.backgroundColor = UIColor.clearColor()
})
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
UIView.animateWithDuration(0.15, animations: { () -> Void in
self.backgroundColor = UIColor.clearColor()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment