私は、ユーザーが数学の問題を解こうとしているときに、ユーザーが指でスクラッチ作業を行うことができる数学関連のアクティビティを作成しています。ただし、指をすばやく動かすと、線が指の後ろに少し遅れていることに気付きます。パフォーマンスで見落としていた部分があるのか、それとも単に足りない部分があるのかと思っていましたtouchesMoved
(速く動かないと完全にスムーズで素晴らしいです)。私はを使用してUIBezierPath
います。まず、次のようにinitメソッドで作成します。
myPath=[[UIBezierPath alloc]init];
myPath.lineCapStyle=kCGLineCapSquare;
myPath.lineJoinStyle = kCGLineJoinBevel;
myPath.lineWidth=5;
myPath.flatness = 0.4;
次に、drawRectで:
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
if(baseImageView.image)
{
CGContextRef c = UIGraphicsGetCurrentContext();
[baseImageView.layer renderInContext:c];
}
CGBlendMode blendMode = self.erase ? kCGBlendModeClear : kCGBlendModeNormal;
[myPath strokeWithBlendMode:blendMode alpha:1.0];
}
baseImageViewは、結果を保存するために使用するもので、多くのパスを描画する必要がありません(しばらくすると非常に遅くなります)。これが私のタッチロジックです:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
CGContextRef c = UIGraphicsGetCurrentContext();
[self.layer renderInContext:c];
baseImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[myPath removeAllPoints];
[self setNeedsDisplay];
}
このプロジェクトはエンタープライズアプリとしてリリースされる予定なので、iPad2にのみインストールされます。ターゲットiOSは5.0です。これからもう少しスピードを絞る方法についての提案をいただければ幸いです。