大同烟草商务电子平台,暴雨去哪了,対魔忍阿莎姬3
前言
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 使用UITextField自定义搜索框 实现用户输入完之后“实时搜索”功能
网友评论