0

我有一个带有 CALayer 掩码的 UIView:

// Getting the right mask image
UIImage *myimage = [UIImage imageNamed:[NSString stringWithFormat:@"img%d", imageIndex]];

// Scaling image to fit UIView
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
[myimage drawInRect:self.bounds];
myimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self.layer setMasksToBounds:YES];

// Setting mask
UIImage *_maskingImage = myimage;
CALayer *_maskingLayer = [CALayer layer];
_maskingLayer.frame = self.bounds;
[_maskingLayer setContents:(id)[_maskingImage CGImage]];
[self.layer setMask:_maskingLayer];

当用户稍后短按 (UILongPressGestureRecognizer *) UIView 时,我只想在用户点击 UIView 图层蒙版时发生一个动作现在 containsPoint 总是返回 YES):

// Object is a UIView
CALayer *layer = [Object.layer mask];

// Sender is a UILongPressGestureRecognizer
location2 = [sender locationInView:Object];

// The position is correct
NSLog(@"%@", NSStringFromCGPoint(location2));

if ([layer containsPoint:location2]){
     NSLog(@"HELLO WORLD!");
     return;
}

请帮忙?

4

1 回答 1

0

我的书给出了两个建议。如果您知道遮罩绘图的边界路径,则可以使用CGPathContainsPoint. 或者,如果有问题的图层外部透明而内部不透明,您可以检查点击位置的像素,看它是否透明。

http://www.aeth.com/iOSBook/ch18.html#_hit_testing

向下滚动到“图纸的命中测试”部分。

于 2013-05-11T23:20:01.917 回答