有时候会遇到点击一张图片,然后让这张图片触发一个事件,或者是跳转视图,想到的第一个方法就是用UIButton,将Button的背景图片属性设置为该图片,效果达到了,但不是最好的方法,直接触发方法
定义Image的对象
- UIImageView *imgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320,100)];
- imgView.backgroundColor = [UIColor redColor];//因为没有设置image属性,为了显示出图片覆盖区域
- imgView.userInteractionEnabled=YES;
- UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage)];
- [imgView addGestureRecognizer:singleTap];
- [singleTap release];
响应方法
- -(void)onClickImage{
- NSLog(@"图片被点击!");
- }
-
-
-
-
- /***************************************关于手势和点击的相关东西***********************************************************/
- 自己做了一个九宫格,里面的图标都是由多张图片重叠构成,然后包装成一个button来使用;遇到的问题是如何给这个“button”增加点击事件?解决思路1:网友提示在该“button”上增加一个真正的透明button,这样就可以顺利执行点击事件了;但是本人尝试发现,如果我调整了button的透明度后,点击是没有效果的!可能是我操作错误,待再次验证。解决思路2:为UIImageView增加手势
-
[ImageView addGestureRecognizer:我们自己的手势recognizer];
如何创建一个手势?看看UITapGestureRecognizer 的用法吧
//单指单击UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleFingerEvent:)];
singleFingerOne.numberOfTouchesRequired = 1; //手指数
singleFingerOne.numberOfTapsRequired = 1; //tap次数
singleFingerOne.delegate= self;
//单指双击
UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleFingerEvent:)];
singleFingerTwo.numberOfTouchesRequired = 1;
singleFingerTwo.numberOfTapsRequired = 2;
singleFingerTwo.delegate= self;
//双指单击
UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleDoubleFingerEvent:)];
doubleFingerOne.numberOfTouchesRequired = 2;
doubleFingerOne.numberOfTapsRequired = 1;
doubleFingerOne.delegate= self;
UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleDoubleFingerEvent:)];
doubleFingerTwo.numberOfTouchesRequired = 2;
doubleFingerTwo.numberOfTapsRequired = 2;
doubleFingerTwo.delegate= self;
//如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
//同理双指亦是如此
[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
[self.view addGestureRecognizer:singleFingerOne];
[self.view addGestureRecognizer:singleFingerTwo];
[self.view addGestureRecognizer:doubleFingerOne];
[self.view addGestureRecognizer:doubleFingerTwo];
[singleFingerOne release];
[singleFingerTwo release];
[doubleFingerOne release];
[doubleFingerTwo release];
处理事件的方法,代码:
//处理单指事件
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
{
if(sender.numberOfTapsRequired == 1) {
//单指单击
NSLog(@"单指单击");
}elseif(sender.numberOfTapsRequired == 2){
//单指双击
NSLog(@"单指双击");
}
}
//处理双指事件
- (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
{
if(sender.numberOfTapsRequired == 1) {
//双指单击
NSLog(@"双指单击");
}elseif(sender.numberOfTapsRequired == 2){
//双指双击
NSLog(@"双指双击");
}
}
-
转载:http://blog.csdn.net/a6472953/article/details/8646346