xcode 如何获取自定义 UIButton 上的点击坐标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11847972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the tap coordinates on a custom UIButton?
提问by twbbas
I'm using XCode 4.4 developing for iOS 5 on an iPad and am using the Storyboard layout when creating my custom button.
我在 iPad 上使用 XCode 4.4 为 iOS 5 开发,并在创建自定义按钮时使用 Storyboard 布局。
I have the touch event correctly working and logging but now I want to get the x/y coordinates of the tap on my custom button.
我的触摸事件正常工作和记录,但现在我想在我的自定义按钮上获取点击的 x/y 坐标。
If possible, I'd like the coordinates to be relative to the custom button instead of relative to the entire iPad screen.
如果可能,我希望坐标相对于自定义按钮,而不是相对于整个 iPad 屏幕。
Here's my code in the .h file:
这是我在 .h 文件中的代码:
- (IBAction)getButtonClick:(id)sender;
and my code in the .m file:
和我在 .m 文件中的代码:
- (IBAction)getButtonClick:(id)sender {
NSLog(@"Image Clicked.");
}
Like I said, that correctly logs when I tap the image.
就像我说的,当我点击图像时,它会正确记录。
How can I get the coordinates of the tap?
我怎样才能得到水龙头的坐标?
I've tried a few different examples from the internet but they always freeze when it displays a bunch of numbers (maybe the coordinates) in the log box. I'm VERY new to iOS developing so please make it as simple as possible. Thanks!
我尝试了一些来自互联网的不同示例,但是当它在日志框中显示一堆数字(可能是坐标)时,它们总是冻结。我对 iOS 开发非常陌生,所以请尽可能简单。谢谢!
回答by Vladimir
To get touch location you can use another variant of button action method: myAction:forEvent:
(if you create it from IB interface note "sender and event" option in arguments field: )
为了让您可以用按钮操作方法的另一种变型触摸位置:myAction:forEvent:
(如果你从参数字段IB接口记“发件人和事件”选项来创建它:)
Then in your action handler you can get touch location from event parameter, for example:
然后在您的操作处理程序中,您可以从事件参数中获取触摸位置,例如:
- (IBAction)myAction:(UIButton *)sender forEvent:(UIEvent *)event {
NSSet *touches = [event touchesForView:sender];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:sender];
NSLog(@"%@", NSStringFromCGPoint(touchPoint));
}
回答by Ammar Mujeeb
Incase of Swift 3.0 the accepted answer works same except syntax will be changed as follows:
Incase Swift 3.0 接受的答案工作相同,除了语法将更改如下:
Swift 3.0:
斯威夫特 3.0:
@IBAction func buyTap(_ sender: Any, forEvent event: UIEvent) {
let myButton = sender as! UIButton
let touches = event.touches(for: myButton)
let touch = touches?.first
let touchPoint = touch?.location(in: myButton)
print("touchPoint\(touchPoint)")
}
回答by NeonBlueHair
For your overall coordinates (with reference to the screen), you need to create a CGPoint
that contains the coordinates of your touch. But to do that, you need to get that touch first. So start by getting the touch event, then by making that point using the locationInView
method. Now, depending on when you want to log the touch - when the user touches down, or when they lift their finger -, you have to implement this code in the touchesBegan
or touchesEnded
method. Let's say you do touchesEnded
, which passes an NSSet cales "touches" containing all the touch events.
对于您的整体坐标(参考屏幕),您需要创建一个CGPoint
包含您的触摸坐标的坐标。但要做到这一点,你需要先接触。因此,从获取触摸事件开始,然后使用该locationInView
方法提出这一点。现在,根据您想要记录触摸的时间——当用户触摸时,或者当他们抬起手指时——,您必须在touchesBegan
ortouchesEnded
方法中实现此代码。假设你这样做touchesEnded
,它传递一个包含所有触摸事件的 NSSet cales“触摸”。
UITouch *tap = [touches anyObject];
CGPoint touchPoint = [tap locationInView:self.view];
"touchPoint" will now contain the point at which the user lifts their finger. To print out the coordinates, you just access the x and y properties of that point:
“touchPoint”现在将包含用户抬起手指的点。要打印出坐标,您只需访问该点的 x 和 y 属性:
CGFloat pointX = touchPoint.x;
CGFloat pointY = touchPoint.y;
NSLog(@" Coordinates are: %f, %f ", pointX, pointY);
That should output the coordinates of the touch. Now to have it be referenced to whatever button you're using, I would suggest you just manually subtract the values for the button's coordinates from the point. It seems like a simple solution, and honestly I don't know a way of getting coordinates with reference to another object, unless you make a view based on that object, and pass it to locationInView
instead of self.view
.
那应该输出触摸的坐标。现在要将它引用到您使用的任何按钮,我建议您只需手动从该点减去按钮坐标的值。这似乎是一个简单的解决方案,老实说,我不知道参考另一个对象获取坐标的方法,除非您基于该对象创建视图,并将其传递给locationInView
而不是self.view
.
For more info on touches, there's a great set of tutorials here.
有关接触更多的信息,有一个很大的套教程在这里。