我有UIViewController分.如何在其中一个以编程方式创建的视图中绘制一条线?

推荐答案

有两种常见的技术.

  1. 使用CAShapeLayer:

    • 创建UIBezierPath(将坐标替换为您想要的任何值):

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

      CAShapeLayer *shapeLayer = [CAShapeLayer layer];
      shapeLayer.path = [path CGPath];
      shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
      shapeLayer.lineWidth = 3.0;
      shapeLayer.fillColor = [[UIColor clearColor] CGColor];
      
    • 将这CAShapeLayer添加到视图的图层中:

      [self.view.layer addSublayer:shapeLayer];
      

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

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

    • 创建一个UIView子类,并定义一个drawRect来绘制您的直线.

      您可以通过核心图形来实现这一点:

      - (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];
      }
      
    • 然后,您可以使用该视图类作为NIB/情节提要或视图的基类,或者让视图控制器以编程方式将其添加为子视图:

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

上述两种方法的快速变体如下:

  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()
        }
    
    }
    

    并将其添加到视图层次 struct 中:

    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相关问答推荐

[NSJSONSerializationdataWithJSONObserver:选项:错误:]:SON写入中无效的顶级类型

如何遮罩,动画和添加阴影到CAShapeLayer?

带有RadStudio 11.3/12的Mac Mini M2(Sonoma 14.2.1)上的PAServer-测试连接按钮有效,但部署不起作用

Xamarin Forms CustomPin iOS Render

用于HDR显示的CI上下文选项

如何在集合视图中创建装饰视图?

如何在SwiftUI中实现淡入淡出加粘标题屏幕效果

NSError 错误领域: "CBATTErrorDomain" 代码: 128 0x0000000282b111a0

Strange UIView.animate更改UIButton标题 colored颜色 的行为

SwiftUI withAnimation 在视图出现之前不会启动

SwiftUI 切换语句

无法为 Flutter 项目构建 ipa [CocoaPods 找不到 pod "GoogleAppMeasurement" 的兼容版本:]

通过按钮打开 AppStore

光标未显示在 UITextView 中

每个版本的 iOS 都附带什么版本的移动 Safari?

Xcode:此时无法安装此应用程序.

在 React Native 中保存敏感数据

iTunes Connect 中的无限制 Web 访问是什么意思

在 iOS 上没有显示数字键盘

打开 XIB 文件后 Xcode 6.3 冻结/挂起