Question

I am an Objective C Programmer. I am developing a universal App. In this App i want to use Quartz to draw a square, but not completely in one frame, rather frame for frame. In the Code below there is a possibility. But its not so good, because i want to draw rectangles, circles and other stuff. So, is there a better way to draw such things.

-(void)drawRect:(CGRect)rect {
    [self drawARect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 20.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
    CGColorRef color = CGColorCreate(colorspace, components);
    CGContextSetStrokeColorWithColor(context, color);
    if (!firstLineReady) {
        CGContextMoveToPoint(context, 200, 200);
        CGContextAddLineToPoint(context, x, y);
    }

    if (firstLineReady && !secondLineReady) {
        CGContextMoveToPoint(context, 200, 200);
        CGContextAddLineToPoint(context, 600, 200);
        CGContextMoveToPoint(context, 600, 200);
        CGContextAddLineToPoint(context, x, y);
    }

    if (secondLineReady && !thirdLineReady) {
        CGContextMoveToPoint(context, 200, 200);
        CGContextAddLineToPoint(context, 600, 200);
        CGContextMoveToPoint(context, 600, 200);
        CGContextAddLineToPoint(context, 600, 600);
        CGContextMoveToPoint(context, 600, 600);
        CGContextAddLineToPoint(context, x, y);
    }

    if (thirdLineReady && ! fourthLineReady) {
        CGContextMoveToPoint(context, 200, 200);
        CGContextAddLineToPoint(context, 600, 200);
        CGContextMoveToPoint(context, 600, 200);
        CGContextAddLineToPoint(context, 600, 600);
        CGContextMoveToPoint(context, 600, 600);
        CGContextAddLineToPoint(context, 200, 600);
        CGContextMoveToPoint(context, 200, 600);
        CGContextAddLineToPoint(context, x, y);
    }

    if (fourthLineReady) {
        CGContextMoveToPoint(context, 200, 200);
        CGContextAddLineToPoint(context, 600, 200);
        CGContextMoveToPoint(context, 600, 200);
        CGContextAddLineToPoint(context, 600, 600);
        CGContextMoveToPoint(context, 600, 600);
        CGContextAddLineToPoint(context, 200, 600);
        CGContextMoveToPoint(context, 200, 600);
        CGContextAddLineToPoint(context, 200, 200);
        [timer invalidate];        
    }

    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);
}

- (void) drawARect{

    if (!firstLineReady) {
        x+=speed; y+=0;
        if (x>=600) {
            x=600;
            firstLineReady = YES;
        }
    }

    if (firstLineReady && !secondLineReady ) {
        x+=0; y+=speed;
        if (y>=600) {
            y=600;
            secondLineReady = YES;
        }
    }


    if (firstLineReady && secondLineReady && !thirdLineReady  ) {
        x-=speed; y+=0;
        if (x<=200) {
            x=200;
            thirdLineReady = YES;
        }
    }

    if (firstLineReady && secondLineReady && thirdLineReady ) {
        x+=0; y-=speed;
        if (y<=200) {
            y=200;
            fourthLineReady = YES;
        }
    }
}

Is it possible to give the CGContextAddLineToPoint a custom Pattern like a pencil or a biro?

Was it helpful?

Solution

Oval
CGRect aRect= CGRectMake(80, 80, 160, 100); 
 CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0); 
 CGContextSetLineWidth(context, 3.0); 
 CGContextAddEllipseInRect(context, aRect); //椭圆 
 CGContextDrawPath(context, kCGPathStroke); 

Diamond
CGContextSetLineWidth(context, 2.0); 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGContextMoveToPoint(context, 100, 100); 
CGContextAddLineToPoint(context, 150, 150); 
CGContextAddLineToPoint(context, 100, 200); 
CGContextAddLineToPoint(context, 50, 150); 
CGContextAddLineToPoint(context, 100, 100); 
CGContextStrokePath(context); 

Rectangle
CGContextSetLineWidth(context, 2.0); 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGRect rectangle = CGRectMake(60,170,200,80); 
CGContextAddRect(context, rectangle); 
CGContextStrokePath(context); 

or you can get the opensource code from github
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top