我想在当前窗口上创建一个透明的遮罩视图,它只跟踪触摸事件,并将它们传递给下面的可见视图。但是,如果我将userInteractionEnabled=YES设置为此掩码,则会阻塞事件,并且不会在下面传递。
有没有什么方法可以防止这个视图阻塞事件,或者手动传递下面的事件?
谢谢,
发布于 2011-02-17 13:49:16
我最近刚刚为我的一个应用程序做了这件事,结果证明它非常简单。
准备好创建UIView的子类:
我称我的Mask View为catcher视图,协议如下所示:
@interface CatcherView : UIView {
UIView *viewBelow;
}
@property(nonatomic,retain)UIView *viewBelow;
@end在这里,你只是继承了UIView,并在下面引用了视图。
在实现上,您需要完全实现至少4个方法来传递对视图的触动,以下是这些方法的外观:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch Began");
[self.viewBelow touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch Moved");
[self.viewBelow touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch Ended");
[self.viewBelow touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch Cancelled");
//Not necessary for my app but you just need to forward it to your view bellow.
}只需记住在创建视图时设置一个或多个下面的视图;将背景颜色设置为透明也是非常重要的,这样它就可以充当蒙版。THis看起来是这样的:
CatcherView *catchView=[[CatcherView alloc] initWithFrame:[self.view bounds]];
catchView.backgroundColor=[UIColor clearColor];
catchView.viewBelow=myViewBellow;
[self.view addSubview:catchView];希望对你有帮助,如果你需要更多的信息,请发表评论。
发布于 2011-02-17 19:05:19
-hitTest:withEvent:消息来确定事件的目标视图因此,如果您在适当的高视图中覆盖-[NSView hitTest:withEvent:] (可能通过使用自定义窗口!)您可以记录所有传入的事件,并调用super使它们的行为正常。
https://stackoverflow.com/questions/5025451
复制相似问题