如何在自定义UIButton上获取抽头坐标? [英] How do I get the tap coordinates on a custom UIButton?

查看:96
本文介绍了如何在自定义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文件中的代码:

    - (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接口中创建它,请在参数字段中注明sender and event选项:)

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上获取抽头坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆