gpt4 book ai didi

iphone - 如何获取 UIView 中手指点击的坐标?

转载 作者:行者123 更新时间:2023-12-03 18:33:22 24 4
gpt4 key购买 nike

如何获取 UIView 中手指点击的坐标?(我不想使用一大堆按钮)

谢谢

最佳答案

有两种方法可以实现此目的。如果您已经拥有正在使用的 UIView 子类,则只需重写该子类上的 -touchesEnded:withEvent: 方法,如下所示:

- (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,并且该 View 由 View Controller 或其他任何东西拥有,那么您可以使用 UITapGestureRecognizer,如下所示:

// 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
}
}

关于iphone - 如何获取 UIView 中手指点击的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6113860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com