为什么当我画一些东西时,它不会出现在我的手指移动的位置?

时间:2016-10-07 04:32:20

标签: ios swift sprite-kit

该线显示距离我想要绘制的地方几英寸。我在SpriteKit中制作了一个绘图应用程序,我尝试在我的UIImageView底部绘制一些东西,但它距离我的手指所在位置几英寸。为什么会这样?

class GameScene: SKScene {

var drawImageView: UIImageView! = UIImageView()
var lastPoint = CGPoint.zero
var swiped = false

override func didMove(to view: SKView) {
    drawImageView.frame = previewCamera.bounds
    previewCamera.addSubview(drawImageView)
}

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

    if let touch = touches.first {
        lastPoint = touch.location(in: self.view)
    }
}

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

    if let touch = touches.first {
        let currentPoint = touch.location(in: self.view)
        draw(fromPoint: lastPoint, toPoint: currentPoint)
        lastPoint = currentPoint
  }
}

func draw(fromPoint:CGPoint,toPoint:CGPoint) {
    UIGraphicsBeginImageContext((self.view?.frame.size)!)
    drawImageView.image?.draw(in: CGRect(x: 0, y: 0, width: (self.view?.frame.width)!, height: (self.view?.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(7)
    context?.setStrokeColor(UIColor(red: red, green: green, blue: blue, alpha: 1.0).cgColor)
    context?.strokePath()

    drawImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        draw(fromPoint: lastPoint, toPoint: lastPoint)
    }
}
}

0 个答案:

没有答案