how to create a path using coregraphics?

前端 未结 3 1745
醉酒成梦
醉酒成梦 2021-02-11 08:48

i want to create a path.something like i touch the screen and draw line in touchmove event.when line intersect from starting point.fill that path using any colour.

3条回答
  •  梦毁少年i
    2021-02-11 09:02

    Header:

    #import 
    
    @interface myView : UIView {
        CGMutablePathRef path;
        CGPathRef drawingPath;
        CGRect start;
        BOOL outsideStart;
    }
    
    @end
    

    Implementation:

    #import "myView.h"
    
    @implementation myView
    
    - (id) init {
        if (self = [super init]) {
            self.userInteractionEnabled = YES;
            self.multipleTouchEnabled = NO;
        }
    }
    
    - (void) finishPath {
        if (drawingPath) {
            CGPathRelease(drawingPath);
        }
        CGPathCloseSubpath(path);
        drawingPath = CGPathCreateCopy(path);
        CGPathRelease(path);
        path = NULL;
        [self setNeedsDisplay];
        return;
    }
    
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        path = CGPathCreateMutable();
        UITouch *t = [touches anyObject];
        CGPoint p = [t locationInView:self];
        start = CGRectZero;
        start.origin = p;
        start = CGRectInset(start,-10, -10);
        outsideStart = NO;
        CGPathMoveToPoint(path, NULL, p.x, p.y);
    }
    
    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        if (!path) {
            return;
        }
        UITouch *t = [touches anyObject];
        CGPoint p = [t locationInView:self];
        if (CGRectContainsPoint(start,p)) {
            if (outsideStart) {
                [self finishPath];
            }
        } else {
            outsideStart = YES;
        }
        CGPathAddLineToPoint(path,NULL,p.x,p.y);
        [self setNeedsDisplay];
    }
    
    - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        if (!path) {
            return;
        }
        [self finishPath];
    }
    
    - (void) touchesCanceled:(NSSet *)touches withEvent:(UIEvent *)event {
        if (!path) {
            return;
        }
        CGPathRelease(path);
        path = NULL;
    }
    
    - (void) drawInRect:(CGRect)rect {
        CGContextRef g = UIGraphicsGetCurrentContext();
        if (drawingPath) {
            CGContextAddPath(g,drawingPath);
            [[UIColor redColor] setFill];
            [[UIColor blackColor] setStroke];
            CGContextDrawPath(g,kCGPathFillStroke);
        }
        if (path) {
            CGContextAddPath(g,path);
            [[UIColor blackColor] setStroke];
            CGContextDrawPath(g,kCGPathStroke);
        }
    }
    
    - (void) dealloc {
        if (drawingPath) {
            CGPathRelease(drawingPath);
        }
        if (path) {
            CGPathRelease(path);
        }
        [super dealloc];
    }
    
    @end
    

    Note that you will probably want to do something so you aren't actually calling setNeedsDisplay every time the path changes. This can get very slow. Suggestions include having an NSTimer that fires every x milliseconds to check if it needs to redisplay and do so if it does, or only redrawing if the touch has moved a significant distance.

提交回复
热议问题