0

TL;DR

I just want to know if there is any way to disable user interaction only for the masked portion of the view.

Here're the scenario:

  1. I have two views View A, View B, both are of Equal Width and Equal Height.

  2. View B is on top of View A.

  3. I applied a mask (CAShapeLayer) on View B at the bottom to see the contents (Two buttons) of View A

Since the View B has a mask, I am only able to see View A's contents, but I am not able to interact with it.

Any help would be appreciated. Thanks.

badhanganesh
  • 3,427
  • 3
  • 18
  • 39
  • 1
    I am not sure but I think my answer here : http://stackoverflow.com/a/37658235/6285303 is relevant – Sanman Feb 22 '17 at 08:25

1 Answers1

0

Make you View B as clear color and Try UIView+ColorOfPoint.h and UIView+ColorOfPoint.m files in your project UIView+ColorOfPoint and user below code in your view controller or subclass of your view .. Sample Project I am using below code in UIView subclass(custom class).

#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;
}
Uttam
  • 236
  • 2
  • 15