gpt4 book ai didi

ios - 如何使用 UIView 画线?

转载 作者:行者123 更新时间:2023-11-28 12:16:57 27 4
gpt4 key购买 nike

我可以使用 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()
}

最佳答案

  1. CAShapeLayer 添加到 UIView

    let shapeLayer = CAShapeLayer()
    view.layer.addSublayer(shapeLayer)
  2. 将形状层路径设置为线条

    shapeLayer.path = // some CGPath you create

参见:https://stackoverflow.com/a/40556796/3937了解如何使用线条制作 CGPath。

关于ios - 如何使用 UIView 画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46161985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com