简易涂鸦板

delegate.h

@property (retain, nonatomic) UIWindow *window;

delegate.m

-(void)dealloc{

    [_window release];

    [super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    MainViewController *mainVC = [[[MainViewController alloc]init]autorelease];

    self.window.rootViewController = mainVC;

    return YES;

}

创建LineMode类

LineMode.h

@interface LineModel : NSObject

@property(nonatomic,retain)UIBezierPath *path; //使用贝塞尔曲线记录触摸轨迹

@property(nonatomic,retain)UIColor *color ; //线条颜色

@property(nonatomic,assign)CGFloat width; //线条宽度

@end

LineMode.m

-(void)dealloc{

    [_path release];

    [_color release];

    [super dealloc];

}

 创建属于UIView的PaintView

PaintView.h

@property(nonatomic,retain)NSMutableArray *allLines; //用于保存画板上所有线条对象.

@property(nonatomic,retain)UIColor *lineColor; //当前线条的颜色

@property(nonatomic,assign)CGFloat lineWidth; //当前线条的宽度

-(void)undoLastDrawing; //撤销上次绘制

-(void)allClean; //清空画板

 PaintView.m

#import "PaintView.h"

#import "LineModel.h"

@implementation PaintView

 -(void)dealloc{

    [_allLines release];

    [_lineColor release];

    [super dealloc];

}

 

-(NSMutableArray *)allLines{

    if (!_allLines) {

        self.allLines = [NSMutableArray array];

    }

    return _allLines;

}

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *aTouch = touches.anyObject;

    CGPoint startPoint = [aTouch locationInView:self.superview];

        //创建贝塞尔曲线对象,并设置曲线的起始点.

    UIBezierPath*bezierPath=[UIBezierPath bezierPath];

    [bezierPath moveToPoint:startPoint];

    //  创建线条模型对象 并保存线条的三个数据

    LineModel*line=[[[LineModel alloc]init]autorelease];

    line.path=bezierPath;

    line.color=self.lineColor;

    line.width=self.lineWidth;

    

    [self.allLines addObject:line];

    

}

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *aTouch = touches.anyObject;

    CGPoint currentPoint =[aTouch locationInView:self.superview];

    //获取对应的线条模型(在触摸移动的过程中,数组的最后一个对象对应当前正在绘制的曲线)

    LineModel *aLine = self.allLines.lastObject;

    [aLine.path addLineToPoint:currentPoint];

    [self setNeedsDisplay]; //调用此方法是通知视图,绘制界面,一旦调用此方法,系统就会自动调用drawRect:方法来绘制界面.

}

 

 

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

//     Drawing code

    for (LineModel *aLine in self.allLines) {

        //设置填充颜色

        [aLine.color setStroke];

        //设置绘制时的线条宽度

        [aLine.path setLineWidth:aLine.width];

        [aLine.path stroke]; //开始绘制曲线

    }

 

}

-(void)undoLastDrawing{

    [self.allLines removeLastObject];

    [self setNeedsDisplay]; //移除数组中的最后一条曲线对象,并重绘界面.

}

-(void)allClean{

    [self.allLines removeAllObjects];

    [self setNeedsDisplay];

}

@end

创建的MainViewController

MainViewController.m

-(void)loadView{

    PaintView *painView = [[PaintView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    painView.backgroundColor = [UIColor whiteColor];

    painView.lineWidth = 5;

    painView.lineColor = [UIColor blackColor];

    self.view = painView;

    [painView release];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //创建菜单按钮

    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeSystem];

    menuButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-80, 40, 60, 40);

    [menuButton setTitle:@"菜单" forState:UIControlStateNormal];

    [menuButton addTarget:self action:@selector(handleMenuButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:menuButton];

    

    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectZero];

    textField.tag = 123;

    [self.view addSubview:textField];

    [textField release];

    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 60)];

    inputView.backgroundColor = [UIColor whiteColor];

    textField.inputView = inputView;

    [inputView release];

    

    NSArray *titles = @[@"减小",@"增大",@"撤销",@"清空",@"颜色"];

    CGFloat width = (CGRectGetWidth(self.view.bounds)-20*6)/5;

    CGFloat height = width;

    CGFloat originY = (60 - height) / 2;

    CGFloat originX = 20;

    for (int i = 0; i<titles.count; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.tag = 100+i;

        button.frame = CGRectMake(originX+(width+originX)*i, originY, width, height);

        [button setTitle:titles[i] forState:UIControlStateNormal];

        button.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.6];

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        //设置按钮的角半径,为按钮宽度的一半

        button.layer.cornerRadius = width / 2.0;

        button.layer.masksToBounds = YES; //按指定的半径裁剪视图.

        [button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        [inputView addSubview:button];

    }

    

}

 

-(void)handleMenuButtonAction:(UIButton *)sender{

    //获取textField

    UITextField *textField = (UITextField *)[self.view viewWithTag:123];

    //判断如果textField当前是第一响应者,则撤销其第一响应者权限,否则让其成为第一响应者.

    if (textField.isFirstResponder) {

        [textField resignFirstResponder];

    }else{

        [textField becomeFirstResponder];//成为第一响应者

    }

}

-(void)handleButtonAction:(UIButton *)sender{

    PaintView *paintView = (PaintView *)self.view; //获取画板视图

    switch (sender.tag) {

        case 100:

            if (paintView.lineWidth > 2) {

                paintView.lineWidth -=1;

            }

            break;

        case 101:

            paintView.lineWidth +=1;

            break;

        case 102:

            [paintView undoLastDrawing];

            break;

        case 103:

            [paintView allClean];

            break;

        case 104:

            sender.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

            //设置画笔颜色

            paintView.lineColor = sender.backgroundColor;

            break;

            

        default:

            break;

    }

}

 

转载于:https://www.cnblogs.com/lz824129/p/4891755.html

[FLASH/AS1/2]简易涂鸦板(带本地保存功能与撤消上一步功能) 作者:古树悬叶 日期:2010-01-07 二类涂鸦板:一种是主流的通过保存鼠标轨迹的方式来保存涂鸦数据,既使用矢量的方式来保存,样例如闪吧的涂鸦程序;另一种是通过保存 BitmapData 颜色值的方式来保存,样例还没有找到比较像样的样例。 在《内置方法Array.shift 与自定义循环++的执行效率比较》一文中,我已经对 shift 方法与自定义的 ++ 方式分别作了比较。虽然自定义 ++ 的方式比shift方法要快,但事实它们二者的效率都很低。由于 SharedObject 类是无法直接保存 BitmapData 对象的,所以只能将 BitmapData 的所有位图像素的每一个像素取 ARGB 值后保存。一张位图按500像素 x 500像素算,自定义 ++ 需要3秒多,而 shit 方法脚本超时。所以通过 BitmapData 的颜色方式来保存只能保存较小的位图。所以通过鼠标轨迹的方式保存涂鸦成了主流。 此涂鸦板在涂鸦之后会自动将涂鸦保存在本地,并且可以撤消上一步操作。涂鸦画线功能我是直接从FLASH帮助文件中考贝出来的,我在涂鸦功能的基础上添加了本地保存和撤消的功能。(代码可以扩展成自定义线条粗细,自定义线条颜色,透明度等等。还可以添加新的数据用来记录被撤消的步聚,这样不旦可以撤消还有了重做功能。甚至还可以在 tempArray.push 添加新的数组,同时保存不同线条粗细、颜色、透明度的涂鸦,做成一个类似画板的程序。) 謾軻(Manction) http://manction.51.com 2010.01.07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值