サイズが約 3000x3000 の非常に大きな UIView があります。この大きなビューでは、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];
}
このアプローチ方法も役に立ちません。
あなたが助けてくれるなら、もう一度感謝します。