我可以使用CGContext绘制线条。如何使用UIView绘制具有相同坐标的线条?我想将来移动线路,所以我需要使用UIView。
func DrawLine()
{
UIGraphicsBeginImageContext(self.view.frame.size)
imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let context = UIGraphicsGetCurrentContext()
let lineWidth : CGFloat = 5
context?.move(to: CGPoint(x: 100, y: 100))
context?.addLine(to: CGPoint(x: self.view.frame.width - 100, y: 100))
context?.setBlendMode(CGBlendMode.normal)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(lineWidth)
context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor)
context?.strokePath()
context?.move(to: CGPoint(x: self.view.frame.width/2, y: 100))
context?.addLine(to: CGPoint(x: self.view.frame.width/2, y: 700))
context?.strokePath()
imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func viewDidLoad()
{
super.viewDidLoad()
DrawLine()
}
答案 0 :(得分:2)
将CAShapeLayer
添加到UIView
let shapeLayer = CAShapeLayer()
view.layer.addSublayer(shapeLayer)
将shapelayer路径设置为行
shapeLayer.path = // some CGPath you create
请参阅:https://stackoverflow.com/a/40556796/3937了解如何使用行创建CGPath。