我有一個非常大的UIView大小約3000x3000。在這個大視圖中,我使用觸筆或手指在iPad上做了一些自由形式的繪圖。這裏是我的觸摸代碼開始並移動方法。setNeedsDisplay在一個大的UIView導致
-(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);
}
當在自由格式模式下繪製時,應用程序令人難以置信地滯後。我相信這是因爲視圖太大,並且setNeedsDisplay在每次調用觸摸移動方法時重新加載整個視圖。有沒有辦法加快setNeedsDisplay方法,使自由格式繪圖不會滯後於iPad。我可以只針對發生繪圖的上下文不重新加載整個事情嗎?當我將尺寸調整到大約1000x1000時,它沒有滯後問題。
感謝您對此事的任何幫助。
EDIT TRYED USING -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];
}
這種方法也沒有幫助。
如果可以幫忙,再次感謝。
我使用了上述方法,它似乎沒有工作。矩形沒有用。我甚至確定矩形是正確的。將編輯上面的代碼。 – Krzemienski
我發現這個[鏈接](https://discussions.apple.com/thread/1630986?start=0&tstart=0)和其他一些,他們說在scrollView的大視圖問題。我會更新回答 – medvedNick
AWSOME!有效! – Krzemienski