Objective c 是否删除由NSRectFill完成的图形?
我有一个Objective c 是否删除由NSRectFill完成的图形?,objective-c,cocoa,drawing,Objective C,Cocoa,Drawing,我有一个NSBox,在其中我用NSRectFill()绘制小矩形。我的代码如下所示: for (int i = 0; i <= 100; i++){ int x = (rand() % 640) + 20; int y = (rand() % 315) + 196; array[i] = NSMakeRect(x, y, 4, 4); NSRectFill(array[i]); } for(int i=0;i听起来像是在drawRect之外绘制:。如
NSBox
,在其中我用NSRectFill()
绘制小矩形。我的代码如下所示:
for (int i = 0; i <= 100; i++){
int x = (rand() % 640) + 20;
int y = (rand() % 315) + 196;
array[i] = NSMakeRect(x, y, 4, 4);
NSRectFill(array[i]);
}
for(int i=0;i听起来像是在drawRect之外绘制:。如果是这种情况,请将绘图代码移动到视图(框或某个子视图)中drawRect:method。否则,您的绘图将像您看到的那样被Cocoa绘图系统踩踏。您还需要使用计时器或动画,而不是循环来重复绘图。我最近为试图对圆进行类似操作的人编写了一个示例程序。我采用的方法是创建一个cir数组cle规范,并在drawRect中绘制它们。它工作得很好。也许它会有帮助。如果你想要整个项目,你可以下载它
还值得注意的是,即使第二个for
循环位于drawRect:
中,性能选择器:withObject:afterDelay:
消息意味着executeFrame
消息在之后发送和接收(即在外部)drawRect:
。向视图发送display
消息是正确的解决方案,这是非常罕见的。通常,您只需设置其needsDisplay
,让AppKit告诉它在正确的时间绘制,并在AppKit告诉它时绘制视图的所有图形,而在任何其他时间都不需要。
for (int i = 0; i <= 10; i++) {
[self performSelector:@selector(executeFrame) withObject:nil afterDelay:(.05*i)];
}
@implementation CircleView
@synthesize maxCircles, circleSize;
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
maxCircles = 1000;
circles = [[NSMutableArray alloc] initWithCapacity:maxCircles];
}
return self;
}
- (void)dealloc {
[circles release];
[super dealloc];
}
- (void)drawRect:(NSRect)dirtyRect {
NSArray *myCircles;
@synchronized(circles) {
myCircles = [circles copy];
}
NSRect bounds = [self bounds];
NSRect circleBounds;
for (NSDictionary *circleSpecs in myCircles) {
NSColor *color = [circleSpecs objectForKey:colorKey];
float size = [[circleSpecs objectForKey:sizeKey] floatValue];
NSPoint origin = NSPointFromString([circleSpecs objectForKey:originKey]);
circleBounds.size.width = size * bounds.size.width;
circleBounds.size.height = size * bounds.size.height;
circleBounds.origin.x = origin.x * bounds.size.width - (circleBounds.size.width / 2);
circleBounds.origin.y = origin.y * bounds.size.height - (circleBounds.size.height / 2);
NSBezierPath *drawingPath = [NSBezierPath bezierPath];
[color set];
[drawingPath appendBezierPathWithOvalInRect:circleBounds];
[drawingPath fill];
}
[myCircles release];
}
#pragma mark Public Methods
-(void)makeMoreCircles:(BOOL)flag {
if (flag) {
circleTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(makeACircle:) userInfo:nil repeats:YES];
}
else {
[circleTimer invalidate];
}
}
-(void)makeACircle:(NSTimer*)theTimer {
// Calculate a random color
NSColor *color;
color = [NSColor colorWithCalibratedRed:(arc4random() % 255) / 255.0
green:(arc4random() % 255) / 255.0
blue:(arc4random() % 255) / 255.0
alpha:(arc4random() % 255) / 255.0];
//Calculate a random origin from 0 to 1
NSPoint origin;
origin.x = (double)arc4random() / (double)0xFFFFFFFF;
origin.y = (double)arc4random() / (double)0xFFFFFFFF;
NSDictionary *circleSpecs = [NSDictionary dictionaryWithObjectsAndKeys:color, colorKey,
[NSNumber numberWithFloat:circleSize], sizeKey,
NSStringFromPoint(origin), originKey,
nil];
@synchronized(circles) {
[circles addObject:circleSpecs];
if ([circles count] > maxCircles) {
[circles removeObjectsInRange:NSMakeRange(0, [circles count] - maxCircles)];
}
}
[self setNeedsDisplay:YES];
}
@end