问题描述
我在 iPad 上使用 XCode 4.4 为 iOS 5 开发,并在创建自定义按钮时使用 Storyboard 布局.
I'm using XCode 4.4 developing for iOS 5 on an iPad and am using the Storyboard layout when creating my custom button.
我的触摸事件正常工作和记录,但现在我想在我的自定义按钮上获取点击的 x/y 坐标.
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.
如果可能,我希望坐标相对于自定义按钮,而不是相对于整个 iPad 屏幕.
If possible, I'd like the coordinates to be relative to the custom button instead of relative to the entire iPad screen.
这是我在 .h 文件中的代码:
Here's my code in the .h file:
- (IBAction)getButtonClick:(id)sender;
和我在 .m 文件中的代码:
and my code in the .m file:
- (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?
我尝试了一些来自互联网的不同示例,但当它在日志框中显示一堆数字(可能是坐标)时,它们总是冻结.我对 iOS 开发非常陌生,所以请让它尽可能简单.谢谢!
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!
推荐答案
要获取触摸位置,您可以使用按钮操作方法的另一种变体:myAction:forEvent:
(如果您从 IB 接口创建它注意参数字段中的发送者和事件"选项:)
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: )
然后在您的动作处理程序中,您可以从事件参数中获取触摸位置,例如:
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));
}
这篇关于如何获取自定义 UIButton 上的点击坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!