IOS UIView直接响应点击事件的解决方法

UIView的手势处理可以响应点击事件。(此处使用 UIImageView 作为示例)

  1.  self.allScreenImg.userInteractionEnabled = YES;//UIImageView默认不响应手势,需要开启userInteractionEnabled

    //处理单击事件

    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAllScreen:)];

    [self.allScreenImg addGestureRecognizer:singleTap];


2. 添加处理的方法

- (void) clickAllScreen:(UITapGestureRecognizer *) recognizer {

    AllScreenViewController* recordViewController = [[AllScreenViewController alloc] initWithStoryboard];

    [self.navigationController pushViewController:recordViewController animated:YES];

}


以上操作即可以响应了UIImageView的点击事件了。

方法二:

为UIImageView设置tag,直接处理手势 以处理单击事件:

    imageView.setTag = 333;

   -(void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event {

        UIImageView* img = (UIImageView *) [self.view viewWithTag: 333];

        if (img) {

            // TODO some thing

        }

    }




 参考博文:  http://www.tekuba.net/program/274/

    


你可能感兴趣的:(ios,UIView,点击)