[英]how to draw new lines without old ones disappering?
I want to create a simple tool for drawing. 我想创建一个简单的绘图工具。 The purpose is to draw a line that follows the accelerometer of the iPhone & iPad, so if the user tilts the device a line will be draw in the direction the device was moved. 目的是在iPhone和iPad的加速度计后面画一条线,因此,如果用户倾斜设备,则会沿设备移动的方向画一条线。
I am able to register acceleration and drawing lines. 我能够注册加速度和绘图线。 My problem is that as soon as I draw a line the old one disappears. 我的问题是,一旦我画一条线,旧的线就消失了。 One possible solution would be to save to points already drawn and then re-draw everything, but I would think there are better solutions? 一种可能的解决方案是保存到已经绘制的点,然后重新绘制所有内容,但是我认为有更好的解决方案吗?
All help is appreciated! 感谢所有帮助!
My drawRect is at the moment like this: 我的drawRect现在是这样的:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 20.0);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
}
A different method is responsible for refreshing. 另一种方法负责刷新。 This method is also called from the uiviewcontroller with certain intervals. 还从uiviewcontroller以一定间隔调用此方法。 Right now it shows a "trail" (or what I should call it) in the direction the device was moved. 现在,它在设备移动的方向上显示了一个“线索”(或我应该称呼它)。 Not exactly what I am looking for: 并非我要找的东西:
- (void)drawNewLine:(CGPoint)to {
// calculate trail behind current point
float pointDifferenceX = ((toPoint.x - to.x) * 9);
float pointDifferenceY = ((toPoint.y - to.y) * 9);
fromPoint = CGPointMake(toPoint.x + pointDifferenceX, toPoint.y + pointDifferenceY);
toPoint = to;
[self setNeedsDisplay];
}
I can think of two options: 我可以想到两种选择:
Either save all points and redraw the lines whenever the screen needs to be refreshed (as you mentioned) 保存所有点并在需要刷新屏幕时重新绘制线条(如前所述)
Draw the lines into an off-screen pixelmap and refresh the screen from there 将线条绘制到屏幕外的像素图中,然后从那里刷新屏幕
In either case, respect the Hollywood principle: Don't call, you will be called. 无论哪种情况,都要遵守好莱坞原则:不要打电话,你会被打电话。 That means don't just draw to the screen but wait for until drawRect:
of your UIView
is called. 这意味着不仅要绘制到屏幕上,还要等到UIView
drawRect:
被调用。 (You can trigger this by calling setNeedsDisplay.) (您可以通过调用setNeedsDisplay触发此操作。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.