Swift 2.1 為 UIView 添加點擊事件和點擊效果
更新時間:2016年07月22日 10:15:39 投稿:lqh
本文主要介紹 Swift UIView,這里給大家提供代碼示例作為參考為UIView 添加點擊事件和點擊效果,希望能幫助IOS開發(fā)的同學(xué)
前言
UIView 不像 UIButton 加了點擊事件就會有點擊效果,體驗要差不少,這里分別通過自定義和擴(kuò)展來實現(xiàn)類似 UIButton 的效果。
正文
一、為 UIView 添加點擊事件
extension UIView { func addOnClickListener(target: AnyObject, action: Selector) { let gr = UITapGestureRecognizer(target: target, action: action) gr.numberOfTapsRequired = 1 userInteractionEnabled = true addGestureRecognizer(gr) } }
二、為 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 可以換成點擊變更透明度。
相關(guān)文章
詳解 swift3.0 可選綁定共用同一塊內(nèi)存空間的實例
這篇文章主要介紹了詳解 swift3.0 可選綁定共用同一塊內(nèi)存空間的實例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09IOS 開發(fā)之UITableView 刪除表格單元寫法
這篇文章主要介紹了IOS 開發(fā)之UITableView 刪除表格單元寫法的相關(guān)資料,這里提供實例幫助大家實現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08在iOS App中實現(xiàn)地理位置定位的基本方法解析
這篇文章主要介紹了在iOS App中實現(xiàn)地理位置定位的基本方法解析,包括獲取當(dāng)前位置和計算兩點間距離等基本功能的實現(xiàn),需要的朋友可以參考下2016-05-05iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫
這篇文章主要介紹了iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫,SQLite是一個簡單的嵌入式數(shù)據(jù)庫,非常適合輕量級使用,需要的朋友可以參考下2015-11-11