代码之家  ›  专栏  ›  技术社区  ›  Rahul Vyas

如何使用CoreGraphics创建路径?

  •  1
  • Rahul Vyas  · 技术社区  · 15 年前

    我想创建一条路径。比如我触摸屏幕并在touchmove事件中绘制线。当线与起点相交时。使用任何颜色填充该路径。

    alt text http://i29.tinypic.com/x5quc6.png

    现在看图片,我画了一条线。我只想检测线是否再次相交到起点。然后用我自己想要的颜色填充路径。我也用核心图形来画线,但在实际设备上很慢。你能告诉我一个提高速度的方法吗?

    3 回复  |  直到 15 年前
        1
  •  2
  •   Ed Marty    15 年前

    页眉:

    #import <UIKit/UIKit.h>
    
    @interface myView : UIView {
        CGMutablePathRef path;
        CGPathRef drawingPath;
        CGRect start;
        BOOL outsideStart;
    }
    
    @end
    

    实施:

    #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
    

    请注意,您可能需要执行一些操作,这样就不会在每次路径更改时都调用setneedsdisplay。这会变得很慢。建议使用一个每隔x毫秒触发一次的nstimer来检查是否需要重新播放,如果需要,则执行此操作;如果触摸移动了很长一段距离,则仅重新绘制。

        2
  •  1
  •   ilagnev    15 年前

    也许对你有用 link text

        3
  •  0
  •   Ed Marty    15 年前

    核心图形绝对不应该使用 [self setNeedsDisplay] 每次图像更改时,这可能就是为什么您的代码在设备上运行得如此缓慢的原因。画画很慢。如果您使用opengl es来画线,它会更快,但代价是更难理解。