iOS 实现屏幕上实时绘图

@property (nonatomic,strong) CAShapeLayer *shapeLayer;
@property (nonatomic,strong) UIBezierPath *beizer;
@property (nonatomic,assign) CGPoint startPoint;
@property (nonatomic,assign) CGPoint movePoint;
@property (nonatomic,strong) CAShapeLayer *shapelayer;

#pragma mark ---实现屏幕上实时绘图
-(void)drawingAtScreen{

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTouch:)];
[self addGestureRecognizer:pan];
self.beizer = [UIBezierPath bezierPath];
self.beizer = [UIBezierPath bezierPath];
[self initCAShaper];

}

-(void)initCAShaper{

self.shapeLayer = [[CAShapeLayer alloc] init];
self.shapeLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
self.shapeLayer.fillColor = nil;
self.shapeLayer.lineCap = kCALineCapRound;
self.shapeLayer.strokeColor = [UIColor cyanColor].CGColor;
self.shapeLayer.lineWidth = 2;
[self.layer addSublayer:self.shapeLayer];

}

-(void)panTouch:(UIPanGestureRecognizer *)sender{

_startPoint = [sender locationInView:self];
if (sender.state == UIGestureRecognizerStateBegan) {
    [self.beizer moveToPoint:_startPoint];
}
if (sender.state == UIGestureRecognizerStateChanged) {
    _movePoint = [sender locationInView:self];
    
    [_beizer addLineToPoint:_movePoint];
    self.shapeLayer.path = _beizer.CGPath;
    
}

}

你可能感兴趣的:(iOS 实现屏幕上实时绘图)