iOS 轻拍手势 和 tag 的混用

iOS中 大多数控件都有tag值,但是手势是不能添加tag值的,那么我们运用的时候如何使用呢、、
比如
-(void)viewDidLoad
{

  UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, imageWidth, imageWidth)];
image.layer.masksToBounds = YES;
image.layer.cornerRadius = imageWidth / 2;
image.userInteractionEnabled = YES;
image.tag = 109;
UITapGestureRecognizer *image9Tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapAction:)];
[image9 addGestureRecognizer:image9Tap];
[self.view addSubview:image9];

}
我们设置image的tag值是109,那么我们要实现他的方法,该如何实现呢

-(void)imageTapAction:(id)sender
{

UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)sender;

NSLog(@"%ld",[singleTap view].tag);

  switch ([singleTap view].tag) {
    case 101:

        NSLog(@"image1...");

        break;
 default:
        break;

}
这样我们就可以根据图片的tag值做我们想做的事!!!

你可能感兴趣的:(常见的错误解决总结)