如何使用coregraphics创建路径? [英] how to create a path using coregraphics?

查看:97
本文介绍了如何使用coregraphics创建路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

现在在图像中看到我画了一条线。我只想检测线是否再次相交以开始点。然后用我自己想要的颜色填充该路径。另外我正在使用绘制线条的核心图形,但它在真实设备上非常慢。你能告诉我一种提高速度的方法吗?

now see in the image i've drawn a line.i just want to detect if line intersects again to start point.then fill that path with my own desired color.also i m using core graphics to draw line but it's very slow on real device.could you tell me a way to improve speed?

推荐答案

标题:

#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。这可能会变得很慢。建议包括使用NSTimer每隔x毫秒触发一次以检查是否需要重新显示,如果需要则重新显示,或仅在触摸移动了相当长的距离时重绘。

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.

这篇关于如何使用coregraphics创建路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆