问题描述
我希望能够使用 UIBezierPath
在iPad屏幕上绘制直线。我该怎么做?
I want to be able to draw straight lines on my iPad screen using an UIBezierPath
. How would I go about this?
我想做的是这样的:我在屏幕上双击以定义起点。一旦我的手指在屏幕上方,直线就会用我的手指移动(这应该会弄清楚我应该把下一根手指放在哪里以便它会形成一条直线)。然后,如果我再次双击屏幕,则定义终点。
What I want to do is something like this: I double tap on the screen to define the start point. Once my finger is above the screen the straight line would move with my finger (this should happen to figure out where I should put my next finger so that it will create a straight line). Then, if I double tap on screen again the end point is defined.
此外,如果我双击终点,新行应该开始。
Further, a new line should begin if I double tap on the end point.
是否有任何资源我可以用来作为指导吗?
Are there any resources available that I can use for guidance?
推荐答案
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startOfLine];
[path addLineToPoint:endOfLine];
[path stroke];
编辑
- (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]];
}
}
我不会写任何少数派报告相机没有货币补偿的魔法代码。
I'm not writing any Minority Report-esque camera magic code without monetary compensation.
这篇关于如何使用UIBezierPath绘制平滑直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!