美文网首页
简单画板(用UIBezierPath和CAShapeLayer几

简单画板(用UIBezierPath和CAShapeLayer几

作者: 开发者老岳 | 来源:发表于2023-08-09 08:18 被阅读0次
@interface CustomView ()

@property (nonatomic, strong) CAShapeLayer *shaperLayer;
@property (nonatomic, strong) UIBezierPath *bezierPath;

@end



@implementation CustomView

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint startPoint = [touch locationInView:self];

    self.bezierPath = [UIBezierPath bezierPath];
    [self.bezierPath moveToPoint:startPoint]; //起始点
    
    self.shaperLayer = [[CAShapeLayer alloc] init];
    self.shaperLayer.fillColor = [[UIColor clearColor] CGColor];
    self.shaperLayer.strokeColor = [[UIColor greenColor] CGColor];
    self.shaperLayer.lineWidth = 20;
    self.shaperLayer.lineCap = kCALineCapRound; //端点圆角
    self.shaperLayer.lineJoin = kCALineJoinRound; //连接点圆角

    [self.layer addSublayer:self.shaperLayer];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint currentPoint = [touch locationInView:self];
    [self.bezierPath addLineToPoint:currentPoint]; //连接点

    self.shaperLayer.path = self.bezierPath.CGPath;
}

@end

相关文章

网友评论

      本文标题:简单画板(用UIBezierPath和CAShapeLayer几

      本文链接:https://www.haomeiwen.com/subject/iulgpdtx.html