问题与setPatternPhase:在NSSplitView的NSColor使用colorWithPatternImage: [英] Issue with setPatternPhase: in an NSSplitView for NSColor using colorWithPatternImage:

查看:66
本文介绍了问题与setPatternPhase:在NSSplitView的NSColor使用colorWithPatternImage:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过,没有找到任何关于这个特殊问题。我有一个垂直的NSSplitView的窗口。在splitview的右视图中,我有一个NSBox对象设置来填充视图的内容。我使用setContentView加载自定义nibs:交换视图到分割视图右侧的NSBox。在我的自定义视图中,我打算使用NSColor对象填充视图,该对象使用NSImage来对背景进行图案化。所有这一切都按预期工作。我使用的代码基于本文: http ://www.mere-mortal-software.com/blog/details.php?d = 2007-01-08 将图案的相位设置为左上角:

I have searched around and haven't found anything regarding this particular problem. I have a window with a vertical NSSplitView. Within the right view of the splitview, I have an NSBox object set to fill the contents of the view. I am loading in custom nibs using setContentView: to swap in views to the NSBox on the righthand side of the splitview. In my custom views I'm tying to fill the views with an NSColor object that uses a NSImage to pattern the background. All of this is working as expected. I am using the code based on this article: http://www.mere-mortal-software.com/blog/details.php?d=2007-01-08 to set the phase of the pattern to the top-left corner:

[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setPatternPhase: NSMakePoint( [self bounds].origin.x, 
                                                                 [self bounds].size.height )];
[[self leatherColor] set];
NSRectFill( [self bounds] );

[[NSGraphicsContext currentContext] restoreGraphicsState];

,意图是当NSBox的左上角的模式看起来保持不变(通过拆分视图移动或窗口本身被调整大小)将重新调整大小。我已经在一个精简的测试应用程序中实现这一点,只有一个窗口和自定义视图,它的行为正如预期。

with the intention that the pattern appears to remain constant in the upper lefthand corner of the NSBox when the views are re-sized as would be the expected behavior (via the splitview moving or the window itself being resized). I have implemented this in a stripped-down test app with just a window and a custom view and it behaves as expected.

在我的项目与splitview然而,行为类似于模式相位被设置在整个窗口的左上角,而不是右上半部分的splitview(即它应该移动与调整splitview的大小)它的行为就像揭示一个更大的图像我很确定这是一个多坐标空间的问题,我误解了NSSplitView的坐标如何工作,或者与视图层次结构有关从NSGraphicsContext的文档,在setPatternPhase:方法说:例如,将模式阶段设置为(2,3)具有在默认用户空间中将模式单元格的开始移动到点(2,3)的效果。默认用户空间将与边界我的自定义视图,或者可能是,我正在创建的点基于右侧视图坐标和setPatternPhase实现它的spitview的全宽(包括左半部分)?

In my project with the splitview however, it is behaving like the patternPhase is being set on the upper lefthand corner of the entire window instead of the upper lefthand corner of the right-half of the splitview (i.e. it should shift with resizing the splitview) It behaves like it's "revealing a larger image instead of shifting with it. I'm pretty certain this is an issue with multiple coordinate spaces, me misunderstanding how the coordinates of an NSSplitView work, or something to do with the view hierarchy. From the documentation on NSGraphicsContext, in the setPatternPhase: method it says: "For example, setting the pattern phase to (2,3) has the effect of moving the start of pattern cell tiling to the point (2,3) in default user space." Will the default user space correspond with the bounds of my custom view, or could it be that I am creating the point based on the right side view set of coordinates and the setPatternPhase implements it for the full width of the spitview (including the left-half)?

您可以提供的任何建议/建议/帮助将非常感谢。

Any suggestions/advice/help you could provide would be greatly appreciated.

编辑:我已经上传了一个项目的副本可以看到以下行为: http://dl.dropbox.com/u/6679821/ViewSwitcher。 zip

I have uploaded a copy of the project so you can see the behavior: http://dl.dropbox.com/u/6679821/ViewSwitcher.zip

推荐答案

您需要根据视图在窗口中的位置来调整相位。我使用这个类别在 NSView

You need to adjust the phase depending on where the view sits in the window. I use this category on NSView:

@implementation NSView (RKAdditions)
- (void)rk_drawPatternImage:(NSColor*)patternColor inRect:(NSRect)rect
{
    [self rk_drawPatternImage:patternColor inBezierPath:[NSBezierPath bezierPathWithRect:rect]];
}


- (void)rk_drawPatternImage:(NSColor*)patternColor inBezierPath:(NSBezierPath*)path
{
    [NSGraphicsContext saveGraphicsState];

    CGFloat yOffset = NSMaxY([self convertRect:self.bounds toView:nil]);
    CGFloat xOffset = NSMinX([self convertRect:self.bounds toView:nil]);
    [[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(xOffset, yOffset)];

    [patternColor set];
    [path fill];
    [NSGraphicsContext restoreGraphicsState];
}

@end

修改 -drawRect:,如下所示:

-(void) drawRect: (NSRect)dirtyRect
{
    [self rk_drawPatternImage:[self leatherColor] inRect:self.bounds];
}

这篇关于问题与setPatternPhase:在NSSplitView的NSColor使用colorWithPatternImage:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆