我相信这是不可能没有子类。如果有人能证明我错了,我很乐意看到他们的解决方案。无论如何,这是我如何解决它:
首先,我的子类视图A
,我称之为子类ParentView
。 ParentView需要指向B1
和B2
。我还添加了一个稍后用于存储当前选定视图的额外指针。然后,我重写hitTest
:
@interface ParentView : UIView
@property (nonatomic, strong) UIView* viewB1;
@property (nonatomic, strong) UIView* viewB2;
@property (nonatomic, strong) UIView* currentlyTouchedView;
@end
@implementation ParentView
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (self.currentlyTouchedView == nil) {
if (CGRectContainsPoint(self.viewB1.frame, point)) {
self.currentlyTouchedView = viewB1;
}else if(CGRectContainsPoint(self.viewB2.frame, point)) {
self.currentlyTouchedView = viewB2;
}
}else {
if (!CGRectContainsPoint(self.currentlyTouchedView.frame, point)) {
return nil;
}
}
return [super hitTest:point withEvent:event];
}
@end
因此,这段代码的含义是什么,它存储的是第一次碰到的看法。如果已经存储了一个,则返回nil hitTest
,这会消耗任何其他触摸事件。
现在,currentlyTouchedView
的位置回到零位?为此,我继承UIWindow
并覆盖sendEvent
,检查所有的触摸已经结束:
@implementation EventOverrideWindow
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
BOOL allTouchesEnded = YES;
for (UITouch* touch in event.allTouches) {
if (touch.phase != UITouchPhaseCancelled && touch.phase != UITouchPhaseEnded) {
allTouchesEnded = NO;
break;
}
}
if (allTouchesEnded) {
ParentView* parentView = ((AppDelegate*)[UIApplication sharedApplication].delegate).parentView;
parentView.currentlyTouchedView = nil;
}
}
@end
我知道这是不是一个非常优雅的解决方案,如果有人有更好的想法,我会很感激。
来源
2012-11-06 00:24:55
avf
对视图禁用多点触控:) – deanWombourne