简体   繁体   English

带有屏蔽的CALayer containsPoint不起作用

[英]CALayer containsPoint with mask not working

I have a UIView that have a CALayer mask: 我有一个具有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];

When the user later short press (UILongPressGestureRecognizer *) the UIView I just want a action to happend if the user taps in the UIView layer mask (for now containsPoint always returns YES): 当用户稍后短按(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;
}

Help, please? 请帮助?

My book gives two suggestions. 我的书有两个建议。 If you know the bounding path of the mask drawing, you can use CGPathContainsPoint . 如果知道蒙版图形的边界路径,则可以使用CGPathContainsPoint Or, if the layer in question is transparent on the outside and opaque on the inside, you can examine the pixel at the tapped location to see if it is transparent or not. 或者,如果所讨论的图层在外部是透明的,而在内部是不透明的,则可以检查点按的像素处的像素是否透明。

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

Scroll down to the section "Hit-testing for drawings". 向下滚动到“图纸命中测试”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM