ios - 在 iOS 中使用触摸绘制水平或垂直线

标签 ios swift paint

我正在做一个项目,我希望如果用户触摸在水平方向移动,那么应该绘制水平线,如果用户触摸在垂直方向移动,那么应该绘制垂直线。请建议一些使用 Swift 的解决方案。 我在下面试过。但这是画自由线。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        super.touchesBegan(touches, with: event)

        let touch: AnyObject? = touches.first
        let lastPoint = touch!.previousLocation(in: holderView)
        path.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))

    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        super.touchesMoved(touches, with: event)

        let touch: AnyObject? = touches.first
        let currentPoint = touch!.location(in: holderView)

        path.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))

        //Design path in layer
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor =  UIColor.orange.cgColor
        shapeLayer.lineWidth = 20.0

        holderView.layer.addSublayer(shapeLayer)

}

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        super.touchesEnded(touches, with: event)
         path=UIBezierPath()
    }

最佳答案

Try this.

class DrawingView: UIView {
    var path = UIBezierPath()
    var initialLocation = CGPoint.zero
    var finalLocation = CGPoint.zero
    var shapeLayer = CAShapeLayer()

    override func awakeFromNib() {
        super.awakeFromNib()
        setupView()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupView(){
        self.layer.addSublayer(shapeLayer)
        self.shapeLayer.lineWidth = 20
        self.shapeLayer.strokeColor = UIColor.blue.cgColor
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if let location = touches.first?.location(in: self){
            initialLocation = location
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)

        if let location = touches.first?.location(in: self){
            let dx =  location.x - initialLocation.x
            let dy = location.y - initialLocation.y

            finalLocation = abs(dx) > abs(dy) ? CGPoint(x: location.x, y: initialLocation.y) : CGPoint(x: initialLocation.x, y: location.y)

            path.removeAllPoints()
            path.move(to: initialLocation)
            path.addLine(to: finalLocation)

            shapeLayer.path = path.cgPath

        }
    }
}

关于ios - 在 iOS 中使用触摸绘制水平或垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40994252/

相关文章:

ios - 具有默认参数的 swift 函数也是一个选择器?

xcode - 模块 bolt 的伞头不包括头 'BFAppLinkResolving.h'

ios - 删除后重新加载应用程序时, TableView 中的核心数据条目会出现

ios - Bolts框架任务队列

ios - TableView 中的控制 "Program Flow"

java - JLayeredPane 不绘图

java - 添加 pacman 图像

java - JInternalFrame 图像显示;

ios - 以编程方式添加 UITableView 会在单元格左侧留下较大空间

ios - 值保留在数组内部