繁体   English   中英

带有屏蔽的CALayer containsPoint不起作用

[英]CALayer containsPoint with mask not working

我有一个具有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;
}

请帮助?

我的书有两个建议。 如果知道蒙版图形的边界路径,则可以使用CGPathContainsPoint 或者,如果所讨论的图层在外部是透明的,而在内部是不透明的,则可以检查点按的像素处的像素是否透明。

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

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

暂无
暂无

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

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