欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-05-16 11:34:51
代码部分 touchview.h 复制代码 代码如下: #import     @interfa...

代码部分
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]); 
     
    //画以前的轨迹 
    for (int j = 0 ; j < [self.points_all count]; j++) { 
        nsmutablearray *points_tmp = [points_all objectatindex:j]; 
             
            for (int i = 0;i < [points_tmp count]-1;i++) 
            { 
                cgpoint point1 = [[points_tmp objectatindex:i] cgpointvalue]; 
                cgpoint point2 = [[points_tmp objectatindex:(i+1)] cgpointvalue]; 
                cgcontextmovetopoint(context, point1.x, point1.y); 
                cgcontextaddlinetopoint(context, point2.x, point2.y); 
                cgcontextstrokepath(context); 
            } 
        } 
     
    //画这次 
    for (int i=0; i < [self.points count]-1; i++) { 
        cgpoint point1 = [[self.points objectatindex:i] cgpointvalue]; 
        cgpoint point2 = [[self.points objectatindex:(i+1)] cgpointvalue]; 
        cgcontextmovetopoint(context, point1.x, point1.y); 
        cgcontextaddlinetopoint(context, point2.x, point2.y); 
        cgcontextstrokepath(context); 
    }     

 
//不支持多点触摸 
- (bool) ismultipletouchenabled 

    return no; 

 
//创建一个array,并且记录初始ponit 
- (void) touchesbegan:(nsset *) touches withevent:(uievent *) event 

    self.points = [nsmutablearray array]; 
    cgpoint pt = [[touches anyobject] locationinview:self]; 
    [self.points addobject:[nsvalue valuewithcgpoint:pt]]; 

 
//移动过程中记录这些points 
//调用setneedsdisplay,会触发drawrect方法的调用 
- (void) touchesmoved:(nsset *)touches withevent:(uievent *)event 

    cgpoint pt = [[touches anyobject] locationinview:self]; 
    [self.points addobject:[nsvalue valuewithcgpoint:pt]]; 
    [self setneedsdisplay]; 

 
- (void) touchesended:(nsset *)touches withevent:(uievent *)event 

    nsmutablearray *points_tmp = [[nsmutablearray alloc] initwitharray:self.points]; 
    if (self.points_all == nil) { 
        self.points_all = [[nsarray alloc] initwithobjects:points_tmp, nil]; 
    }else { 
        self.points_all = [self.points_all arraybyaddingobject:points_tmp]; 
    } 

@end 

viewcontroller.h

复制代码 代码如下:

#import <uikit/uikit.h> 
 
@class touchview; 
@interface viewcontroller : uiviewcontroller 

    touchview *tv; 

@end 

viewcontroller.m

复制代码 代码如下:

#import "viewcontroller.h" 
#import "touchview.h" 
 
@interface viewcontroller () 
 
@end 
 
@implementation viewcontroller 
 
- (void)viewdidload 

    [super viewdidload]; 
    // do any additional setup after loading the view, typically from a nib. 
    self.view.userinteractionenabled = yes; 
     
  // touchview *tv = [[touchview alloc]initwithframe:cgrectmake(0.0f, 0.0f, 260.0f, 260.0f)]; 
    tv = [[touchview alloc]initwithframe:self.view.frame]; 
    tv.backgroundcolor = [uicolor blackcolor]; 
     
    [self.view addsubview:tv]; 
     
    uisegmentedcontrol *seg = [[uisegmentedcontrol alloc] initwithitems:[@"white red blue green yellow" componentsseparatedbystring:@" "]]; 
    seg.segmentedcontrolstyle = uisegmentedcontrolsegmentcenter; 
    seg.tintcolor = [uicolor blackcolor];  
    seg.center = cgpointmake(self.view.center.x, (self.view.bounds.size.height - seg.bounds.size.height));  
    [self.view addsubview:seg]; 
     
    [seg addtarget:self action:@selector(colorchange:) forcontrolevents:uicontroleventvaluechanged]; 

 
- (void)viewdidunload 

    [super viewdidunload]; 
    // release any retained subviews of the main view. 

 
- (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation 

    return (interfaceorientation != uiinterfaceorientationportraitupsidedown); 

 
- (void) colorchange: (uisegmentedcontrol *) seg 

    switch ([seg selectedsegmentindex]) 
    { 
        case 0:  
            tv.paint_clr = [uicolor whitecolor]; 
            break; 
        case 1: 
            tv.paint_clr = [uicolor redcolor]; 
            break; 
        case 2: 
            tv.paint_clr = [uicolor bluecolor]; 
            break; 
        case 3: 
            tv.paint_clr = [uicolor greencolor]; 
            break; 
        case 4: 
            tv.paint_clr = [uicolor yellowcolor]; 
            break; 
        default: 
             
            break; 
    } 

 
@end 

效果图

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