绘制形状时线条被擦除

发布于 2024-08-09 17:53:33 字数 971 浏览 18 评论 0 原文

我正在尝试制作一个通过触摸在屏幕上绘制形状的应用程序。

我可以从一个点到另一个点画一条线,但每次新画时它都会被擦除。

这是我的代码:

CGPoint location;
CGContextRef context;
CGPoint drawAtPoint;
CGPoint lastPoint;
-(void)awakeFromNib{
    //[self addSubview:noteView];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    location = [touch locationInView:touch.view];
    [self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)];
}

- (void)drawRect:(CGRect)rect {
    context = UIGraphicsGetCurrentContext();
    [[UIColor blueColor] set];
    CGContextSetLineWidth(context,10);
    drawAtPoint.x =location.x;
    drawAtPoint.y =location.y;
    CGContextAddEllipseInRect(context,CGRectMake(drawAtPoint.x, drawAtPoint.y, 2, 2));
    CGContextAddLineToPoint(context,lastPoint.x, lastPoint.y);
    CGContextStrokePath(context);

    lastPoint.x =location.x;
    lastPoint.y =location.y;
}

感谢您的帮助 -

Nir。

I am trying to make an application for drawing shapes on screen by touching it.

I can draw a line from one point to another- but it erases on each new draw.

Here is my code:

CGPoint location;
CGContextRef context;
CGPoint drawAtPoint;
CGPoint lastPoint;
-(void)awakeFromNib{
    //[self addSubview:noteView];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    location = [touch locationInView:touch.view];
    [self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)];
}

- (void)drawRect:(CGRect)rect {
    context = UIGraphicsGetCurrentContext();
    [[UIColor blueColor] set];
    CGContextSetLineWidth(context,10);
    drawAtPoint.x =location.x;
    drawAtPoint.y =location.y;
    CGContextAddEllipseInRect(context,CGRectMake(drawAtPoint.x, drawAtPoint.y, 2, 2));
    CGContextAddLineToPoint(context,lastPoint.x, lastPoint.y);
    CGContextStrokePath(context);

    lastPoint.x =location.x;
    lastPoint.y =location.y;
}

Appreciate your help-

Nir.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界和平 2024-08-16 17:53:33

正如您所发现的,-drawRect 是显示视图内容的地方。您只能在屏幕上“看到”您在此处绘制的内容。

这比 Flash 之类的东西要低级得多,在 Flash 中,您可能会向舞台添加一个包含一行的影片剪辑,一段时间后将另一个包含一行的影片剪辑添加到舞台,现在您会看到 - 两行!

您将需要做一些工作,并且可能需要设置类似的东西。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    location = [touch locationInView:touch.view]; 

    [self addNewLineFrom:lastPoint to:location];

    lastPoint = location;

    [self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)]; 
}

- (void)drawRect:(CGRect)rect {

    context = UIGraphicsGetCurrentContext();

    for( Line *eachLine in lineArray )
        [eachLine drawInContext:context];

}

我想您可以看到如何将其充实到您需要的内容。

解决此问题的另一种方法是使用 CALayers。使用这种方法,您根本不需要在内部绘制 - (void)drawRect - 您可以添加和删除图层,在其中绘制您喜欢的内容,视图将处理将它们合成在一起并根据需要绘制到屏幕上。可能更多的是您正在寻找的东西。

As you have discovered, -drawRect is where you display the contents of a view. You will only 'see' on screen what you draw here.

This is much more low-level than something like Flash where you might add a movieclip containing a line to the stage and some time later add another movieclip containing a line to the stage and now you see - two lines!

You will need to do some work and prdobably set up something like..

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    location = [touch locationInView:touch.view]; 

    [self addNewLineFrom:lastPoint to:location];

    lastPoint = location;

    [self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)]; 
}

- (void)drawRect:(CGRect)rect {

    context = UIGraphicsGetCurrentContext();

    for( Line *eachLine in lineArray )
        [eachLine drawInContext:context];

}

I think you can see how to flesh this out to what you need.

Another way to approach this is to use CALayers. Using this approach you dont draw inside - - (void)drawRect at all - you add and remove layers, draw what you like inside them, and the view will handle compositing them together and drawing to the screen as needed. Probably more what you are looking for.

倒带 2024-08-16 17:53:33

每次调用 drawRect 时,您都会从一张白纸开始。如果您不记录之前绘制的所有内容以便再次绘制,那么您最终只会绘制手指的最新滑动,而不是任何旧的滑动。您必须跟踪所有手指滑动操作,以便在每次调用 drawRect 时重新绘制它们。

Every time that drawRect is called, you start with a blank slate. If you don't keep track of everything you have drawn before in order to draw it again, then you end up only drawing the latest swipe of your finger, and not any of the old ones. You will have to keep track of all of your finger swipes in order to redraw them every time drawRect is called.

我家小可爱 2024-08-16 17:53:33

您可以绘制图像,然后在 drawRect: 方法中显示该图像,而不是重新绘制每一行。图像将为您累积线条。当然,这种方法使得undo的实现更加困难。

来自iPhone应用程序编程指南

使用 UIGraphicsBeginImageContext
函数创建一个新的基于图像的
图形上下文。创建此后
上下文,你可以画你的图像
内容进入其中,然后使用
UIGraphics从当前图像上下文获取图像
生成图像的函数
你画了什么。 (如果需要,您可以
甚至继续绘图并生成
其他图像。)完成后
创建图像,使用
UIGraphicsEndImageContext 函数
关闭图形上下文。如果你
更喜欢使用核心显卡,您可以
使用 CGBitmapContextCreate 函数
创建位图图形上下文
并将您的图像内容绘制到其中。
完成绘图后,使用
CGBitmapContextCreateImage 函数
从位图创建 CGImageRef
语境。你可以画出核心
直接图形图像还是用这个吧
初始化 UIImage 对象。

Instead of redrawing every line, you can draw into an image and then just display the image in your drawRect: method. The image will accumulate the lines for you. Of course, this method makes undo more difficult to implement.

From the iPhone Application Programming Guide:

Use the UIGraphicsBeginImageContext
function to create a new image-based
graphics context. After creating this
context, you can draw your image
contents into it and then use the
UIGraphicsGetImageFromCurrentImageContext
function to generate an image based on
what you drew. (If desired, you can
even continue drawing and generate
additional images.) When you are done
creating images, use the
UIGraphicsEndImageContext function to
close the graphic context. If you
prefer using Core Graphics, you can
use the CGBitmapContextCreate function
to create a bitmap graphics context
and draw your image contents into it.
When you finish drawing, use the
CGBitmapContextCreateImage function to
create a CGImageRef from the bitmap
context. You can draw the Core
Graphics image directly or use this it
to initialize a UIImage object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文