Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中使用触摸键绘制水平线或垂直线_Ios_Swift_Paint - Fatal编程技术网

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

在iOS中使用触摸键绘制水平线或垂直线,ios,swift,paint,Ios,Swift,Paint,我正在做一个项目,我想如果用户触摸在水平方向移动,那么水平线应该画,而用户触摸在垂直方向移动,那么垂直线应该画。请建议一些使用Swift的解决方案。 我在下面试过了。但这是自由线 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesBegan(touches, with: event) let touch: AnyObjec

我正在做一个项目,我想如果用户触摸在水平方向移动,那么水平线应该画,而用户触摸在垂直方向移动,那么垂直线应该画。请建议一些使用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()
    }
override func touchsbegind(touch:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)
让触摸:AnyObject?=首先触摸
让lastPoint=touch!.previousLocation(在:holderView中)
移动路径(到:CGPoint(x:lastPoint.x,y:lastPoint.y))
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
让触摸:AnyObject?=首先触摸
让currentPoint=touch!。位置(在:holderView中)
addLine(to:CGPoint(x:currentPoint.x,y:currentPoint.y))
//分层设计路径
设shapeLayer=CAShapeLayer()
shapeLayer.path=path.cgPath
shapeLayer.strokeColor=UIColor.orange.cgColor
shapeLayer.lineWidth=20.0
holderView.layer.addSublayer(shapeLayer)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
super.touchesend(触摸,带有:事件)
path=UIBezierPath()
}
试试这个

class DrawingView:UIView{
var path=UIBezierPath()
var initialLocation=CGPoint.zero
var finalLocation=CGPoint.zero
var shapeLayer=CAShapeLayer()
重写func awakeFromNib(){
super.awakeFromNib()
setupView()
}
重写初始化(帧:CGRect){
super.init(frame:frame)
setupView()
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
func setupView(){
self.layer.addSublayer(shapeLayer)
self.shapeLayer.lineWidth=20
self.shapeLayer.strokeColor=UIColor.blue.cgColor
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)
如果let location=touch.first?.location(in:self){
初始位置=位置
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
如果let location=touch.first?.location(in:self){
设dx=location.x-initialLocation.x
设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.addLine(收件人:finalLocation)
shapeLayer.path=path.cgPath
}
}
}

您可以检查此链接,如果用户触摸屏幕并创建点的曲线路径,该怎么办?未知因素太多了!您的代码说,“有一个贝塞尔路径,当触摸开始时,跳到触摸位置,当触摸移动时,将线绘制到新位置并在新的子层中显示结果,当触摸结束时,创建一个新的贝塞尔路径”。子层逻辑似乎非常低效(创建数百个部分重复的子层),bezier路径创建似乎不必要地向后,但这不是重点。鉴于上述逻辑的解释,您能澄清一下您想要实现什么吗?@SamipShah即使用户尝试绘制曲线,他也无法做到这一点。这条线总是笔直的、垂直的或水平的。@baglan上面的代码只是在触摸时画出路径。我测试了这个和它的工作。
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

        }
    }
}