iOS源码之实现基本绘画板功能的简单方法
白羽 2018-06-13 来源 :网络 阅读 758 评论 0

摘要:本文将带你了解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;  
    }  
}

 


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程