如何禁用UIView掩码的用户交互

时间:2017-02-22 08:21:21

标签: ios uiview swift3 cashapelayer setuserinteractionenabled

TL; DR

我只是想知道是否有任何方法只能为视图的蒙版部分禁用用户交互。

以下是该方案:

  1. 我有两个观点查看A 查看B ,两者都是等宽等高

  2. 视图B位于视图A之上。

  3. 我在底部的查看B 上应用了一个掩码(CAShapeLayer),以查看查看A

  4. 由于查看B 有掩码,我只能看到查看A 的内容,但我无法与之互动。

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

1 个答案:

答案 0 :(得分:0)

让您将视图B视为清晰颜色并在项目UIView+ColorOfPoint中尝试UIView+ColorOfPoint.hUIView+ColorOfPoint.m个文件,并在视图控制器或视图的子类中使用以下代码。{{3我在 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;
}
&#13;
&#13;
&#13;