如何给uiimageview添加点击事件

 我来答
badkano
推荐于2016-03-08 · 知道合伙人体育行家
badkano
知道合伙人体育行家
采纳数:144777 获赞数:885365
团长

向TA提问 私信TA
展开全部
给的UIImageView增加点击事件

解决思路1:网友提示在该“button”上增加一个真正的透明button,这样就可以顺利执行点击事件了;但是本人尝试发现,如果我调整了button的透明度后,点击是没有效果的!可能是我操作错误,待再次验证。
解决思路2:为的UIImageView增加手势
[ImageView的addGestureRecognizer:我们自己的手势识别 ]
如何创建一个手势?看看UITapGestureRecognizer的用法吧

//单指单击
UITapGestureRecognizer * singleFingerOne = [[UITapGestureRecognizer页头] initWithTarget:自
操作:@selector(handleSingleFingerEvent :)]
singleFingerOne.numberOfTouchesRequired = 1; //手指数
singleFingerOne.numberOfTapsRequired = 1; //抽头次数
singleFingerOne 委托 =自我;

//单指双击
UITapGestureRecognizer * singleFingerTwo = [[UITapGestureRecognizer页头] initWithTarget:自
操作:@selector(handleSingleFingerEvent :)];
singleFingerTwo.numberOfTouchesRequired = 1;
singleFingerTwo.numberOfTapsRequired = 2;
。singleFingerTwo 委托 =自我;

//双指单击
UITapGestureRecognizer * doubleFingerOne = [[UITapGestureRecognizer页头] initWithTarget:自
操作:@selector(handleDoubleFingerEvent :)];
doubleFingerOne.numberOfTouchesRequired = 2;
doubleFingerOne.numberOfTapsRequired = 1;
。doubleFingerOne 委托 =自我;

UITapGestureRecognizer * doubleFingerTwo = [[UITapGestureRecognizer页头] initWithTarget:自
操作:@selector(handleDoubleFingerEvent :)];
doubleFingerTwo.numberOfTouchesRequired = 2;
doubleFingerTwo.numberOfTapsRequired = 2;
。doubleFingerTwo 委托 =自我;

//如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo]
//同理双指亦是如此
[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo]

[self.view addGestureRecognizer:singleFingerOne]
[self.view addGestureRecognizer:singleFingerTwo]
[self.view addGestureRecognizer:doubleFingerOne]
[self.view addGestureRecognizer:doubleFingerTwo]

[singleFingerOne发布]
[singleFingerTwo发布]
[doubleFingerOne发布]
[doubleFingerTwo发布]

处理事件的方法,代码:
//处理单指事件
- (无效)handleSingleFingerEvent:(UITapGestureRecognizer *)发件人
{
如果(sender.numberOfTapsRequired == 1){
//单指单击
的NSLog(@“单指单击”);
} 否则如果(sender.numberOfTapsRequired == 2){
//单指双击
的NSLog(@“单指双击”);
}
}
//处理双指事件
- (无效)handleDoubleFingerEvent:(UITapGestureRecognizer *)发件人
{
如果(sender.numberOfTapsRequired == 1){
//双指单击
的NSLog(@“双指单击”);
} 否则如果(sender.numberOfTapsRequired == 2){
//双指双击
的NSLog(@“双指双击”);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式