I would like to develop an app when the user can draw lines... but I do not want to draw straight lines but want to show the line as the users draws it. When the user gets from point A to B I would like to straighten the line (if the users wants this).
我想开发一个应用程序,当用户可以绘制线条...但我不想绘制直线,但想要在用户绘制它时显示该行。当用户从A点到B点时,我想拉直线(如果用户想要这样)。
To be able to do this I want to change my view into a grid starting at 0,0 (top left) and ending at 320,480 (for iPhone) and 768,1024 (for iPad) (bottom right).
为了能够做到这一点,我想将我的视图改为从0,0(左上)开始到320,480(对于iPhone)和768,1024(对于iPad)(右下)的网格。
For this question I have point A at 10,10 and point B at 100,100.
对于这个问题,我的A点在10,10点,B点在100,100点。
My question:
- How do I create this grid?
- How do I create these points?
- How do I draw this line without straightening it?
- How do I draw the straighten line?
我的问题: - 我如何创建这个网格? - 我如何创建这些点? - 如何在不拉直的情况下画出这条线? - 如何绘制拉直线?
My problem is that I am familiar with creating "normal" UI apps. I am not familiar with Open-GL ect.
我的问题是我熟悉创建“普通”UI应用程序。我不熟悉Open-GL等。
I hope someone can help me with this.
我希望有人可以帮助我。
Best regards,
Paul Peelen
最诚挚的问候,Paul Peelen
2 个解决方案
#1
17
You subclass your UIView
and override the - (void)drawRect:(CGRect)rect
method.
你继承你的UIView并覆盖 - (void)drawRect:(CGRect)rect方法。
In there you grab a graphics context:
在那里你抓住一个图形上下文:
CGContextRef context = UIGraphicsGetCurrentContext();
And you use that to make Core Graphics calls, like:
并使用它来进行Core Graphics调用,例如:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath (context);
for (k = 0; k < count; k += 2) {
CGContextMoveToPoint(context, s[k].x, s[k].y);
CGContextAddLineToPoint(context, s[k+1].x, s[k+1].y);
}
CGContextStrokePath(context);
Look up the Quartz 2D Programming Guide for all the details.
查看Quartz 2D Programming Guide了解所有细节。
#2
0
You can drag straight line when user drag it based on starting and ending point draw a line using UIBezierPath and CAShapeLayer:
当用户根据起点和终点拖动它时,你可以拖动直线使用UIBezierPath和CAShapeLayer画一条线:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
startingPoint = [touch locationInView:baseHolderView];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
endingPoint = [touch locationInView:baseHolderView];
[self makeLineLayer:baseHolderView.layer lineFromPointA:startingPoint toPointB:endingPoint];
}
-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB
{
CAShapeLayer *line = [CAShapeLayer layer];
UIBezierPath *linePath=[UIBezierPath bezierPath];
[linePath moveToPoint: pointA];
[linePath addLineToPoint:pointB];
line.path=linePath.CGPath;
line.fillColor = nil;
line.opacity = 2.0;
line.strokeColor = [UIColor blackColor].CGColor;
[layer addSublayer:line];
}
Hope this will help to achieve your goal.
希望这有助于实现您的目标。
#1
17
You subclass your UIView
and override the - (void)drawRect:(CGRect)rect
method.
你继承你的UIView并覆盖 - (void)drawRect:(CGRect)rect方法。
In there you grab a graphics context:
在那里你抓住一个图形上下文:
CGContextRef context = UIGraphicsGetCurrentContext();
And you use that to make Core Graphics calls, like:
并使用它来进行Core Graphics调用,例如:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath (context);
for (k = 0; k < count; k += 2) {
CGContextMoveToPoint(context, s[k].x, s[k].y);
CGContextAddLineToPoint(context, s[k+1].x, s[k+1].y);
}
CGContextStrokePath(context);
Look up the Quartz 2D Programming Guide for all the details.
查看Quartz 2D Programming Guide了解所有细节。
#2
0
You can drag straight line when user drag it based on starting and ending point draw a line using UIBezierPath and CAShapeLayer:
当用户根据起点和终点拖动它时,你可以拖动直线使用UIBezierPath和CAShapeLayer画一条线:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
startingPoint = [touch locationInView:baseHolderView];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
endingPoint = [touch locationInView:baseHolderView];
[self makeLineLayer:baseHolderView.layer lineFromPointA:startingPoint toPointB:endingPoint];
}
-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB
{
CAShapeLayer *line = [CAShapeLayer layer];
UIBezierPath *linePath=[UIBezierPath bezierPath];
[linePath moveToPoint: pointA];
[linePath addLineToPoint:pointB];
line.path=linePath.CGPath;
line.fillColor = nil;
line.opacity = 2.0;
line.strokeColor = [UIColor blackColor].CGColor;
[layer addSublayer:line];
}
Hope this will help to achieve your goal.
希望这有助于实现您的目标。