用UIPanGesture Swift绘制

时间:2018-03-15 09:14:15

标签: ios swift4 uipangesturerecognizer

我尝试使用我的应用程序进行绘制,所有工作都是触摸开始/移动/结束,但我想使用UIPanGesture,所以我尝试了一些东西,但我不知道,如何让我工作,因为它没有我有一些问题。

@objc func Drawing(recognizer: UIPanGestureRecognizer) {
    var lastpointo = CGPoint()
    if recognizer.state == UIGestureRecognizerState.began{
        lastpointo = recognizer.location(in: Drawinglayer)
    }else if recognizer.state == UIGestureRecognizerState.ended{
        drawLines(fromPoint: lastpointo, toPoint: lastpointo)
    }else{
        drawLines(fromPoint: lastpointo, toPoint: recognizer.translation(in: Drawinglayer))
        lastpointo = recognizer.location(in: Drawinglayer)
    }
}

这是Gesture函数的代码,drawLines的代码是:

func drawLines(fromPoint:CGPoint, toPoint: CGPoint){
    if DrawingActive == true{
        UIGraphicsBeginImageContext(self.view.frame.size)
        Drawinglayer.image?.draw(in: CGRect(x: 0, y: 0, width: Drawinglayer.frame.width, height: Drawinglayer.frame.height))

        let context = UIGraphicsGetCurrentContext()

        context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
        context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))

        context?.setBlendMode(CGBlendMode.normal)
        context?.setLineCap(CGLineCap.round)
        context?.setLineWidth(fontSizeOfDraw)
        context?.setStrokeColor(DrawingColor.cgColor)

        context?.strokePath()

        Drawinglayer.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        PickSize.isEnabled = false
        PickSize.alpha = 0
        ColorPickerController.isEnabled = false
        ColorPickerController.alpha = 0
        DrawBtn.isEnabled = false
        DrawBtn.alpha = 0
        SelectButton.isEnabled = false
        SelectButton.alpha = 0
    }
}

我希望有人可以帮助我并给我很好的建议。

先谢谢。

0 个答案:

没有答案