Swift 2.1 為 UIView 添加點(diǎn)擊事件和點(diǎn)擊效果
前言
UIView 不像 UIButton 加了點(diǎn)擊事件就會(huì)有點(diǎn)擊效果,體驗(yàn)要差不少,這里分別通過自定義和擴(kuò)展來實(shí)現(xiàn)類似 UIButton 的效果。
正文
一、為 UIView 添加點(diǎn)擊事件
extension UIView {
func addOnClickListener(target: AnyObject, action: Selector) {
let gr = UITapGestureRecognizer(target: target, action: action)
gr.numberOfTapsRequired = 1
userInteractionEnabled = true
addGestureRecognizer(gr)
}
}
二、為 UIView 添加點(diǎn)擊效果
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()
})
}
}
這里大家可以換成自己的點(diǎn)擊效果,如果是 UIImageView 可以換成點(diǎn)擊變更透明度。
相關(guān)文章
IOS textField限制字節(jié)長(zhǎng)度
這篇文章主要介紹了IOS textField限制字節(jié)長(zhǎng)度的相關(guān)資料,需要的朋友可以參考下2016-02-02
詳解 swift3.0 可選綁定共用同一塊內(nèi)存空間的實(shí)例
這篇文章主要介紹了詳解 swift3.0 可選綁定共用同一塊內(nèi)存空間的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
IOS 開發(fā)之UITableView 刪除表格單元寫法
這篇文章主要介紹了IOS 開發(fā)之UITableView 刪除表格單元寫法的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08
在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析
這篇文章主要介紹了在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析,包括獲取當(dāng)前位置和計(jì)算兩點(diǎn)間距離等基本功能的實(shí)現(xiàn),需要的朋友可以參考下2016-05-05
iOS開發(fā)之一些實(shí)用小知識(shí)點(diǎn)總結(jié)
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之實(shí)用小知識(shí)點(diǎn)的相關(guān)資料,其中包括防止UIButton,cell等重復(fù)點(diǎn)擊、獲取當(dāng)前視圖最頂層的ViewController以及代碼截圖相關(guān)的等知識(shí),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫
這篇文章主要介紹了iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫,SQLite是一個(gè)簡(jiǎn)單的嵌入式數(shù)據(jù)庫,非常適合輕量級(jí)使用,需要的朋友可以參考下2015-11-11

