ios - 你如何以编程方式从 View Controller 中画一条线?

标签 ios objective-c uiview uikit

我有一个 UIViewController。如何在其中一个以编程方式创建的 View 中画一条线?

最佳答案

有两种常见的技术。

  1. 使用 CAShapeLayer :

    • 创建一个UIBezierPath (用任何你想要的替换坐标):

      UIBezierPath *path = [UIBezierPath bezierPath];
      [path moveToPoint:CGPointMake(10.0, 10.0)];
      [path addLineToPoint:CGPointMake(100.0, 100.0)];
      
    • 创建一个CAShapeLayer使用那个 UIBezierPath :

      CAShapeLayer *shapeLayer = [CAShapeLayer layer];
      shapeLayer.path = [path CGPath];
      shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
      shapeLayer.lineWidth = 3.0;
      shapeLayer.fillColor = [[UIColor clearColor] CGColor];
      
    • 添加 CAShapeLayer到您的 View 层:

      [self.view.layer addSublayer:shapeLayer];
      

    在以前的 Xcode 版本中,您必须手动添加 QuartzCore.framework to your project's "Link Binary with Libraries"并导入 <QuartzCore/QuartzCore.h> .m 文件中的 header ,但这不再是必需的(如果您启用了“启用模块”和“自动链接框架”build设置)。

  2. 另一种方法是子类 UIView然后使用 CoreGraphics调用 drawRect 方法:

    • 创建一个UIView子类化并定义一个 drawRect划清界限。

      您可以使用 Core Graphics 做到这一点:

      - (void)drawRect:(CGRect)rect {
          CGContextRef context = UIGraphicsGetCurrentContext();
      
          CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
          CGContextSetLineWidth(context, 3.0);
          CGContextMoveToPoint(context, 10.0, 10.0);
          CGContextAddLineToPoint(context, 100.0, 100.0);
          CGContextDrawPath(context, kCGPathStroke);
      }
      

      或使用 UIKit :

      - (void)drawRect:(CGRect)rect {
          UIBezierPath *path = [UIBezierPath bezierPath];
          [path moveToPoint:CGPointMake(10.0, 10.0)];
          [path addLineToPoint:CGPointMake(100.0, 100.0)];
          path.lineWidth = 3;
          [[UIColor blueColor] setStroke];
          [path stroke];
      }
      
    • 然后您可以将此 View 类用作您的 NIB/ Storyboard或 View 的基类,或者您可以让您的 View Controller 以编程方式将其添加为 subview :

      PathView *pathView = [[PathView alloc] initWithFrame:self.view.bounds];
      pathView.backgroundColor = [UIColor clearColor];
      
      [self.view addSubview: pathView];
      

上述两种方法的 Swift 版本如下:

  1. CAShapeLayer :

    // create path
    
    let path = UIBezierPath()
    path.move(to: CGPoint(x: 10, y: 10))
    path.addLine(to: CGPoint(x: 100, y: 100))
    
    // Create a `CAShapeLayer` that uses that `UIBezierPath`:
    
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = UIColor.blue.cgColor
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.lineWidth = 3
    
    // Add that `CAShapeLayer` to your view's layer:
    
    view.layer.addSublayer(shapeLayer)
    
  2. UIView子类:

    class PathView: UIView {
    
        var path: UIBezierPath?           { didSet { setNeedsDisplay() } }
        var pathColor: UIColor = .blue    { didSet { setNeedsDisplay() } }
    
        override func draw(_ rect: CGRect) {
            // stroke the path
    
            pathColor.setStroke()
            path?.stroke()
        }
    
    }
    

    并将其添加到您的 View 层次结构中:

    let pathView = PathView()
    pathView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(pathView)
    
    NSLayoutConstraint.activate([
        pathView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        pathView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        pathView.topAnchor.constraint(equalTo: view.topAnchor),
        pathView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])
    
    pathView.backgroundColor = .clear
    
    let path = UIBezierPath()
    path.move(to: CGPoint(x: 10, y: 10))
    path.addLine(to: CGPoint(x: 100, y: 100))
    path.lineWidth = 3
    
    pathView.path = path
    

    在上面,我要添加 PathView以编程方式,但您也可以通过 IB 添加它,只需设置它的 path以编程方式。

关于ios - 你如何以编程方式从 View Controller 中画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16846413/

相关文章:

ios - 为 UIView 使用 Nib 文件——布局问题

iphone - 在 USplitView iOS 5.1 中加载和隐藏主视图

ios - 如何在 swift 3 中使用 Backendless REST API

ios - 如何顺利移动 UIImageViews 数组?

iphone - 如何在表格单元格按钮中执行操作

ios - 在 Swift 中使用带有有效 SSL 证书的 NSURLConnection

iphone - 使用 xcode 4.2 和 Storyboard 通过代码更改 View ?

iphone - 如何删除应用程序指定目录中的所有文件?

ios - 错误的接收器类型 'CGImageRef'(又名 'CGImage *')

ios - 自定义 UIView 类 - Swift