Tap手势

使UIView能够支持点击的手势,需要用下面的代码:

UITapGestureRecognizer *t = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(singleTap:)];

t.delegate = self;

UIImageView *subView = (UIImageView*)[self.view viewWithTag:1234];

[subView addGestureRecognizer:t];

 

这里把手势加到了subView里,而没有加到整个rootView中,也就只保证了在这里面的点击手势有效,而不对subView外面的区域产生影响。

 

另外在xib设计时,注意该控件的User Interaction Enabled 这一项要选上。

 

然后处理singleTap方法:

 

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

    CGPoint p = [tap locationInView:tap.view];

    NSLog(@"single tap: %f %f", p.x, p.y );

}

用上面方法很容易找到单击点的坐标,再完成相应的工作即可。

posted @ 2015-11-09 13:35  驭人之道  阅读(493)  评论(0编辑  收藏  举报