imageview怎么添加点击事件ios

imageview怎么添加点击事件ios,第1张

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]btn.frame = CGRectMake(0, 0, 320, 40)[btn addTarget:self action:@selector() forControlEvents:UIControlEventTouchDownRepeat][cell addSubview:btn]使用以上代码,...

首先我需要确定这个加号是button还是一个imageview。

如果是button直接add即可代码如下。

[button addTarget:self action:@selector(addBankCardAction) forControlEvents:UIControlEventTouchUpInside]

如果是一个imageview建议添加手势

//首先开启imageview的交互 

[imageView1 setUserInteractionEnabled:YES]

//创建一个手势

UITapGestureRecognizer *singleTap =

  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(whenClickImage)]

//将手势添加到iamgeview上

      [imageView addGestureRecognizer:singleTap]

//在事件中做你想做的。

-(void)whenClickImage

{

NSLog(@"you click image")

}

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

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存