我给UIImageView添加单击手势为什么没有反应,直接上码

我给UIImageView添加单击手势为什么没有反应,直接上码,第1张

给的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(@“双指双击”)

}

}

你需要给<a>添加href属性,

<a target="_top"><img src="..\images/anniu1.gif" width="157" height="38" border="0" onclick="javascript:window.history.back()"/></a>

改成

<a target="_top" href="javascript:window.history.back()"><img src="..\images/anniu1.gif" width="157" height="38" border="0" /></a>

UITapGestureRecognizer 的使用,判断点击哪个控件

说明:_backgroundImg 和_userHeaderImg都在_headerView上,给_headerView添加手势,判断用户点击是在_userHeaderImg还是_backgroundImg上,一个是修改头像,一个是修改背景图

注意:convertRect:后跟的是bounds,而不是frame

//添加手势

UITapGestureRecognizer *backgroundTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeHeaderImageAction:)]

[_headerView addGestureRecognizer:backgroundTap]

-(void)changeHeaderImageAction :(UITapGestureRecognizer *)tap {

CGPoint touchPoint = [tap locationInView:_headerView]

CGRect userHeaderImageRect = [_headerView convertRect:_userHeaderImg.bounds fromView:_userHeaderImg]

if (CGRectContainsPoint(userHeaderImageRect, touchPoint)) {

//clicked user header image.

_isHeaderImg=@"1"

[self setPhotoOrTakePictureAction]

return

}

CGRect backgroundImageRect = [_backgroundImg convertRect:_backgroundImg.bounds toView:_headerView]

if (CGRectContainsPoint(backgroundImageRect, touchPoint)) {

//clicked back image.

_isHeaderImg=@"0"

[self setPhotoOrTakePictureAction]

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11648134.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存