【问题标题】:Swift - drawing lines from one point to anotherSwift - 从一个点画线到另一个点
【发布时间】:2015-01-22 15:35:51
【问题描述】:

从一点到另一点画线的最佳方法是什么?我想画一条线,这是通往下一点的最有效方式。我应该使用 NSBezierPath 吗?

我将如何开始或开始学习?

【问题讨论】:

  • 为什么不直接使用CGContextMoveToPoint& CGContextAddLineToPoint?还是我错过了什么?
  • 我会调查的。我是动画和线条的新手。
  • 在不知道您的意图或至少有一些代码的情况下很难给您建议。
  • 我的意图是在绕过障碍物时从一个点到另一个点画一条线。我正在研究向用户展示绕过某个区域的最佳方式。

标签: swift cgpath


【解决方案1】:

Sebastian 在这点上是完全正确的;不要让它变得比需要的更难。为了添加更多细节,以下代码在窗口周围绘制了一个框:

CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];

NSRect nsBounds = [self bounds];                                                 // Get the bounds
CGRect cgBounds = NSRectToCGRect(nsBounds);                                      // Sets the graphics bounds

// ******************************************** Box the Window in Gray ********************************************

CGContextMoveToPoint(context, 0.0,1.0);
CGContextAddLineToPoint(context,0.0, cgBounds.size.height - 1.0);
CGContextAddLineToPoint(context,cgBounds.size.width - 2.0, cgBounds.size.height - 1.0);
CGContextAddLineToPoint(context,cgBounds.size.width - 2.0, 1.0);
CGContextAddLineToPoint(context,0.0, 1.0);
CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context, 0.7, 0.7, 0.7, 1.0);
CGContextStrokePath(context);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多