【问题标题】:Pass specific touch events in UIView/UIWindow to the next responder (sendEvent:/hitTest: doesn't work )将 UIView/UIWindow 中的特定触摸事件传递给下一个响应者(sendEvent:/hitTest: 不起作用)
【发布时间】:2012-04-10 15:40:02
【问题描述】:

我试图在不破坏其功能的情况下捕获 UIKeyboard 中的所有触摸事件。我试过了:

  • 命中测试:
  • UIGestureRecognizer
  • 在顶部添加一个 UIWindow 并将事件传递给下一个响应者

但是,这些都不起作用。

而且似乎不允许我们对 UIKeyboard 进行子类化。

你能想到任何可行的方法吗?

谢谢,

高峰

更新:

让我们简化问题:如何添加一个 UIView 或 UIWindow 将特定的触摸事件传递给下一个响应者(就像将 userInteractionEnabled 设置为 NO)?

这是我的代码(当然不能工作...):

   - (void)sendEvent:(UIEvent *)event {
    NSLog(@"%@",event);
    //if ([self eventIsNoteworthy:event]) [self extraEventHandling:event];
    [super sendEvent:event];  // Apple says you must always call this!
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    CGPoint hitPoint = [underneathButton convertPoint:point fromView:self];
    if ([underneathButton pointInside:hitPoint withEvent:event]) return underneathButton;
    NSLog(@"Called");
    return [super hitTest:point withEvent:event];
    //return mainWindow;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"Start");

    [self.nextResponder touchesBegan:touches withEvent:event];

}

【问题讨论】:

  • 当您说触摸事件时,您是指按下某些键的顺序吗?
  • @blake305 不,我是指触摸位置的CGPoints。
  • 你现在想构建自己的键盘,展示它,让它将按下的字母传递给 textField,然后 NSLog CGPoints?
  • @AMayes 我可以构建自己的键盘,但我需要在这里使用系统键盘(不同的语言)。我只想要 CGPoints ;-)

标签: iphone objective-c ios keyboard touch


【解决方案1】:

我不确定这是否可能。我想到了几件事。一种是自己重新创建UIKeyboard,然后以这种方式捕捉触摸。 Apple Docs 说明这是可能的。

另一种方法是重新查看您是如何在键盘上方添加 UIWindow 的。据我了解,键盘视图在您的应用程序上以某种方式分层,因此您可能没有将 UIWindow 置于键盘 (ref) 之上。该链接提供了一个解决方案,用于获取对键盘实际 UIView 的引用,从那里您希望将 UIWindow 放置到键盘视图的父级上。

为了提供帮助,您可以使用以下脚本转储所有视图以查看它们是如何分层的:

static void dumpViews(UIView* view, NSString *text, NSString *indent) 
{
    Class cl = [view class];
    NSString *classDescription = [cl description];
    while ([cl superclass]) 
    {
        cl = [cl superclass];
        classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    }

    if ([text compare:@""] == NSOrderedSame)
        NSLog(@"%@ %@", classDescription, NSStringFromCGRect(view.frame));
    else
        NSLog(@"%@ %@ %@", text, classDescription, NSStringFromCGRect(view.frame));

    for (NSUInteger i = 0; i < [view.subviews count]; i++)
    {
        UIView *subView = [view.subviews objectAtIndex:i];
        NSString *newIndent = [[NSString alloc] initWithFormat:@"  %@", indent];
        NSString *msg = [[NSString alloc] initWithFormat:@"%@%d:", newIndent, i];
        dumpViews(subView, msg, newIndent);
        [msg release];
        [newIndent release];
    }
}

这样称呼它:dumpViews([[UIApplication sharedApplication] keyWindow], @"", @"");

希望能有所帮助! :D

【讨论】:

  • 这是一个单行代码,您可以在调试器中使用它来实现相同的目的:po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]
  • @cbowns 太棒了!谢谢。
  • 感谢您的回答;-) 我确定我的 UIWindow 位于键盘顶部。我已经更新了问题。
【解决方案2】:

查看this answer 关于访问(包含)UIKeyboard 的私有视图层次结构。注意这在 iOS 中不受支持,Apple 拒绝执行此操作的应用程序。 (干扰 UIKit 内部的私有视图层次结构就等于使用私有 API。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    相关资源
    最近更新 更多