我想让用户在 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'方法,我只能得到一个接触点。