我有一个 UIViewController
。如何在其中一个以编程方式创建的 View 中画一条线?
最佳答案
有两种常见的技术。
使用 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设置)。
另一种方法是子类 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 版本如下:
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)
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/
相关文章:
iphone - UITableView 单元格中的 UISwitch
objective-c - ARC : strong or retain? 的@property 定
ios - 为什么masksToBounds = YES会阻止CALayer阴影?
ios - 如何使用 UISegmentedControl 切换 View ?
objective-c - 如何向 objc_exception_throw 添加断点?
ios - NSOperation 和 NSOperationQueue 工作线程与主线程
objective-c - Objective-C中的ivars和属性有什么区别
iphone - 如何找到 malloc "double free"错误的原因?