Swift 2.1 为 UIView 添加点击事件和点击效果
前言
UIView不像UIButton加了点击事件就会有点击效果,体验要差不少,这里分别通过自定义和扩展来实现类似UIButton的效果。
正文
一、为UIView添加点击事件
extensionUIView{
funcaddOnClickListener(target:AnyObject,action:Selector){
letgr=UITapGestureRecognizer(target:target,action:action)
gr.numberOfTapsRequired=1
userInteractionEnabled=true
addGestureRecognizer(gr)
}
}
二、为UIView添加点击效果
classUIViewEffect:UIView{
overridefunctouchesBegan(touches:Set<UITouch>,withEventevent:UIEvent?){
backgroundColor=UIColor.groupTableViewBackgroundColor()
}
overridefunctouchesCancelled(touches:Set<UITouch>?,withEventevent:UIEvent?){
UIView.animateWithDuration(0.15,animations:{()->Voidin
self.backgroundColor=UIColor.clearColor()
})
}
overridefunctouchesEnded(touches:Set<UITouch>,withEventevent:UIEvent?){
UIView.animateWithDuration(0.15,animations:{()->Voidin
self.backgroundColor=UIColor.clearColor()
})
}
}
这里大家可以换成自己的点击效果,如果是UIImageView可以换成点击变更透明度。
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短