ユーザーが iPhone の画面に署名を描画できるようにしたいので、UIView のサブクラスを追加し、その 'touchesMoved' メソッドにいくつかのコードを追加します。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
firstTouch = [touch locationInView:self];
CGSize mySize = CGSizeMake(5, 5);
UIGraphicsBeginImageContext(mySize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextAddRect(ctx, CGRectMake(0, 0, 5, 5));
CGContextFillPath(ctx);
UIImage *redRect = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *redRectView = [[UIImageView alloc] initWithImage:redRect];
redRectView.center = CGPointMake(firstTouch.x, firstTouch.y);
[self addSubview:redRectView];
}
小さな長方形で描いていますが、ドットバイドットであることがわかりました。あまりにも醜いので、署名を線で描きたいと思います。しかし、firstTouch と lastTouch を区別する方法は? 「touchesMoved」メソッドのみを使用すると、1 つのタッチ ポイントしか取得できません。