2013-05-11 55 views
0

我有一個有一個的CALayer面具一個UIView:的CALayer containsPoint與面膜不工作

// 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的我只是想,如果用戶點擊一個動作happend在 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; 
} 

幫助,好嗎?

回答

0

我的書給出了兩點建議。如果您知道面罩圖形的邊界路徑,則可以使用CGPathContainsPoint。或者,如果所討論的圖層在外部是透明的而在內部是不透明的,那麼您可以檢查點擊位置處的像素以查看它是否透明。

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

向下滾動到部分「命中測試圖紙」。