如何获取自定义 UIButton 上的点击坐标?

How do I get the tap coordinates on a custom UIButton?(如何获取自定义 UIButton 上的点击坐标?)
本文介绍了如何获取自定义 UIButton 上的点击坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 上的点击坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)