我想创建一个路径。就像我触摸屏幕并在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 i29.tinypic/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创建路径?
发布评论