gpt4 book ai didi

iphone - 如何获得任意控制 objective-c 的屏幕触摸位置?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:14 27 4
gpt4 key购买 nike

您好,我想获取任何控件或发生触摸的任何位置的触摸位置/点。

为此,我已经实现了这个,但我没有得到正确的接触点。

   // Create gesture recognizer, notice the selector method
UITapGestureRecognizer *oneFingerTwoTaps =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
oneFingerTwoTaps.delegate=self;
// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:1];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
[[self view] addGestureRecognizer:oneFingerTwoTaps];

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

CGPoint point= [touch locationInView:touch.view];
NSLog(@"Point - %f, %f",point.x,point.y);
NSLog(@"Touch");
return NO; // handle the touch
}

当我试图点击任何 UIButton、UIImage、UITableView 时,它没有给我正确的点击点,我做错了什么吗?请帮我。谢谢你。

最佳答案

您的代码打印触摸发生在 View 中的位置。因此,如果您触摸中间的 50x100 大小的按钮,它将打印“Point 25.0, 50.0”。

如果要找到UIScreen的触摸位置,就得进行值转换:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

CGPoint point = [touch locationInView:touch.view];
CGPoint pointOnScreen = [touch.view convertPoint:point toView:nil];
NSLog(@"Point - %f, %f", pointOnScreen.x, pointOnScreen.y);
NSLog(@"Touch");
return NO; // handle the touch
}

或者直接获取窗口(屏幕)空间中的坐标:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

CGPoint pointOnScreen = [touch locationInView:nil];
NSLog(@"Point - %f, %f", pointOnScreen.x, pointOnScreen.y);
NSLog(@"Touch");
return NO; // handle the touch
}

关于iphone - 如何获得任意控制 objective-c 的屏幕触摸位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17110043/

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