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

代码部分
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 

效果图

时间: 2024-08-31 11:28:50

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

Win7系统下添加直接使用摄像头功能的简单方法

Win7.Win8系统下区别于XP系统,通常不显示单独的摄像头图标,若希望实现此功能,只需下载附件中的摄像头插件,双击运行即可实现摄像头图标在"我的电脑.计算机"下面的显示. 测试系统:Windows 7系统 操作步骤: 请下载附件中的W7sxt.rar 进行解压缩,解压后的图标如下图: 双击图标后的效果如下图所示: 如不需要此功能,可以直接利用右键点击该图标,选择"删除"按钮完成操作,如下图:         注:更多精彩教程请关注三联电脑教程栏目,三联电脑办公群

V5Shop分享一个独立网店快速提升PR和销售额的方法

V5Shop分享一个独立网店快速提升PR和销售额的方法 很多独立网店的店主都在为宣传推广头疼.在现在这个产品丰富的市场经济环境下,各行各业的竞争都异常激烈,"酒香也怕巷子深"已经成为网商们的共识.作为独立网店,独立运营固然可以避免许多"拼低价"的竞争,客户对网店和品牌的忠诚度也有保证,但是在推广这个问题上,却难住了不少网商. 许多网商之所以过于依赖淘宝等大型平台,就是因为这些平台能够为其带来源源不断的客户,其不用为如何招揽客户,如何推广网店而劳心.有得必有失,这同样

Win7旗舰版系统下无法使用传真和扫描功能的解决方法

  传真机是应用扫描和光电变换技术,广泛应用于办公场所,特别是传真和扫描功能会经常使用到,但是在使用过程中可能会碰到各种各样的问题,比如有使用win7旗舰版系统的用户反映说无法使用传真和扫描机功能,要如何解决呢,其实只要重新安装传真机驱动就可以解决了,下面随小编来学习一下具体的操作步骤吧.   1.打开"控制面板"-"程序和功能",点击左上角的"打开或关闭windows功能";   2.找到"打印和文件服务"中的"w

iOS缓存文件大小显示功能和一键清理功能的实现方法_IOS

缓存占用了系统的大量空间,如何实时动态的显示缓存的大小,使用户清晰的了解缓存的积累情况,有效的进行一键清理呢? 为方便读者和未来自己更好理解,我们创建这样场景.(在表视图的清除缓存一单元格内创建一个UILabel *cacheLabel用于显示当前缓存,当点击单元格弹出提示框,点击确定,清除缓存). 下面是实现代码: #pragma mark - 计算缓存大小 - (NSString *)getCacheSize { //定义变量存储总的缓存大小 long long sumSize = 0; /

理解iOS多线程应用的开发以及线程的创建方法_IOS

一.进程和线程1.什么是进程   进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcode,系统就会分别启动2个进程 通过"活动监视器"可以查看Mac系统中所开启的进程   2.什么是线程 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) 线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 比如使用酷狗播放音乐.使用迅雷下载电影,都需要在线程中执行 3.线程的串行   1个线程

iOS读取txt文件出现中文乱码的解决方法_IOS

一.情景描述:  后台给一个txt文件,编码是utf-8,在Mac电脑Xcode开发环境下读取txt文件内容,汉字会出现乱码,英文没有乱码这种情况.  二.尝试解决方法: 修改编码格式,尝试了NSUTF16StringEncoding,NSUTF8StringEncoding,NSASCIIStringEncoding编码等,出现的问题有时是中文乱码,有时是utf-8不能打开文件问题,最终问题都没能解决.  三.猜测原因: txt文件是从window电脑上创建,有可能和环境有关,第二,编码问题.

IOS中Json解析实例方法详解(四种方法)_IOS

作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此"http://www.bejson.com/"网站来进行JSON格式化校验(点击打开链接).此网站不仅可以检测Json代码中的错误,而且可以以视图形式显示json中的数据内容,很是方便. 从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization),但是为了兼容以前的iOS版本,可以使用第三方库来解析Json. 本文将介绍Tou

IOS应用内跳转系统设置相关界面的方法_IOS

在iOS开发中,有时会有跳转系统设置界面的需求,例如提示用户打开蓝牙或者WIFI,提醒用户打开推送或者位置权限等.在iOS6之后,第三方应用需要跳转系统设置界面,需要在URL type中添加一个prefs值,如下图: 跳转系统设置根目录中的项目使用如下的方法: _array = @[ @{@"系统设置":@"prefs:root=INTERNET_TETHERING"}, @{@"WIFI设置":@"prefs:root=WIFI&qu

iOS让软键盘消失的简单方法_IOS

一些文本输入控件等待输入时会弹出软键盘,我们可以设置这些控件的Did End On Exit之类的回调方法以在用户点击软键盘上的done或return之列的按键时收起键盘. 不过某些时候有些键盘没有上述的按键,或者我们希望点击App视图的背景时使软键盘收起来,无论当时软键盘是否打开. 一种办法是拖动一个按钮到View上,然后使其大小和View相同,然后设置点击该按钮的回调. but,我们还可以有更简单的方法. 我们知道UIView本身没什么Action,不过它的子类UIControl却有不少,所