垂直使用uibezierPath绘制线

问题描述

下面的我的快速代码绘制了一条水平线,即水平线的角度不变。像x轴一样思考。我想以完全相反的方式画一条线。想想y轴。线画在

bezier.addLine(至:CGPoint(x:point.x,y:startPoint!.y))

    import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!
    
    var startPoint: CGPoint?

    let shapeLayer: CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        shapeLayer.lineWidth = 4
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = UIColor.blue.cgColor
        return shapeLayer
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.backgroundColor = .systemOrange
        imageView.layer.addSublayer(shapeLayer)
    }

    override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
        let touch = touches.first
        startPoint = touch?.location(in: imageView)
    }

    override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
        guard var touch = touches.first else { return }

        if let predicted = event?.predictedTouches(for: touch)?.last {
            touch = predicted
        }

        updatePath(in: imageView,to: touch)
    }

    override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
        guard let touch = touches.first else { return }
        updatePath(in: imageView,to: touch)
        let image = UIGraphicsImageRenderer(bounds: imageView.bounds).image { _ in
            imageView.drawHierarchy(in: imageView.bounds,afterScreenUpdates: true)
        }
        shapeLayer.path = nil
        imageView.image = image
    }
}

private extension ViewController {
    func updatePath(in view: UIView,to touch: UITouch) {
        let point = touch.location(in: view)
        guard view.bounds.contains(point) else { return }

        let bezier = UIBezierPath()

        bezier.move(to: startPoint!)
        bezier.addLine(to: CGPoint(x: point.x,y: startPoint!.y))

        shapeLayer.path = bezier.cgPath
    }
}

解决方法

实际上,您移至startPoint并尝试将线添加到同一静态点上……如何将线添加到您当前所在的同一点上……向Y添加一些值,而要添加行的静态X位置

 bezier.move(to: startPoint!)
  bezier.addLine(to: CGPoint(x: startPoint!.x,y: point.y))

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...