ios-day17-03(使用UIBezierPath实现“涂鸦”小程序)

源码下载地址:http://download.csdn.net/detail/liu537192/8546721


效果图同ios-day17-02


核心代码除了JLPaintView.m,其他的都与ios-day17-02一样,下面附上JLPaintView.m的代码:

//
//  JLPaintView.m
//  02-涂鸦
//
//  Created by XinYou on 15-3-30.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLPaintView.h"

@interface JLPaintView()
/**
 *  可变数组,用于存储所有的路径(每一条线表示一个路径)
 */
@property (nonatomic, strong)NSMutableArray *allPaths;

@end


@implementation JLPaintView


- (void)clear{

    // 清空存储所有路径的数组
    [self.allPaths removeAllObjects];
    
    // 重绘(刷新)
    [self setNeedsDisplay];
}

- (void)back{
    
    // 删除最后一条路径
    [self.allPaths removeLastObject];
    
    // 重绘(刷新)
    [self setNeedsDisplay];
}

- (NSMutableArray *)allPaths{

    if (_allPaths == nil) {
        _allPaths = [NSMutableArray array];
    }
    
    return _allPaths;
}
/**
 *  手指刚接触屏幕时调用
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    
    // 确定起点
    CGPoint startPoint = [touch locationInView:touch.view];

    // 每一次开始触摸,创建一个新的路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    // 设置线段头尾的样式
    path.lineCapStyle = kCGLineCapRound;
    // 设置线段转折点的样式
    path.lineJoinStyle = kCGLineJoinRound;
    
    // 设置起点
    [path moveToPoint:startPoint];
    
    // 把本次的路径(path)添加到allPaths中
    [self.allPaths addObject:path];
    
    // 重绘(刷新)
    [self setNeedsDisplay];
}

/**
 *  手指在屏幕上移动时会调用
 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    
    // 当前移动到了哪个点(当前点)
    CGPoint currentPoint = [touch locationInView:touch.view];
    
    // 取出本次路径
    UIBezierPath *path = [self.allPaths lastObject];
    
    // 连线
    [path addLineToPoint:currentPoint];
    
    // 重绘(刷新)
    [self setNeedsDisplay];
}
/**
 *  手指离开屏幕时会调用
 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [self touchesMoved:touches withEvent:event];
}

- (void)drawRect:(CGRect)rect{
   
    // 设置颜色
    [[UIColor blackColor] set];
    
    for (UIBezierPath *path in self.allPaths) {
        // 设置线宽(也可以在创建UIBezierPath对象的时候设置)
        path.lineWidth = 2;
        // 渲染
        [path stroke];
    }
}

@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值