2016-10-03 191 views
2

我想在NSView中实现放大镜,它看起来像预览。
我有一些问题
1)它在NSView中实现吗?
2)如何实现它?
3)如何让视图上的放大镜?
感谢您的帮助 enter image description here如何在NSView中实现放大镜

回答

0

我已经做到了这一点,但我在目标-C做到了。

它其实需要一些工作来做到这一点,但在短:

  1. 子NSImageView
  2. 在子类中,重写,的mouseEntered,的mouseExited,并在的mouseEntered
  3. 的mouseMoved

NSPoint mouseLoc; 
BubbleNSImage * magImg; 
BubbleNSImage * magGlass; 
mouseLoc = [self convertPoint:[event locationInWindow] fromView:nil]; 

// Setup the magnifying glass image 
magGlass = [[NSImage alloc] initWithData: 
       [[NSImage imageNamed:@"magnifierCursor.png"] TIFFRepresentationUsingCompression:NSTIFFCompressionNone factor:0]]; 

// Get a 4x zoomed verion of a selection around where the mouse is 
// set the image for the image view 
if(0 == _zoomMode){ 
    magImg = [BubbleNSImage getPixelMultipliedNSImageRegionFromImage:self.image 
                 withMultiplier:4 
                 andSubRegion:NSMakeRect(mouseLoc.x - 17, 
                       self.image.size.height - mouseLoc.y - 17, 
                       34, 
                       34)]; 
}else{ 
    magImg = [BubbleNSImage getAppleMultipliedNSImageRegionNSImage:self.image 
                withMultiplier:4          
              andInterpolationMethod:_zoomMode  
                 andSubRegion:NSMakeRect(mouseLoc.x - 17, 
                       mouseLoc.y - 17, 
                       34, 
                       34)]; 
} 


// Mask the 4x zoomed version with a mask made for the zoomer 
[magImg maskWithImage:[NSImage imageNamed:@"magnifierCursorMask.png"] 
     andMaskOffset:NSMakePoint(0, 0) 
     andMaskColor:[NSColor colorWithRed:0 green:0 blue:0 alpha:1]]; 

// Add the 4x zoomed image behind the magnifying glass image 
[magGlass addImageBehind:(NSImage *)magImg 
       withOffset:NSMakePoint(16, 16) 
     clipToOrigImgSize:NO]; 

// Preseent the full image as the cursor 
[[[NSCursor alloc] initWithImage:(NSImage *)magGlass hotSpot:NSMakePoint(83, 83)] set]; 

上面的代码中的BubbleNSImage是NSImage的一个子类,它允许我对标准NSImage对象不可用的NSImages执行一系列操作。正如您所看到的,它允许我使用苹果的不同内插器进行缩放,以及使用我个人写入的像素复制功能进行缩放。它允许我设置缩放倍数,以及我希望缩放的子区域。

后来它可以让我执行掩码操作(这是我使用的是圆形放大镜的欢迎,并到圈外剪辑需要的是有用的。

然后它可以让我来执行叠加操作,其中I采取裁剪缩放图像,并把它背后我的放大镜图像。

最后,您需要初始化与新创建的图像一个新的光标对象,然后将其设置为当前光标。

  • 这个相同的代码需要在y我们重载的mouseMoved方法。
  • 需要设置光标回到正常的重载的mouseExited方法内部
  • Objective-C代码:

    -(void)mouseExited:(NSEvent *)event{ 
        [[NSCursor arrowCursor] set]; 
    } 
    

    有相当多的例子代码在那里进行了许多图像处理功能我有上面的高层算法。