IOS Quartz2D 学习一 手写板

Quartz2D 学习一
该文章只为记录学习的过程,和我一样刚接触这块的人可以看看,大牛勿喷。

首先 手写的内容 包含 n 条曲线,每一条曲线包含n个点

NSMutableArray *allPoint;

NSMutableArray  *allLine;


allLine 在view init 的时候初始化 


接下来要捕获手指和设备的交互的位置。

代码如下:

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

{

 

    allPoint = [[NSMutableArrayalloc] init];

    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView:self];

    LocationMode *mode = [[LocationModealloc] init];

    mode.x = location.x;

    mode.y = location.y;

    [allPoint addObject:mode];

    [mode release];

}

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

{

    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView:self];

    LocationMode *mode = [[LocationModealloc] init];

    mode.x = location.x;

    mode.y = location.y;

    [allPoint addObject:mode];

    [mode release];

    [selfsetNeedsDisplay];

}


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

{

    [allLine addObject:allPoint];

    [allPoint release];

    

    [selfsetNeedsDisplay];

}


现在要做的就是 去将这些点绘制出来

- (void)drawRect:(CGRect)rect

因为我们每次在触摸屏幕的时候,在touchend 的时候才会将 点数组 添加到 allLine 数组中,所以要分两次绘制,就是之前的所有路径,当前正在绘制的路径。


具体代码:

 CGContextRef cgContext = UIGraphicsGetCurrentContext();

    //设置笔冒

CGContextSetLineCap(cgContext,kCGLineCapRound);

//设置画线的连接处 拐点圆滑

CGContextSetLineJoin(cgContext,kCGLineJoinRound);

    CGContextBeginPath(cgContext);

    if(allLine && allLine.count > 0)

    {

        for(int j =0;j<allLine.count;j++)

        {

            NSMutableArray *array = [allLineobjectAtIndex:j];

            if(array)

            {

                [array retain];

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

                {

                    LocationMode *location = [arrayobjectAtIndex:i];

                    if (i == 0)

                    {   

                        CGContextMoveToPoint(cgContext, location.x, location.y);

                    }

                    else

                    {

                        CGContextAddLineToPoint(cgContext, location.x, location.y);

                    }

                }

                [array release];

            }

        }

    }

    if(allPoint &&allPoint.count >0)

    {

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

        {

            LocationMode *location = [allPointobjectAtIndex:i];

            

            if (i == 0)

            {

                CGContextMoveToPoint(cgContext, location.x, location.y);

            }

            else

            {

                CGContextAddLineToPoint(cgContext, location.x, location.y);

            }      

        }

    }

    

    CGContextSetFillColorWithColor(cgContext, [[UIColorblueColor] CGColor]);

    CGContextSetStrokeColorWithColor(cgContext, [UIColorredColor].CGColor);

    CGContextSetLineWidth(cgContext,3.0);

    CGContextStrokePath(cgContext);




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值