我有一个非常大的 UIView,大小约为 3000x3000。在这个大视图中,我在 iPad 上使用触控笔或手指进行了一些自由形式的绘图。这是我的触摸开始和移动方法的代码。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
Line_Point * to_be_added = [[Line_Point alloc] init];
to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];
UITouch *touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
to_be_added.point = [NSValue valueWithCGPoint:(location)];
if (self.points == nil)
{
NSMutableArray *newPoints = [[NSMutableArray alloc] init];
self.points = newPoints;
[newPoints release];
}
[self.points addObject:to_be_added];
}
这是我的触摸移动方法,这似乎是导致问题的原因,因为应用程序调用 setNeedsDisplay 调用它的次数不够
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
Line_Point * to_be_added = [[Line_Point alloc] init];
to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];
UITouch * touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
to_be_added.point = [NSValue valueWithCGPoint:(location)];
[self.points addObject:to_be_added];
[self setNeedsLayout];
}
现在这是我的 drawRect 方法,它显示绘图
-(void)drawRect:(CGRect)rect
{
if (self.points.count == 0)
{
}
else
{
Line_Point * first = [self.points objectAtIndex:0];
CGContextRef context2 = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context2, first.line_color.CGColor);
if ([first.line_pattern isEqualToString:@"normal"])
{
CGContextSetLineWidth(context2, 4.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([first.line_pattern isEqualToString:@"thick"])
{
CGContextSetLineWidth(context2, 6.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([first.line_pattern isEqualToString:@"dotted"])
{
CGFloat Pattern[] = {5, 5, 5, 5};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
else if([first.line_pattern isEqualToString:@"super_dotted"])
{
CGFloat Pattern[] = {1, 2, 1, 2};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
CGPoint firstPoint2 = [first.point CGPointValue];
CGContextBeginPath(context2);
CGContextMoveToPoint(context2, firstPoint2.x, firstPoint2.y);
int i2 = 1;
while (i2 < self.points.count)
{
Line_Point * nextPoint = [self.points objectAtIndex:i2];
if (nextPoint.point.CGPointValue.x < 0 && nextPoint.point.CGPointValue.y < 0)
{
CGContextDrawPath(context2, kCGPathStroke);
if (i2 < (self.points.count-1))
{
CGContextBeginPath(context2);
Line_Point * nextPoint2 = [self.points objectAtIndex:i2+1];
CGContextMoveToPoint(context2, nextPoint2.point.CGPointValue.x, nextPoint2.point.CGPointValue.y);
CGContextSetStrokeColorWithColor(context2, nextPoint2.line_color.CGColor);
if ([nextPoint2.line_pattern isEqualToString:@"normal"])
{
CGContextSetLineWidth(context2, 4.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([nextPoint2.line_pattern isEqualToString:@"thick"])
{
CGContextSetLineWidth(context2, 6.0);
CGContextSetLineDash(context2, 0, NULL, 0);
}
else if([nextPoint2.line_pattern isEqualToString:@"dotted"])
{
CGFloat Pattern[] = {5, 5, 5, 5};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
else if([nextPoint2.line_pattern isEqualToString:@"super_dotted"])
{
CGFloat Pattern[] = {1, 2, 1, 2};
CGContextSetLineDash(context2, 0, Pattern, 4);
}
i2 = i2 + 2;
}
else
i2++;
}
else
{
CGContextAddLineToPoint(context2, nextPoint.point.CGPointValue.x, nextPoint.point.CGPointValue.y);
i2++;
}
}
CGContextDrawPath(context2, kCGPathStroke);
}
在自由形式模式下绘图时,该应用程序令人难以置信地滞后。我相信这是因为视图太大并且每次调用 touchesmoved 方法时 setNeedsDisplay 都会重新加载整个视图。有没有一种方法可以加快 setNeedsDisplay 方法的速度,这样自由形式的绘图就不会在 iPad 上滞后。我可以只针对发生绘图的上下文而不是重新加载整个内容吗?当我将大小调整到大约 1000x1000 时,它没有滞后问题。
感谢您对此事的任何帮助。
编辑尝试使用 -SetNeedsDisplayinRect
UIView 包装在 UISCrollview 中,代码如下。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
Line_Point * to_be_added = [[Line_Point alloc] init];
to_be_added.line_color = [UIColor colorWithCGColor: current_color.CGColor];
to_be_added.line_pattern = [NSString stringWithString: current_line_pattern];
UITouch * touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
to_be_added.point = [NSValue valueWithCGPoint:(location)];
[self.points addObject:to_be_added];
CGRect visibleRect;
visibleRect.origin = big_view.scrollable_view.contentOffset;
visibleRect.size = CGSizeMake(950, 500);
[self setNeedsDisplayInRect:visibleRect];
}
这种方法也无济于事。
如果您能提供帮助,再次感谢。