UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startOfLine];
[path addLineToPoint:endOfLine];
[path stroke];
UIBezierPath Class Reference
编辑
- (void)viewDidLoad
{
[super viewDidLoad];
// Create an array to store line points
self.linePoints = [NSMutableArray array];
// Create double tap gesture recognizer
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTap];
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateRecognized) {
CGPoint touchPoint = [sender locationInView:sender.view];
// If touch is within range of previous start/end points, use that point.
for (NSValue *pointValue in linePoints) {
CGPoint linePoint = [pointValue CGPointValue];
CGFloat distanceFromTouch = sqrtf(powf((touchPoint.x - linePoint.x), 2) + powf((touchPoint.y - linePoint.y), 2));
if (distanceFromTouch < MAX_TOUCH_DISTANCE) { // Say, MAX_TOUCH_DISTANCE = 20.0f, for example...
touchPoint = linePoint;
}
}
// Draw the line:
// If no start point yet specified...
if (!currentPath) {
currentPath = [UIBezierPath bezierPath];
[currentPath moveToPoint:touchPoint];
}
// If start point already specified...
else {
[currentPath addLineToPoint:touchPoint];
[currentPath stroke];
currentPath = nil;
}
// Hold onto this point
[linePoints addObject:[NSValue valueWithCGPoint:touchPoint]];
}
}
我不能没有金钱补偿编写任何少数派报告式的摄像头魔码。
在投票时进行某种解释是一种常态。 – ryanprayogo
@raaz一旦停止接触玻璃杯,您将无法跟踪用户的手指。那么,除非你用相机实现一些少数民族报告式的神奇魔力,但我认为这是不值得的巨大努力。其余部分很容易实现:只需将'UITouch'信息导入@ StuDev的答案(UIBezierPath'的要点)。 –