媒介
UIView 不像 UIButton 加了點擊事宜就會有點擊後果,體驗要差很多,這裡分離經由過程自界說和擴大來完成相似 UIButton 的後果。
注釋
1、為 UIView 添加點擊事宜
extension UIView { func addOnClickListener(target: AnyObject, action: Selector) { let gr = UITapGestureRecognizer(target: target, action: action) gr.numberOfTapsRequired = 1 userInteractionEnabled = true addGestureRecognizer(gr) } }
2、為 UIView 添加點擊後果
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() }) } }
這裡年夜家可以換成本身的點擊後果,假如是 UIImageView 可以換成點擊變革通明度。
【Swift 2.1 為 UIView 添加點擊事宜和點擊後果】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!