Swift 2.1 為 UIView 添加點擊事件和點擊效果
更新時間:2016年07月22日 10:15:39 投稿:lqh
本文主要介紹 Swift UIView,這里給大家提供代碼示例作為參考為UIView 添加點擊事件和點擊效果,希望能幫助IOS開發(fā)的同學
前言
UIView 不像 UIButton 加了點擊事件就會有點擊效果,體驗要差不少,這里分別通過自定義和擴展來實現類似 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 可以換成點擊變更透明度。
相關文章
IOS 開發(fā)之UITableView 刪除表格單元寫法
這篇文章主要介紹了IOS 開發(fā)之UITableView 刪除表格單元寫法的相關資料,這里提供實例幫助大家實現該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08
iOS開發(fā)中使用FMDB來使程序連接SQLite數據庫
這篇文章主要介紹了iOS開發(fā)中使用FMDB來使程序連接SQLite數據庫,SQLite是一個簡單的嵌入式數據庫,非常適合輕量級使用,需要的朋友可以參考下2015-11-11

