为什么UINavigationBar会窃取触摸事件? [英] Why does UINavigationBar steal touch events?

查看:105
本文介绍了为什么UINavigationBar会窃取触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UIButton,UILabel被添加为子视图。按钮仅在我触摸顶部边界约15个点时执行给定选择器。当我在该区域上方点击时,没有任何反应。

I have a custom UIButton with UILabel added as subview. Button perform given selector only when I touch it about 15points lower of top bound. And when I tap above that area nothing happens.

我发现它并非由错误创建按钮和标签引起的,因为在我将按钮向下移动约15 px后,它可以正常工作。

I found out that it hasn't caused by wrong creation of button and label, because after I shift the button lower at about 15 px it works correctly.

更新我忘了说UINavigationBar下方的按钮和按钮上半部分的1/3没有触摸事件。

UPDATE I forgot to say that button located under the UINavigationBar and 1/3 of upper part of the button don't get touch events.

图片在这里

带有4个按钮的视图位于NavigationBar下方。当触摸顶部的篮球时,BackButton会触摸事件,当触摸顶部的Piano时,则右边的BarButton(如果存在)触摸。如果不存在,则什么也没发生。

View with 4 buttons is located under the NavigationBar. And when touch the "Basketball" in top, BackButton get touch event, and when touch "Piano" in top, then rightBarButton (if exists) get touch. If not exists, nothing happened.

我没有在App文档中找到这个记录的功能。

I didn't find this documented feature in App docs.

我还发现这个与我的问题相关的主题,但也没有答案。

Also I found this topic related to my problem, but there is no answer too.

推荐答案

我找到了答案这里(Apple开发者论坛)。

I found out the answer here(Apple Developer Forum).

2010年5月18日,Apple开发者技术支持部门的Keith(iPhone OS 3) ):

Keith at Apple Developer Technical Support, on 18th May 2010 (iPhone OS 3):


我建议您避免在导航栏或工具栏附近使用触敏UI。这些区域通常被称为slop因子,使得用户更容易在按钮上执行触摸事件而不会难以执行精确触摸。例如,对于UIButtons也是如此。

I recommend that you avoid having touch-sensitive UI in such close proximity to the nav bar or toolbar. These areas are typically known as "slop factors" making it easier for users to perform touch events on buttons without the difficulty of performing precision touches. This is also the case for UIButtons for example.

但是如果要在导航栏或工具栏接收之前捕获触摸事件,则可以创建UIWindow的子类并覆盖:
- (void)sendEvent:(UIEvent *)event;

But if you want to capture the touch event before the navigation bar or toolbar receives it, you can subclass UIWindow and override: -(void)sendEvent:(UIEvent *)event;

我也发现,当我触摸该区域时在UINavigationBar下,location.y定义为64,尽管它不是。
所以我做了这个:

Also I found out,that when I touch the area under the UINavigationBar, the location.y defined as 64,though it was not. So I made this:

CustomWindow.h

CustomWindow.h

@interface CustomWindow: UIWindow 
@end

CustomWindow.m

CustomWindow.m

@implementation CustomWindow
- (void) sendEvent:(UIEvent *)event
{       
  BOOL flag = YES;
  switch ([event type])
  {
   case UIEventTypeTouches:
        //[self catchUIEventTypeTouches: event]; perform if you need to do something with event         
        for (UITouch *touch in [event allTouches]) {
          if ([touch phase] == UITouchPhaseBegan) {
            for (int i=0; i<[self.subviews count]; i++) {
                //GET THE FINGER LOCATION ON THE SCREEN
                CGPoint location = [touch locationInView:[self.subviews objectAtIndex:i]];

                //REPORT THE TOUCH
                NSLog(@"[%@] touchesBegan (%i,%i)",  [[self.subviews objectAtIndex:i] class],(NSInteger) location.x, (NSInteger) location.y);
                if (((NSInteger)location.y) == 64) {
                    flag = NO;
                }
             }
           }  
        }

        break;      

   default:
        break;
  }
  if(!flag) return; //to do nothing

    /*IMPORTANT*/[super sendEvent:(UIEvent *)event];/*IMPORTANT*/
}

@end

在AppDelegate类中,我使用CustomWindow而不是UIWindow。

In AppDelegate class I use CustomWindow instead of UIWindow.

现在当我触摸导航栏下的区域时,没有任何反应。

Now when I touch area under navigation bar, nothing happens.

我的按钮仍然没有触摸事件,因为我不知道如何使用按钮将此事件(和更改坐标)发送到我的视图。

My buttons still don't get touch events,because I don't know how to send this event (and change coordinates) to my view with buttons.

这篇关于为什么UINavigationBar会窃取触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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