我想让的应用绘图形屏幕上通过触摸它.

我可以得出一条从一点向另一个,但它擦除对每一个新的绘制。

这里是我的代码:

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;
}

感谢你的帮助-

国家清单报告。

有帮助吗?

解决方案

如你已经发现,-drawRect是你显示视图的内容。你只将“看见”在屏幕上你画在这里。

这是更加级低不是一些像Flash,您可以添加包含一行到舞台一段时间后,一个影片剪辑添加其他影片剪辑包含一行到舞台,现在你看到的 - 两条线!

您需要做一些工作,像prdobably建立东西..

- (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。使用这种方法,你不画内 - - (空)的drawRect在所有 - 添加和删除图层,绘制你里面他们喜欢什么,并认为将处理一起合成它们,并根据需要在屏幕上绘图。可能更多的是你所期待的。

其他提示

每一次drawRect被调用时,你开始用一张白纸。如果你没有保持跟踪你之前已经引起一切为了再次战平,那么你最终只绘制你的手指的最新刷卡,而不是任何旧的。你将不得不跟踪所有手指挥动,以便每个drawRect被调用时重绘它们。

而不是重新划分每一条线,可以绘制成图像,然后只是显示器的图像在你的 drawRect: 法。图像将会积累的行为。当然,这种方法使得撤消更难以实现。

iPhone应用程序指南:

使用UIGraphicsBeginImageContext 功能,以创建一个新的基于图像的 图形的上下文。在创建这个 背景下,你可以画你的图像 内容纳入它,然后使用 UIGraphicsGetImageFromCurrentImagecontext 功能产生图像的基础上 什么,你提请。(如果需要的话,你可以 甚至继续绘制和产生 其他图像。) 当您完成 创建图像,使用 UIGraphicsEndImageContext功能 靠近图形上下文。如果你 更喜欢使用核心的图形,你可以 使用CGBitmapContextCreate功能 创建一个图形上下文 和绘制图像内容列入其中。当你完成的绘图,使用 CGBitmapContextCreateImage功能 创建一个CGImageRef从位 上下文。你可以借鉴的核心 图像直接或使用此它 初始化UIImage对象。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top