在iOS中使用触摸绘制水平或垂直线 [英] Draw horizontal or vertical line using touch in iOS

查看:160
本文介绍了在iOS中使用触摸绘制水平或垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我希望如果用户触摸沿水平方向移动,则应绘制水平线,而用户触摸沿垂直方向移动,则应绘制垂直线.请提出一些使用Swift的解决方案. 我在下面尝试过.但这是徒劳的.

I am working on a project where I want that if user touch move in horizontal direction then horizontal line should draw and of user touch move in vertical direction then vertical line should draw. Kindly suggest some solution using Swift. I tried below. But this is drawing free line.

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中使用触摸绘制水平或垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆