CALayer containsPoint,掩码不起作用

时间:2013-05-11 22:24:52

标签: ios ios6 calayer

我有一个带有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时,如果用户点击 in 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;
}

请帮帮忙?

1 个答案:

答案 0 :(得分:0)

我的书提出了两点建议。如果您知道蒙版绘图的边界路径,则可以使用CGPathContainsPoint。或者,如果所讨论的图层在外部是透明的,而在内部是不透明的,则可以检查点击位置处的像素以查看它是否透明。

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

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