本文介绍了如何在UIView中获取手指敲击的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在UIView中获取手指敲击的坐标?
(我不想使用大量的按钮)
How do I get the coordinates for finger tapping in UIView?(I would prefer not to use a big array of buttons)
谢谢
推荐答案
有两种方法可以实现这一目标。如果你已经有了你正在使用的UIView的子类,你可以在该子类上覆盖 -touchesEnded:withEvent:
方法,如下所示:
There’re two ways to accomplish this. If you’ve already got a subclass of UIView that you’re using, you can just override the -touchesEnded:withEvent:
method on that subclass, like this:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
CGPoint point = [aTouch locationInView:self];
// point.x and point.y have the coordinates of the touch
}
如果您还没有将UIView子类化,并且该视图归视图控制器所有,那么您可以使用UITapGestureRecognizer,如下所示:
If you’re not already subclassing UIView, though, and the view is owned by a view controller or whatever, then you can use a UITapGestureRecognizer, like this:
// when the view's initially set up (in viewDidLoad, for example)
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[someView addGestureRecognizer:rec];
[rec release];
// elsewhere
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateRecognized)
{
CGPoint point = [recognizer locationInView:recognizer.view];
// again, point.x and point.y have the coordinates
}
}
这篇关于如何在UIView中获取手指敲击的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!