2012-05-02 54 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; 
} 
相關問題