封裝CoreGraphics的API簡化繪圖操作


封裝CoreGraphics的API簡化繪圖操作

 

效果

 

說明

1. 將CoreGraphics的API接口抽象為對象,讓繪圖變得簡單易懂

2. 簡化常用的繪制操作

3. 源碼長期更新

 

源碼

https://github.com/YouXianMing/CGContextObject

//
//  CGContextObject.h
//  DrawRect
//
//  Created by YouXianMing on 15/7/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import "RGBColor.h"
#import "GradientColor.h"
@class CGContextObject;


typedef void(^CGContextObjectDrawBlock_t)(CGContextObject *contextObject);


@interface CGContextObject : NSObject

/**
 *  操作句柄
 */
@property (nonatomic)          CGContextRef   context;

/**
 *  線頭樣式
 */
@property (nonatomic)          CGLineCap      lineCap;

/**
 *  線條寬度
 */
@property (nonatomic)          CGFloat        lineWidth;

/**
 *  線條顏色
 */
@property (nonatomic, strong)  RGBColor      *strokeColor;

/**
 *  填充顏色
 */
@property (nonatomic, strong)  RGBColor      *fillColor;

/**
 *  由context進行初始化
 *
 *  @param context 繪制句柄
 *
 *  @return 繪制對象
 */
- (instancetype)initWithCGContext:(CGContextRef)context;

#pragma mark - 繪制操作流程
/**
 *  開始path
 */
- (void)beginPath;

/**
 *  關閉path
 */
- (void)closePath;

/**
 *  線條繪制
 */
- (void)strokePath;

/**
 *  填充繪制
 */
- (void)fillPath;

/**
 *  線條繪制 + 填充繪制
 */
- (void)strokeAndFillPath;

/**
 *  繪制線條用block (beginPath + closePath + 你繪制的代碼 + strokePath)
 *
 *  @param block 繪制用block
 */
- (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block;

/**
 *  填充區域用block (beginPath + closePath + 你繪制的代碼 + fillPath)
 *
 *  @param block 填充用block
 */
- (void)drawFillBlock:(CGContextObjectDrawBlock_t)block;

/**
 *  繪制加填充
 *
 *  @param block 繪制加填充用block
 */
- (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block;

/**
 *  繪制線條用block (beginPath + closePath + 你繪制的代碼 + strokePath)
 *
 *  @param block     繪制用block
 *  @param closePath 是否關閉曲線
 */
- (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath;

/**
 *  填充區域用block (beginPath + closePath + 你繪制的代碼 + fillPath)
 *
 *  @param block     繪制用block
 *  @param closePath 是否關閉曲線
 */
- (void)drawFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath;

/**
 *  繪制加填充
 *
 *  @param block     繪制用block
 *  @param closePath 是否關閉曲線
 */
- (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath;

#pragma mark - 繪制圖片API

- (void)drawImage:(UIImage *)image atPoint:(CGPoint)point;
- (void)drawImage:(UIImage *)image atPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
- (void)drawImage:(UIImage *)image inRect:(CGRect)rect;
- (void)drawImage:(UIImage *)image inRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
- (void)drawImage:(UIImage *)image asPatternInRect:(CGRect)rect;

#pragma mark - 保存操作

/**
 *  將當前設置存取到棧區中(入棧操作)
 */
- (void)saveStateToStack;

/**
 *  從棧區中取出之前保存的設置(出棧操作)
 */
- (void)restoreStateFromStack;

#pragma mark - 圖形繪制API
/**
 *  移動到起始點
 *
 *  @param point 起始點
 */
- (void)moveToStartPoint:(CGPoint)point;

/**
 *  添加一個點(與上一個點直線相連)
 *
 *  @param point 點
 */
- (void)addLineToPoint:(CGPoint)point;

/**
 *  添加二次貝塞爾曲線
 *
 *  @param point    結束點
 *  @param pointOne 控制點1
 *  @param pointTwo 控制點2
 */
- (void)addCurveToPoint:(CGPoint)point controlPointOne:(CGPoint)pointOne controlPointTwo:(CGPoint)pointTwo;

/**
 *  添加一次貝塞爾曲線
 *
 *  @param point        結束點
 *  @param controlPoint 控制點
 */
- (void)addQuadCurveToPoint:(CGPoint)point controlPoint:(CGPoint)controlPoint;

/**
 *  在指定的區域填充彩色的矩形(此為直接繪制)
 *
 *  @param rect          指定的區域
 *  @param gradientColor 漸變色對象
 */
- (void)drawLinearGradientAtClipToRect:(CGRect)rect gradientColor:(GradientColor *)gradientColor;

#pragma mark - 
/**
 *  添加一個矩形
 *
 *  @param rect
 */
- (void)addRect:(CGRect)rect;

/**
 *  在給定的矩形中繪制橢圓
 *
 *  @param rect
 */
- (void)addEllipseInRect:(CGRect)rect;

/**
 *  將string繪制在指定的點上
 *
 *  @param string     字符串
 *  @param point      點
 *  @param attributes 富文本設置(可以為空)
 */
- (void)drawString:(NSString *)string atPoint:(CGPoint)point withAttributes:(NSDictionary *)attributes;

/**
 *  將string繪制在制定的區域
 *
 *  @param string     字符串
 *  @param rect       區域
 *  @param attributes 富文本設置(可以為空)
 */
- (void)drawString:(NSString *)string inRect:(CGRect)rect withAttributes:(NSDictionary *)attributes;

/**
 *  將富文本繪制在制定的點上
 *
 *  @param string 富文本
 *  @param point  點
 */
- (void)drawAttributedString:(NSAttributedString *)string atPoint:(CGPoint)point;

/**
 *  將富文本繪制在制定的矩形中
 *
 *  @param string 富文本
 *  @param rect   矩形
 */
- (void)drawAttributedString:(NSAttributedString *)string inRect:(CGRect)rect;

@end
//
//  CGContextObject.m
//  DrawRect
//
//  Created by YouXianMing on 15/7/2.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "CGContextObject.h"

@interface CGContextObject ()

@end

@implementation CGContextObject

- (instancetype)initWithCGContext:(CGContextRef)context {
    
    self = [super init];
    if (self) {
        
        self.context = context;
    }
    
    return self;
}

- (void)moveToStartPoint:(CGPoint)point {
    
    if (_context) {
        CGContextMoveToPoint(_context, point.x, point.y);
    }
}

- (void)addLineToPoint:(CGPoint)point {
    
    if (_context) {
        CGContextAddLineToPoint(_context, point.x, point.y);
    }
}

- (void)addCurveToPoint:(CGPoint)point controlPointOne:(CGPoint)pointOne controlPointTwo:(CGPoint)pointTwo {

    if (_context) {
        CGContextAddCurveToPoint(_context, pointOne.x, pointOne.y, pointTwo.x, pointTwo.y, point.x, point.y);
    }
}

- (void)addQuadCurveToPoint:(CGPoint)point controlPoint:(CGPoint)controlPoint {
    
    if (_context) {
        CGContextAddQuadCurveToPoint(_context, controlPoint.x, controlPoint.y, point.x, point.y);
    }
}

- (void)drawLinearGradientAtClipToRect:(CGRect)rect gradientColor:(GradientColor *)gradientColor {
    
    [self saveStateToStack];
    
    
    if (_context) {
        
        CGContextClipToRect(_context, rect);
        
        CGContextDrawLinearGradient(_context,
                                    gradientColor.gradientRef,
                                    gradientColor.gradientStartPoint,
                                    gradientColor.gradientEndPoint, kCGGradientDrawsBeforeStartLocation);
    }
    
    
    [self restoreStateFromStack];
}

- (void)addRect:(CGRect)rect {

    if (_context) {
        CGContextAddRect(_context, rect);
    }
}

- (void)addEllipseInRect:(CGRect)rect {
    
    if (_context) {
        CGContextAddEllipseInRect(_context, rect);
    }
}

- (void)drawString:(NSString *)string atPoint:(CGPoint)point withAttributes:(NSDictionary *)attributes {

    [string drawAtPoint:point withAttributes:attributes];
}

- (void)drawString:(NSString *)string inRect:(CGRect)rect withAttributes:(NSDictionary *)attributes {

    [string drawInRect:rect withAttributes:attributes];
}

- (void)drawAttributedString:(NSAttributedString *)string atPoint:(CGPoint)point {

    [string drawAtPoint:point];
}

- (void)drawAttributedString:(NSAttributedString *)string inRect:(CGRect)rect {

    [string drawInRect:rect];
}

- (void)beginPath {
    
    if (_context) {
        CGContextBeginPath(_context);
    }
}

- (void)closePath {

    if (_context) {
        CGContextClosePath(_context);
    }
}

- (void)strokePath {

    if (_context) {
        CGContextStrokePath(_context);
    }
}

- (void)fillPath {

    if (_context) {
        CGContextFillPath(_context);
    }
}

- (void)strokeAndFillPath {

    if (_context) {
        CGContextDrawPath(_context, kCGPathFillStroke);
    }
}

- (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block {

    [self beginPath];
    
    __weak CGContextObject *weakSelf = self;
    
    block(weakSelf);
    
    [self closePath];
    
    [self strokePath];
}

- (void)drawFillBlock:(CGContextObjectDrawBlock_t)block {
    
    [self beginPath];
    
    __weak CGContextObject *weakSelf = self;
    
    block(weakSelf);
    
    [self closePath];
    
    [self fillPath];
}

- (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block {

    [self beginPath];
    
    __weak CGContextObject *weakSelf = self;
    
    block(weakSelf);
    
    [self closePath];
    
    [self strokeAndFillPath];
}

- (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath {

    [self beginPath];
    
    __weak CGContextObject *weakSelf = self;
    
    block(weakSelf);
    
    if (closePath) {
        [self closePath];
    }
    
    [self strokePath];
}

- (void)drawFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath {

    [self beginPath];
    
    __weak CGContextObject *weakSelf = self;
    
    block(weakSelf);
    
    if (closePath) {
        [self closePath];
    }
    
    [self fillPath];
}

- (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath {

    [self beginPath];
    
    __weak CGContextObject *weakSelf = self;
    
    block(weakSelf);
    
    if (closePath) {
        [self closePath];
    }
    
    [self strokeAndFillPath];
}

- (void)drawImage:(UIImage *)image atPoint:(CGPoint)point {

    [image drawAtPoint:point];
}

- (void)drawImage:(UIImage *)image atPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha {

    [image drawAtPoint:point blendMode:blendMode alpha:alpha];
}

- (void)drawImage:(UIImage *)image inRect:(CGRect)rect {

    [image drawInRect:rect];
}

- (void)drawImage:(UIImage *)image inRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha {

    [image drawInRect:rect blendMode:blendMode alpha:alpha];
}

- (void)drawImage:(UIImage *)image asPatternInRect:(CGRect)rect {

    [image drawAsPatternInRect:rect];
}

- (void)saveStateToStack {
    
    if (_context) {
        CGContextSaveGState(_context);
    }
}

- (void)restoreStateFromStack {
    
    if (_context) {
        CGContextRestoreGState(_context);
    }
}

#pragma mark - 重寫setter,getter方法
@synthesize strokeColor = _strokeColor;
- (void)setStrokeColor:(RGBColor *)strokeColor {

    if (_context) {
        
        _strokeColor = strokeColor;
        CGContextSetRGBStrokeColor(_context, strokeColor.red, strokeColor.green, strokeColor.blue, strokeColor.alpha);
    }
}
- (RGBColor *)strokeColor {

    return _strokeColor;
}

@synthesize fillColor = _fillColor;
- (void)setFillColor:(RGBColor *)fillColor {
    
    if (_context) {
        
        _fillColor = fillColor;
        CGContextSetRGBFillColor(_context, fillColor.red, fillColor.green, fillColor.blue, fillColor.alpha);
    }
}
- (RGBColor *)fillColor {
    
    return _fillColor;
}

@synthesize lineWidth = _lineWidth;
- (void)setLineWidth:(CGFloat)lineWidth {
    
    if (_context) {
        
        _lineWidth = lineWidth;
        CGContextSetLineWidth(_context, lineWidth);
    }
}
- (CGFloat)lineWidth {
    
    return _lineWidth;
}

@synthesize lineCap = _lineCap;
- (void)setLineCap:(CGLineCap)lineCap {
    
    if (_context) {
    
        _lineCap = lineCap;
        CGContextSetLineCap(_context, lineCap);
    }
    
}
- (CGLineCap)lineCap {

    return _lineCap;
}

@end

 

細節

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com