视图外的addSubview

Vin*_*ins 0 xcode objective-c uiviewcontroller

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

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

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

谢谢!

Mar*_*ark 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;
}
Run Code Online (Sandbox Code Playgroud)