iOS开发基础110-Core Graphics应用场景

球哥-chg / 2024-07-17 / 原文

Core Graphics是一种强大的二维图形绘制框架,广泛应用于iOS开发中。以下是几个常见的运用场景以及对应的代码示例:

1. 自定义视图绘制

通过覆盖UIView的drawRect:方法,可以自定义视图的外观。

示例代码:

#import <UIKit/UIKit.h>

@interface CustomView : UIView

@end

@implementation CustomView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Set fill color
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    
    // Draw a filled rectangle
    CGContextFillRect(context, CGRectMake(20, 20, 100, 100));
    
    // Set stroke color
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    
    // Draw a circle
    CGContextStrokeEllipseInRect(context, CGRectMake(150, 20, 100, 100));
}

@end

2. 绘制图像

使用Core Graphics可以在视图中绘制图像,并进行一些基本的图像处理操作。

示例代码:

#import <UIKit/UIKit.h>

@interface ImageView : UIView

@end

@implementation ImageView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage *image = [UIImage imageNamed:@"example.png"];
    
    // Draw image in the center of the view
    CGRect imageRect = CGRectMake((self.bounds.size.width - image.size.width) / 2,
                                  (self.bounds.size.height - image.size.height) / 2,
                                  image.size.width,
                                  image.size.height);
    CGContextDrawImage(context, imageRect, image.CGImage);
}

@end

3. 绘制文本

使用Core Graphics可以自定义文本的绘制,包括设置字体、颜色、对齐方式等。

示例代码:

#import <UIKit/UIKit.h>

@interface TextView : UIView

@end

@implementation TextView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    
    NSString *text = @"Hello, Core Graphics!";
    UIFont *font = [UIFont systemFontOfSize:24];
    NSDictionary *attributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor blackColor]};
    
    CGSize textSize = [text sizeWithAttributes:attributes];
    CGRect textRect = CGRectMake((self.bounds.size.width - textSize.width) / 2,
                                 (self.bounds.size.height - textSize.height) / 2,
                                 textSize.width,
                                 textSize.height);
    [text drawInRect:textRect withAttributes:attributes];
}

@end

4. 绘制渐变

通过Core Graphics可以绘制线性或径向渐变。

示例代码:

#import <UIKit/UIKit.h>

@interface GradientView : UIView

@end

@implementation GradientView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    NSArray *colors = @[(__bridge id)[UIColor redColor].CGColor,
                        (__bridge id)[UIColor blueColor].CGColor];
    
    CGFloat locations[] = { 0.0, 1.0 };
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
    
    CGPoint startPoint = CGPointMake(0, 0);
    CGPoint endPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height);
    
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

@end

5. 绘制路径

使用Core Graphics可以创建复杂路径,包括直线、曲线等。

示例代码:

#import <UIKit/UIKit.h>

@interface PathView : UIView

@end

@implementation PathView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 2.0);
    
    CGContextMoveToPoint(context, 20, 20);
    CGContextAddLineToPoint(context, 200, 20);
    CGContextAddCurveToPoint(context, 200, 70, 50, 70, 50, 120);
    CGContextAddArc(context, 100, 100, 50, 0, M_PI, 0);
    
    CGContextStrokePath(context);
}

@end