2012-05-02 52 views
0

我有一个viewcontroller,通过“[self.view addSubview:secondView.view],”添加第二个视图。问题在于第二个视图是在一半以外添加的。addSubview视图外

secondView = [[SecondView alloc] initWithFrame: CGRectMake (-160, 0, 320, 460)]; 
[self.view addSubview: secondView.view]; " 

但是,我注意到,0(-160)之前的部分不是interagibile。这是正常的吗?有没有办法解决?

谢谢!

+0

最简单的解决方案是将两个视图放在透明容器视图中。你想要的是不可能检查[UIView]的文档(http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCCAICB)** hitTest:withEvent:** –

回答

1

我担心,鉴于UIResponder链的工作方式,你想要的不是直接可能的(superview只会传递给它的子视图,它认为它影响到它自己的事件)。另一方面,如果您确实需要将此视图放在其父框架之外,则可以将手势识别器(reference)关联到子视图。事实上,手势识别器是在正常的触摸事件分派之外进行处理的,它应该可以工作。

尝试此水龙头:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
[secondView addGestureRecognizer:singleTap]; 
+0

好吧,我会试试看,谢谢 – Vins

6

可以允许子视图通过重写pointInside:withEvent:父视图收到家长的范围之外的一面。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    BOOL pointInside = NO; 

    // step through our subviews' frames that exist out of our bounds 
    for (UIView *subview in self.subviews) 
    { 
     if(!CGRectContainsRect(self.bounds, subview.frame) && [subview pointInside:[self convertPoint:point toView:subview] withEvent:event]) 
     { 
      pointInside = YES; 
      break; 
     } 
    } 

    // now check inside the bounds 
    if(!pointInside) 
    { 
     pointInside = [super pointInside:point withEvent:event]; 
    } 

    return pointInside; 
} 
相关问题