how to create a path using coregraphics?

给你一囗甜甜゛ 提交于 2019-12-03 10:06:34

Header:

#import <UIKit/UIKit.h>

@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.

Maybe it can be useful to you link text

Core Graphics should definitely not be using [self setNeedsDisplay] every time the image changes, which is probably why your code is so slow on the device. Drawing is slow. If you use OpenGL ES to draw the lines, it will be much quicker, at the expense of being more difficult to understand.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!