如何禁用 UIView 面具的用户交互



tl; dr

我只想知道是否有任何方法可以禁用用户交互。

这是场景:

  1. 我有两个视图查看a 查看b ,两个都是等于宽度 and 等高的高度

  2. 视图b在视图a。

  3. 的顶部
  4. 我在>上使用掩码(CASHAPELAYER) on view b 在底部查看的内容(两个按钮)>

由于 view b 有一个掩码,因此我只能看到 view a 的内容,但是我无法与之交互。

任何帮助将不胜感激。谢谢。

使您将b视为清晰的颜色,然后在您的项目uiview colorofpoint中尝试 UIView+ColorOfPoint.hUIView+ColorOfPoint.m文件,以及视图控制器或视图的子类中的代码。下面的代码 uiview子类(自定义类)。

#pragma mark - Hit testing
- (BOOL)isAlphaVisibleAtPoint:(CGPoint)point forImage:(UIView *)view
{
    // Correct point to take into account that the image does not have to be the same size
    // as the button. See https://github.com/ole/OBShapedButton/issues/1
    CGSize iSize = view.bounds.size;
    CGSize bSize = self.bounds.size;
    point.x *= (bSize.width != 0) ? (iSize.width / bSize.width) : 1;
    point.y *= (bSize.height != 0) ? (iSize.height / bSize.height) : 1;
    
    UIColor *pixelColor = [view colorOfPoint:point];
    CGFloat alpha = 0.0;
    
    if ([pixelColor respondsToSelector:@selector(getRed:green:blue:alpha:)])
    {
        // available from iOS 5.0
        [pixelColor getRed:NULL green:NULL blue:NULL alpha:&alpha];
    }
    else
    {
        // for iOS < 5.0
        // In iOS 6.1 this code is not working in release mode, it works only in debug
        // CGColorGetAlpha always return 0.
        CGColorRef cgPixelColor = [pixelColor CGColor];
        alpha = CGColorGetAlpha(cgPixelColor);
    }
    return alpha >= kAlphaVisibleThreshold;
}
// UIView uses this method in hitTest:withEvent: to determine which subview should receive a touch event.
// If pointInside:withEvent: returns YES, then the subview’s hierarchy is traversed; otherwise, its branch
// of the view hierarchy is ignored.
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    // Return NO if even super returns NO (i.e., if point lies outside our bounds)
    BOOL superResult = [super pointInside:point withEvent:event];
    if (!superResult) {
        return superResult;
    }
    
    // Don't check again if we just queried the same point
    // (because pointInside:withEvent: gets often called multiple times)
    if (CGPointEqualToPoint(point, self.previousTouchPoint)) {
        return self.previousTouchHitTestResponse;
    } else {
        self.previousTouchPoint = point;
    }
    
    BOOL response = NO;
    
    if (self == nil) {
        response = YES;
    }
    else if (self!= nil) {
        response = [self isAlphaVisibleAtPoint:point forImage:self];
    }
    else {
        if ([self isAlphaVisibleAtPoint:point forImage:self]) {
            response = YES;
        } else {
            response = [self isAlphaVisibleAtPoint:point forImage:self];
        }
    }
    
    self.previousTouchHitTestResponse = response;
    return response;
}

- (void)resetHitTestCache
{
    self.previousTouchPoint = CGPointMake(CGFLOAT_MIN, CGFLOAT_MIN);
    self.previousTouchHitTestResponse = NO;
}

最新更新