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

2016-02-19 10:59 57 1 收藏

岁数大了,QQ也不闪了,微信也不响了,电话也不来了,但是图老师依旧坚持为大家推荐最精彩的内容,下面为大家精心准备的分享一个iOS下实现基本绘画板功能的简单方法,希望大家看完后能赶快学习起来。

【 tulaoshi.com - 编程语言 】

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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 

效果图

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1596284.html

延伸阅读
定位获取位置及位置编码-反编码 我们的应用程序,可以通过添加Core Location框架所包含的类,获取设备的地图位置。 添加CoreLocation.framework框架,导入#importCoreLocation/CoreLocation.h。 使用地图服务时,会消耗更多地设备电量.因此,在获取到设备的位置后,应该停止定位来节省电量。 我们通过一个demo来展示内容与效果 代码如下: // //...
标签: Web开发
代码如下: Function AutoLinkURLs(strString) Dim match, matches, offset, url, email, link, relnkAutoLinkURL relnkAutoLinkURL = "a href=""[[%URL%]]""[[%URLText%]]/a" If Not IsObject(regExp) Then Set regExp = New RegExp regExp.Global = True regExp.IgnoreCase = True 'Look for URLs regExp.Pattern = "...
一个简单的登录对话框的实现 作者:不会游泳的鱼 下载源代码 要求用户正确输入用户名和密码,然后才能进入系统。刚好前几天有个人问俺如何在程序启动时先启动登录对话框的问题,俺就给他写了个示例程序,今天拿出来给大伙共享,有什么不正确的地方请大家多多指教。 一、在 LoginTe...
在应用中,我们常常需要Thread缓冲池来做一些事以提高程序的效率和并发性。本文演示了如何利用Queue这种数据结构实现一个简单的Thread缓冲池。 一个Thread缓冲池可以设计成以下这样:缓冲池由几个工作Thread和一个Queue组成,Client负责把任务放到Queue里面(put方法),而工作Thread就依次取出这些任务并执行它们(get方法)。 Queue的一个经...
标签: Web开发
这里是js的代码: 代码如下: jQuery.fn.size = function() { return jQuery(this).get(0).options.length; } //获得选中项的索引 jQuery.fn.getSelectedIndex = function() { return jQuery(this).get(0).selectedIndex; } //获得当前选中项的文本 jQuery.fn.getSelectedText = function() { if(this.size() == 0) { return ...

经验教程

341

收藏

51
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部