2011-07-01 6 views
2

NSBox, 그 중 작은 사각형을 그리는 중 NSRectFill()이 있습니다. 내 코드는 다음과 같습니다.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 루프는 격자 내에 임의로 배치 된 직사각형을 100 개 생성합니다.

for (int i = 0; i <= 10; i++) {  

    [self performSelector:@selector(executeFrame) withObject:nil afterDelay:(.05*i)]; 

    } 

루프의 첫 번째는 유일한 것입니다 : 내가 뭘하려고 한 것은이 코드로, 반복 실행이 코드에 의해 생성 애니메이션의 종류, 생성 무작위로 나타나는 사각형의 애니메이션을 만드는 것입니다 그런데, executeFrame 함수 안에서. 그래서, 제가해야 할 일은 프레임 사이의 모든 사각형을 지우는 것입니다. 그래서 그것들의 수는 동일하게 유지되고 움직이는 것처럼 보입니다. executeFrame를 호출하기 전에 [myNsBox display];을 호출하여 배경을 다시 그려 보았습니다.하지만 사각형이 그려지지 않은 것처럼 보였습니다. 같은 것을 한 후에 호출하면 디스플레이 대신에 setNeedsDisplay으로 전환합니다. 나는 이것을 이해할 수 없으며, 어떤 도움을 주시면 감사하겠습니다.

덧붙여서, 추가적으로, 프레임을 실행하기 위해 코드를 실행하려고 할 때, 그 사이에 사각형을 지우지 않고, 100 개의 사각형이 그려지는 것입니다. 제가 1000 그루의 그림을 그리도록 요청했거나, 10,000 장을 그렸습니다. 그럼에도 불구하고 창을 나가서 다시 돌아 오면 (즉, 시간은 여기에 요인이되지 않습니다.) 페이지가 업데이트되고 사각형이 있습니다. 나는 이것을 이상한 방법으로 작업 한 [box setNeedsDisplayInRect:array[i]];으로 극복하려고 시도하여 모든 프레임을 업데이트하지만 사각형의 일부를 삭제했습니다. 이것에있는 어떤 도움든지 또한 평가 될 것입니다.

+1

그것은 당신의보기를 보내는 것은'display' 메시지가 올바른 해결책이 매우 드문 다운로드 할 수 있습니다. 일반적으로 'needsDisplay'를 설정하고 AppKit이 적절한 시간에 그리도록 지시하고 AppKit에서 지시 할 때 모든 뷰의 그림을 수행하도록합니다. 다른 경우에는 아무 것도 표시하지 않습니다. –

답변

1

drawRect : 외부에서 그림을 그린 것처럼 들립니다. 이 경우 드로잉 코드를 뷰의 (상자 또는 일부 하위 뷰의) drawRect : 메서드로 이동합니다. 그렇지 않으면 그림이 코코아 드로잉 시스템에 의해 밟히게됩니다. 반복되는 그림을 그리기 위해 루프가 아닌 타이머 나 애니메이션을 사용하기를 원할 것입니다.

+1

또한 두 번째'for' 루프가'drawRect :'에 있다고해도 performSelector : withObject : afterDelay :'메시지는'executeFrame' 메시지가 (* 'drawRect :'. –

1

저는 최근에 서클과 비슷한 것을하려고하는 누군가를위한 예제 프로그램을 작성했습니다. 내가 취한 접근법은 원 사양의 배열을 생성하고 drawRect에서 그릴 수 있도록하는 것이 었습니다. 그것은 꽤 잘 작동합니다. 아마도 도움이 될 것입니다. 당신이 전체 프로젝트를 원하는 경우에, 당신은 from here

@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 
관련 문제