1

我正在创建一个 UIview 并以这种方式对其应用蒙版:

baseView = [[ViewTutorialSubclass alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//self.view = baseView;
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
baseView.userInteractionEnabled = YES;
baseView.alpha = 0.7;

mask = [[CAShapeLayer alloc] init];
mask.frame = baseView.layer.bounds;
CGRect biggerRect = CGRectMake(mask.frame.origin.x, mask.frame.origin.y, mask.frame.size.width, mask.frame.size.height);
CGRect smallerRect = CGRectMake(7.0f, 160.0f, 590.0f, 43.0f);

UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath moveToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];

[maskPath moveToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];

mask.path = maskPath.CGPath;
[mask setFillRule:kCAFillRuleEvenOdd];
mask.fillColor = [[UIColor blackColor] CGColor];
baseView.layer.mask = mask;

在我的 ViewTutorialSubclass.m 我有:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *v in self.subviews) {
    CGPoint localPoint = [v convertPoint:point fromView:self];
    if (v.alpha > 0.8 && [v pointInside:localPoint withEvent:event])
        return YES;
}
return NO;
}

这个想法是,如果用户使用掩码(非常透明)触摸 UIview 的任何部分,则所有用户触摸都会传递到该 UIview 下面的 UIview,而如果用户触摸其他任何地方,则触摸将应用于当前 UIview。我该怎么做?pointInside 是最好的解决方案吗?

4

1 回答 1

3

包含遮罩层的视图类应覆盖 pointInside 并根据遮罩的状态返回 YES 或 NO。如果掩码总是一个矩形,那么你可以在矩形测试中做一个点,但我敢打赌这不是你想要的。您希望此测试适用于任意形状的面罩。设置(在 dispatch_once 中)一个像素宽的 CGBitmapContext。在 pointInside:withEvent: 中,将遮罩层渲染到位图上下文中,看看它是否绘制了任何东西。这是一个例子:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    static CGContextRef context;
    static CGColorSpaceRef rgbColorSpace;
    static unsigned char bitmapData[4];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        rgbColorSpace = CGColorSpaceCreateDeviceRGB();
        context = CGBitmapContextCreate(bitmapData, 1, 1, 8, 4, rgbColorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
    });
    if ([super pointInside:point withEvent:event]) {
        CGContextTranslateCTM(context, -point.x, -point.y);
        [self.layer.mask renderInContext:context];
        CGContextTranslateCTM(context, point.x, point.y);
        BOOL result = (bitmapData[3] > 0);
        memset(bitmapData, 0, sizeof(bitmapData));
        return result;
    }
    return NO;
}

编辑:如果您将 kCGImageAlphaPremultipliedLast (单独)传递给CGBitmapContextCreate. 参数的类型应该是CGBitmapInfokCGImageAlphaPremultipliedLast来自枚举类型CGImageAlphaInfo。要消除警告,您可以结合枚举类型中kCGImageAlphaPremultipliedLast的值,例如. 无论如何,这种方式更好,因为新代码明确表明我们正在发送一般的“位图信息”,但我们对与如何处理 alpha 通道相关的位图信息感兴趣。CGBitmapInfokCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast

于 2013-03-16T23:53:06.380 回答