我正在尝试使用的NSScrollView有一些问题,可能需要一些帮助。我已经阅读了NSView、NSScrollView和其他一些指南和参考资料,以及这里的问题和cocoadev,但仍然无法解决这些问题。
(我的视图子类的代码可以在下面找到。)
我的总体目标是像KGChart这样的界面,这是一个针尖图表制造商,是个人电脑独家。(KGChart网站)
最后注意:我正在使用垃圾收集,我已经翻转了视图和它所在的窗口,如果这些事情真的很重要的话。
程序运行的图片:

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
[self setFrameSize:NSMakeSize(3000, 3000)];
return self;
}
- (void)drawRect:(NSRect)rect {
width = [self frame].size.width;
height = [self frame].size.height;
[[NSColor grayColor] setStroke];
NSBezierPath* drawingPath = [NSBezierPath bezierPath];
[NSBezierPath setDefaultLineWidth:2.0];
[self addDashStyleToPath:drawingPath];
int i;
for( i = 0 ; i <= width ; i=i+30) {
[drawingPath moveToPoint:NSMakePoint(i, 0)];
[drawingPath lineToPoint:NSMakePoint(i, height)];
}
for( i = 0 ; i <= height ; i=i+30) {
[drawingPath moveToPoint:NSMakePoint(0,i)];
[drawingPath lineToPoint:NSMakePoint(width, i)];
}
[drawingPath stroke];
}
-(void)drawStitch{
NSPoint thisPoint;
NSPoint fillPoint;
NSRect fillRect;
float thisPointX = [self calculatedItemBounds].origin.x + 5.0;
float thisPointY = [self calculatedItemBounds].origin.y - 8.0;
float fillPointX = [self calculatedItemBounds].origin.x + 1.0;
float fillPointY = [self calculatedItemBounds].origin.y + 1.0;
thisPoint.x = thisPointX;
thisPoint.y = thisPointY;
fillPoint.x = fillPointX;
fillPoint.y = fillPointY;
fillRect.origin.x = fillPoint.x;
fillRect.origin.y = fillPoint.y;
fillRect.size.width = 28;
fillRect.size.height = 28;
NSMutableDictionary *theAttributes;
theAttributes = [[NSMutableDictionary alloc] init];
[theAttributes setObject: [NSColor whiteColor] forKey:NSForegroundColorAttributeName];
[theAttributes setObject: [NSFont fontWithName:@"Helvetica" size: 32] forKey: NSFontAttributeName];
NSString *theString = @"+";
[self lockFocus];
[[NSColor blackColor] setFill];
[NSBezierPath setDefaultLineWidth:2.0];
[self addDashStyleToPath:[NSBezierPath bezierPathWithRect:[self calculatedItemBounds]]];
[NSBezierPath fillRect:fillRect];
NSNumber* myInteger = [NSNumber numberWithInt:310];
[myArray setObject:myInteger :location.x/30 :location.y/30];
NSLog(@"%@", [myArray objectInSection:location.x/30 :location.y/30]);
NSLog(@"%f, %f", location.x/30,location.y/30);
[theString drawAtPoint:thisPoint withAttributes:theAttributes];
[self displayIfNeededInRectIgnoringOpacity:[self calculatedItemBounds]];
[self unlockFocus];
[theAttributes release];
}很明显,我对此很不在行,所以谢谢你的帮助!
发布于 2011-07-13 03:08:42
我相信我把这个修好了。为了使其工作,我不得不重新安排我的代码在drawRect中进行绘图,而不是在我的drawStitch方法中使用lockFocus (我需要阅读MVC设计)。另外,滚动视图似乎只重画了最新的bezier路径,因此要进行多次“缝合”,我需要在路径后面添加rects。
发布于 2011-07-06 02:42:10
默认情况下,NSClipView的内容视图( NSScrollView的一个实例)通过在滚动时复制其现有呈现的图像来进行一些性能优化。大多数情况下,这很好,但是当您绘制一个复杂的自定义视图时,它通常是不需要的行为,因此您应该关闭它。
如果您使用nib来定义您的UI,只需选择IB中的滚动视图并取消选中“”复选框。
您可以使用‑setCopiesOnScroll:的NSClipView方法以编程方式设置它。
[[scrollView contentView] setCopiesOnScroll:NO];https://stackoverflow.com/questions/6590746
复制相似问题