如何使用coregraphics创建路径?

编程入门 行业动态 更新时间:2024-10-14 10:43:28
本文介绍了如何使用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 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创建路径?

本文发布于:2023-11-24 12:40:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   路径   coregraphics

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!