iOS简单的画板

2018-02-11  本文已影响0人  Desert_Eagle
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UIBezierPath *path;
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.frame = self.view.bounds;
    self.shapeLayer.strokeColor = [UIColor redColor].CGColor;
    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.shapeLayer.lineJoin = kCALineJoinRound;
    self.shapeLayer.lineCap = kCALineCapRound;
    self.shapeLayer.lineWidth = 5;
    [self.view.layer addSublayer:_shapeLayer];
    
    self.path = [[UIBezierPath alloc] init];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    [self.path moveToPoint:point];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    [self.path addLineToPoint:point];
    
    self.shapeLayer.path = self.path.CGPath;
}


@end
上一篇 下一篇

猜你喜欢

热点阅读