iOS应用开发——小画板Demo

原创 2012年03月29日 16:57:10

     还有很多功能可以加,记在这里留着慢慢加吧。


代码部分

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 使用UIBezierPath类实现随手画画板

在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画,清除...

(素材源码)猫猫学IOS(三十四)UI之Quartz2D画画板的实现

猫猫分享,必须精品原创文章,欢迎转载。转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 源码:http://do...

iOS画板实现第二波

画板实现第二波1.将对图片添加一系列手势操作,原有实现不能实现 以前详细说明个六种手势在控件上的操作(手势只能添加在控件上) 对原有代码进行改造 1.1添加一个图片处理的VIEW-(void) ima...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

ios绘图demo,做一个涂鸦板(下)

ios绘图demo,做一个涂鸦板(下) 之前一篇ios绘图demo,做一个涂鸦板(上) 完成了一个基本功能的绘图板,这一篇,最要是增加一个画贝塞尔曲线的功能。 ios绘图基础 ios...

ios UIKit框架分析 第8天

1.viewController @property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle NS_AV...

iOS应用开发入门(3)——点击button控制textview

上一节中我们讲到添加一个button并为其添加点击监听事件,上上一节中我们讲到添加一个textview并显示一串字符串,这一节我们把前面的内容连起来,改成点击button,然后就改变textview上...

IOS应用开发09——为自定义UITableViewCell的部分区域添加手势

程序中在cell点击的时候需要不同的操作,点击s

IOS应用开发15——UITableView多选删除及样式自定

项目中要实现一个TableViewCell的多选,全选删除功能,累

iOS应用开发——ARC

原文链接 :  http://www.yifeiyang.net/development-of-the-iphone-simply-1/ARC是什么        ARC是iOS 5推出的新功能,全称...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS应用开发——小画板Demo
举报原因:
原因补充:

(最多只允许输入30个字)