DrawRect

在RootView.m里重写如下方法

- (void)drawRect:(CGRect)rect
{
    // 初始化一个路线
//    self.path = [UIBezierPath bezierPath];  // 点之间的过度效果
//    _path.lineCapStyle = kCGLineCapRound; // 设置节点过度效果
//    _path.lineJoinStyle = kCGLineJoinRound; // 设置终点过度效果
//    _path.lineWidth = 3.0; // 设置线宽
//    
//    [_path moveToPoint:CGPointMake(100, 100)];
//    
//    [_path addLineToPoint:CGPointMake(150, 150)];
//    [path addLineToPoint:CGPointMake(180, 130)];
//    [path addLineToPoint:CGPointMake(200, 170)];
//    [path addLineToPoint:CGPointMake(120, 180)];
//    
//    [path stroke];



    // 得到上下文 1 在drawRect拿到上下文 2 通过一个image图片拿到上下文
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    // 设置了画笔的颜色
//    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//    // 设置画笔的粗细
//    CGContextSetLineWidth(context, 2.0);


    for (UIBezierPath *path in self.paths) {

        [path stroke];
    }

}

- (NSMutableArray *)paths
{
    if (!_paths) {
        _paths = [NSMutableArray array];
    }
    return _paths;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 获取touch对象
    UITouch *touch = [touches anyObject];

    // 获取touch起始点
    CGPoint point = [touch locationInView:touch.view];

    // 创建路径对象
    UIBezierPath *path = [UIBezierPath bezierPath];

    path.lineWidth = 3.0;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;

    // 设置起点
    [path moveToPoint:point];

    // 添加路径
    [self.paths addObject:path];

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 获取touch对象
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:touch.view];

    UIBezierPath *path = [self.paths lastObject];
    [path addLineToPoint:point];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值