【问题标题】:how to know the view that is receiving the current gesture如何知道正在接收当前手势的视图
【发布时间】:2013-07-15 17:28:15
【问题描述】:

我有一个UIButton,当我点击其中一个非常小的子区域时,它只会对我的点击手势做出反应。我是assuming,当我点击按钮内的其他位置时,可能另一个视图正在吞噬我的手势。

问题是.. 有没有办法知道哪个UIView 正在接收水龙头?我知道我可以强行将所有手势处理程序附加到我所有的视图..但似乎工作量太大。有什么想法吗?

更新: 下面艾哈迈德的回答完美无缺。我还添加了 UIView 的recursiveDescription 以快速找到有问题的视图。

即如果我在 NSLog 行上放一个断点:

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    UIView *touchReceipientView =((UITouch *)[event.allTouches anyObject]).view;
    NSLog(@"View = %@ ",touchReceipientView);
}

我得到这个输出(例如):

(lldb) po [touchReceipientView recursiveDescription]
<UIView: 0x1fd16470; frame = (0 430; 320 40); layer = <CALayer: 0x1fd164d0>>
   | <UIButton: 0x1fd13030; frame = (297 0; 18 19); opaque = NO; tag = 11; layer = <CALayer: 0x1fd130f0>>
   |    | <UIImageView: 0x1fd13190; frame = (-41 -40.5; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x1fd131f0>>
   |    | <UIImageView: 0x1fd14d00; frame = (1 2; 16 15); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1fdc8e50>>

这让我立即识别出有问题的 UIView!

【问题讨论】:

  • 从应用的关键窗口开始循环浏览视图,然后向它们发送isFirstResponder。
  • gestureRecognizer.view
  • @Desdenova 如果我已经拥有了 gestureRecognizer 对象,我认为这个问题会很简单,不是吗?
  • @H2CO3.. 有趣.. 我不知道会发生什么.. 但无论如何我都会尝试一下,看看会发生什么.. 谢谢!
  • 我的错,以为你在使用识别器。

标签: iphone ios objective-c uiview uikit


【解决方案1】:

您可以继承 UIApplication 并查看谁在接收事件。

在 main.m 中,

@interface MyApplication : UIApplication

@end


@implementation MyApplication

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    NSLog(@"View = %@",  ((UITouch *)[event.allTouches anyObject]).view);
}

@end





int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([MyAppDelegate class]));
    }
}

【讨论】:

  • 我猜你的意思是@implementation MyApplication 对吧?
猜你喜欢
  • 1970-01-01
  • 2013-06-23
  • 2020-12-23
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多