UIButton TouchUpInside触摸位置 [英] UIButton TouchUpInside Touch Location

查看:352
本文介绍了UIButton TouchUpInside触摸位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个大的 UIButton ,它是一个 UIButtonTypeCustom ,并且为<$调用按钮目标C $ C> UIControlEventTouchUpInside 。我的问题是如何确定 UIButton 触摸发生的位置。我想要这个信息,所以我可以从触摸位置显示一个弹出窗口。这是我尝试过的:

So I have a large UIButton, it is a UIButtonTypeCustom, and the button target is called for UIControlEventTouchUpInside. My question is how can I determine where in the UIButton the touch occured. I want this info so I can display a popup from the touch location. Here is what I've tried:

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

以及其他各种迭代。按钮的目标方法通过发件人从中获取信息:

and various other iterations. The button's target method get info from it via the sender:

    UIButton *button = sender;

所以我有什么方法可以使用类似的东西: button.touchUpLocation

So is there any way I could use something like: button.touchUpLocation?

我在网上看了一下,找不到类似的东西,所以提前谢谢。

I looked online and couldn't find anything similar to this so thanks in advance.

推荐答案

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

这是正确的想法,除了这个代码可能在视图控制器中的一个动作内,对吧?如果是这样,那么 self 指的是视图控制器而不是按钮。您应该将指向该按钮的指针传递到 -locationInView:

That's the right idea, except that this code is probably inside an action in your view controller, right? If so, then self refers to the view controller and not the button. You should be passing a pointer to the button into -locationInView:.

这是您可以尝试的经过测试的操作在您的视图控制器中:

Here's a tested action that you can try in your view controller:

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
    UIView *button = (UIView *)sender;
    UITouch *touch = [[event touchesForView:button] anyObject];
    CGPoint location = [touch locationInView:button];
    NSLog(@"Location in button: %f, %f", location.x, location.y);
}

这篇关于UIButton TouchUpInside触摸位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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