[英]Drawing a line in iPhone / iPad
我想开发一个应用程序,当用户可以绘制线条...但我不想绘制直线,但希望在用户绘制它时显示该行。 当用户从A点到达BI时,想要整理线(如果用户想要这样)。
为了能够做到这一点,我想将我的视图改为从0,0(左上角)到320,480(对于iPhone)和768,1024(对于iPad)(右下)的网格。
对于这个问题,我的A点在10,10点,B点在100,100点。
我的问题:
- 如何创建此网格?
- 我如何创建这些点?
- 如何在不拉直的情况下画出这条线?
- 如何绘制拉直线?
我的问题是我熟悉创建“普通”UI应用程序。 我不熟悉Open-GL等。
我希望有人可以帮助我。
最好的祝福,
保罗佩伦
你- (void)drawRect:(CGRect)rect
你的UIView
并覆盖- (void)drawRect:(CGRect)rect
方法。
在那里你抓住图形上下文:
CGContextRef context = UIGraphicsGetCurrentContext();
并使用它来进行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);
查看Quartz 2D Programming Guide了解所有细节。
当用户根据起点和终点拖动它时,您可以拖动直线使用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];
}
希望这有助于实现您的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.