Swift 2.1 為 UIView 添加點(diǎn)擊事件和點(diǎn)擊效果
前言
UIView 不像 UIButton 加了點(diǎn)擊事件就會(huì)有點(diǎn)擊效果,體驗(yàn)要差不少,這里分別通過(guò)自定義和擴(kuò)展來(lái)實(shí)現(xiàn)類(lèi)似 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實(shí)現(xiàn)聯(lián)系人按照首字母進(jìn)行排序的實(shí)例
下面小編就為大家分享一篇iOS實(shí)現(xiàn)聯(lián)系人按照首字母進(jìn)行排序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
ios利用正則表達(dá)式判斷手機(jī)號(hào)碼格式是否正確的實(shí)例
下面小編就為大家分享一篇ios利用正則表達(dá)式判斷手機(jī)號(hào)碼格式是否正確的實(shí)例,具有很好的參考價(jià)值。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
iOS開(kāi)發(fā)UICollectionView實(shí)現(xiàn)拖拽效果
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)UICollectionView實(shí)現(xiàn)拖拽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
詳解ios中的SQL數(shù)據(jù)庫(kù)文件加密 (使用sqlcipher)
本篇文章主要介紹了ios中的SQL數(shù)據(jù)庫(kù)文件加密 (使用sqlcipher),具有一定的參考價(jià)值,這里整理了詳細(xì)的代碼,感興趣的小伙伴們可以參考一下。2016-12-12

