2012-09-21 75 views
-1

我有一個NSCustomView NSImage放置在它的自定義矩形。如何檢查我的mousedown事件中的某個點是否位於此圖像內?檢查圖像是否包含鼠標位置

事情是這樣的:

- (void)mouseDown:(NSEvent *)theEvent { 
     NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; 
     if ([myImage containspoint:point]) { 
      ...do stuff... 
+0

我知道我可以使用一個矩形,但圖像具有透明不應包含這一觀點的領域 – RMaggi

回答

0

可以檢查你點擊的點是圖像的矩形與NSPointInRect這樣的:

-(void)mouseDown:(NSEvent *)theEvent { 

    NSPoint point = [self convertPoint: [theEvent locationInWindow] fromView: nil]; 

    if (NSPointInRect(point, imageRect)) { 
     //Do stuff here 
    } 
}