У меня очень большой 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);
}
Приложение невероятно тормозит при рисовании в режиме произвольной формы. Я считаю, что это связано с тем, что представление слишком велико, а 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];
}
Такой подход тоже не помогает.
Еще раз спасибо, если вы можете помочь.