2013-03-26 80 views
0

我划分了一个自定义视图,该视图被拖入Interface Builder中的一个窗口中。当鼠标进入视图的边界时,我希望视图的高度发生改变。我的问题是高度变化是向上而不是向下。我尝试用(BOOL)isFlipped翻转视图的坐标,但它对高度变化的方向没有任何影响。任何帮助我如何改变向下的高度?带有翻转绘图坐标的NSView高度方向

#import "ViewA.h" 

@implementation ViewA 

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] 
                    options:(NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways) 
                     owner:self 
                    userInfo:nil]; 
     [self addTrackingArea:trackingArea]; 

    } 

    return self; 
} 

- (void)drawRect:(NSRect)dirtyRect 
{ 
    [[NSColor redColor] setFill]; 
    NSRectFill(dirtyRect); 
} 

- (BOOL)isFlipped { 
    return YES; 
} 


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

    NSRect rect = self.frame; 
    rect.size.height = 120; 
    self.frame = rect; 
} 

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

    NSRect rect = self.frame; 
    rect.size.height = 90; 
    self.frame = rect; 
} 

@end 

回答

0

这是因为Cocoa的坐标系统从左下角开始。即协调(0,0)位于屏幕/父视图的左下角。所以y坐标(高度)的增量会增加尺寸。你应该做的是当你增加高度,向下移动原点,例如如果你想增加/减少框架高度到90;

CGFloat heightDiff = 90 -self.frame.size.height; 

NSRect rect = self.frame; 

rect.size.height = 90; 

rect.origin.y -= heightDiff; 

self.frame = rect; 

应该这样做。

覆盖父视图的isFlipped而不是预期行为的视图本身的方法。

举例来说,如果你将它添加到的window内容来看,子类的窗口contentView并覆盖其isFlipped方法返回YES

+0

但我实现了isFlipped方法将坐标系翻转到左上角。 – wigging 2013-03-26 04:38:52

+0

'isFlipped'方法不适用于直接设置。您可以在子类中重写该方法,以便在使用翻转坐标系时返回YES。只要将'isFlipped'设置为YES就不会翻转坐标系统。 – Rakesh 2013-03-26 04:43:19

+0

@Gavin:我编辑了答案以包含解释。 – Rakesh 2013-03-26 04:48:55