Quartz 2D繪圖

- (void)drawRect:(CGRect)rect {

    

    CGContextRef content = UIGraphicsGetCurrentContext();

    

    //1.繪製圖畫

//    [self drawImage:content];

    

    //2.繪製文字

//    [self drawtext:content];

    

    //3.繪製貝塞爾曲線

//    [self drawCure:content];

    

    //4.繪製圓弧

    [self drawArc:content];

}


#pragma mark - 繪製圖畫

- (void)drawImage:(CGContextRef)content {

    UIImage *image = [UIImage imageNamed:@"2012100413195471481.jpg"];

    

    //轉換座標

    CGContextSaveGState(content);

    CGContextRotateCTM(content, M_PI);

    CGContextScaleCTM(content, -1, 1);

    CGContextTranslateCTM(content, 0, -self.bounds.size.height);

    

    CGContextDrawImage(content, self.bounds, image.CGImage);

    

    CGContextRestoreGState(content);


}


#pragma mark - 繪製文字

- (void)drawtext:(CGContextRef)content {

    NSString *str = @"哈哈哈";

    

    CGRect rect = CGRectMake(50, 50, 200, 200);

    [[UIColor redColor] setFill];

    UIRectFill(rect);

    

    CGRect rect1 = CGRectMake(200, 50, 200, 200);

    [[UIColor orangeColor] setFill];

    UIRectFill(rect1);

    

    

    UIFont *font = [UIFont systemFontOfSize:20];

    [str drawInRect:rect withAttributes:@{NSFontAttributeName:font}];

    

    

}


#pragma mark - 繪製貝塞爾曲線

- (void)drawCure:(CGContextRef)context {

    CGContextSetLineWidth(context, 5);

    [[UIColor redColor] setStroke];

    

    CGContextMoveToPoint(context, 20, 100);

    

    CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);

    

    CGContextDrawPath(context, kCGPathStroke);

}


#pragma mark - 繪製圓弧

- (void)drawArc:(CGContextRef)context {

    

    //轉換座標

    CGContextSaveGState(context);

    CGContextRotateCTM(context, M_PI);

    CGContextScaleCTM(context, -1, 1);

    CGContextTranslateCTM(context, 0, -self.bounds.size.height);

    

    [[UIColor grayColor] setFill];

    [[UIColor redColor] setStroke];

    CGPoint centerPoint = CGPointMake(150, 150);

    float radius = 100;

    CGFloat beginAngle = 0;

    CGFloat endAngle = M_PI_4;

    CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, beginAngle, endAngle, 1);

    

    CGContextMoveToPoint(context, centerPoint.x+radius*cos(beginAngle), centerPoint.y+radius*sin(beginAngle));

    CGContextAddLineToPoint(context, centerPoint.x, centerPoint.y);

    CGContextAddLineToPoint(context, centerPoint.x+radius*cos(endAngle), centerPoint.y+radius*sin(endAngle));

    

    CGContextDrawPath(context, kCGPathEOFillStroke);

}


發佈了34 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章