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 妈妈生日祝福语简短励志