分享一个iOS下实现基本绘画板功能的简单方法

2020-01-14 16:46:12王旭
易采站长站为您分析iOS下实现基本绘画板功能的简单方法,代码基于传统的Objective-C,需要的朋友可以参考下  

代码部分
TouchView.h

复制代码
#import <UIKit/UIKit.h>  
  
@interface TouchView : UIView  
{  
    NSMutableArray *points;  
    NSArray *points_all;  
    CGContextRef context;  
    UIColor *paint_clr;  
}  
@property (strong,nonatomic) NSMutableArray *points;  
@property (strong,nonatomic) NSArray *points_all;  
@property (strong,nonatomic) UIColor *paint_clr;  
  
@end  

 

TouchView.m

复制代码
#import "TouchView.h"  
  
@implementation TouchView  
@synthesize points, points_all, paint_clr;  
  
- (id)initWithFrame:(CGRect)frame  
{  
    self = [super initWithFrame:frame];  
    if (self) {  
        // Initialization code  
        paint_clr = [UIColor greenColor];  
    }  
    return self;  
}  
  
// Only override drawRect: if you perform custom drawing.  
// An empty implementation adversely affects performance during animation.  
- (void)drawRect:(CGRect)rect  
{  
    // Drawing code  
    if ((!self.points) || (self.points.count < 2)) {  
        return;  
    }  
        
    context = UIGraphicsGetCurrentContext();  
    //设置画笔粗细   
    CGContextSetLineWidth(context, 5.0f);  
    //设置画笔颜色  
    //[[UIColor blueColor]set ];  
    // [paint_clr set];  
    //CGContextSetStrokeColorWithColor(context, [[UIColor blueColor]CGColor]);  
    CGContextSetStrokeColorWithColor(context, [paint_clr CGColor]);  
      
    //画以前的轨迹