简体   繁体   English

如何在自定义UIButton上获取分接头坐标?

[英]How do I get the tap coordinates on a custom UIButton?

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. 我是iOS开发的新手,所以请尽可能简单。 Thanks! 谢谢!

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));
}

Incase of Swift 3.0 the accepted answer works same except syntax will be changed as follows: 除了Swift 3.0之外,接受的答案是相同的,除了语法将改变如下:

Swift 3.0: Swift 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)")
  }

For your overall coordinates (with reference to the screen), you need to create a CGPoint that contains the coordinates of your touch. 对于您的整体坐标(参考屏幕),您需要创建一个包含触摸坐标的CGPoint 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. 因此,首先获取触摸事件,然后使用locationInView方法创建该点。 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. 现在,根据您要记录触摸的时间 - 当用户触摸时,或者当他们抬起手指时 - ,您必须在touchesBegantouchesEnded方法中实现此代码。 Let's say you do touchesEnded , which passes an NSSet cales "touches" containing all the touch events. 假设你做了touchesEnded ,它传递了包含所有触摸事件的NSSet cales“touches”。

UITouch *tap = [touches anyObject];
CGPoint touchPoint = [tap locationInView:self.view];

"touchPoint" will now contain the point at which the user lifts their finger. “touchPoint”现在将包含用户抬起手指的点。 To print out the coordinates, you just access the x and y properties of that point: 要打印出坐标,只需访问该点的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 . 有关接触更多的信息,有一个很大的套教程在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM