iOS开发之涂鸦板

4 篇文章 0 订阅


这是一个很简单的涂鸦板,只能在这里画线条而已~


首先创建一个可变数组:


#import "PaintView.h"

//延展 在延展中声明的属性只能属于这个类

@interface PaintView ()

//用于保存所有线段的数组

@property(nonatomic, retain)NSMutableArray *allLines;

@end



对这个数组进行懒加载~

- (NSMutableArray *)allLines

{

    if (!_allLines) {

        self.allLines = [NSMutableArray array];//右边是便利构造器的创建,左边是getter方法

    }

    return _allLines;

}


开始画线条:


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

{

    //获取触摸的起始点

    UITouch *aTouch = touches.anyObject;

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

    //通过贝塞尔曲线对象来记录当前触摸移动产生的轨迹点

    UIBezierPath *path = [UIBezierPath bezierPath];

    //通过贝塞尔曲线对象记录起始点

    [path moveToPoint:startPoint];

    //将曲线保存在数组中

    [self.allLines addObject:path];

}


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

{

    //获取当前触摸产生的当前点

    UITouch *atouch = touches.anyObject;

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

    //获取当前创建的贝塞尔曲线

    UIBezierPath *path = self.allLines.lastObject;

    //添加当前触摸的轨迹点

    [path addLineToPoint:currentPoint];  //r让当前视图重绘界面

    //一旦调用此方法,视图对象会自动调用drawRect:方法来绘制自己的界面

    [self setNeedsDisplay];

}



 - (void)drawRect:(CGRect)rect {

//     Drawing code

     //设置画笔的颜色

     [[UIColor redColor]setStroke];

     //通过遍历数组来绘制每一条画板上的曲线

     for (UIBezierPath *path in self.allLines) {

         //设置贝塞尔曲线的线宽

         path.lineWidth = 5;

         //在视图上重绘贝塞尔曲线

         [path stroke];

     }

}


这里设计到了系统的方法:事件触发; 线条是由一个个点组成的,触摸开始的时候是一个点,移动的时候也是一个点,这些点连接起来就成了一条线~

可以进一步的丰富这个涂鸦板的功能,比如增加几个按钮,可以改变它的线条的宽度,颜色,删除线条,撤销删除等功能;

请多多指教~~




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值