1

我在导航栏上的 appdelegate 文件中显示 UILabel(不完全在导航栏上)。它显示完美。但是没有触摸事件正在调用它。触摸仅适用于状态栏。请帮忙。提前致谢。

这是我的代码。

CGRect frame=[[UIApplication sharedApplication] statusBarFrame];
backgroundImageView=[[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height, 320, 44)];        
backgroundImageView.tag=50;
[backgroundImageView setTextAlignment:UITextAlignmentCenter];
[backgroundImageView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"music.png"]]];
[self.window addSubview:backgroundImageView];

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{   
    NSLog(@"in touched");
}
4

1 回答 1

0

正如泰勒在他的回答中所说:

UIWindow 中有一个方便的包罗万象的方法,称为 sendEvent:,它可以查看事件处理管道开始附近的每个事件。如果您想做任何非标准的附加事件处理,这是放置它的好地方。

要拦截所有触摸,您应该为控制器视图使用 UIView 子类并覆盖其中的 hitTest:withEvent: 方法:

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        touchedView = [super hitTest:point withEvent:event];
        NSSet* touches = [event allTouches];
        // handle touches if you need
        return touchedView;
        }

或者

2)您可以使用:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(anyMethod)];  
[imageView addGestureRecognizer:tgr];  
[tgr release];  

Before doing this, enable user interaction of UIimageView.
于 2013-01-24T11:26:03.770 回答