Ios 为什么我画东西时它不';我的手指在哪里动?

Ios 为什么我画东西时它不';我的手指在哪里动?,ios,swift,sprite-kit,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:

这条线看起来离我想要画的地方有几英寸远。我在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)
    }
}
}
class游戏场景:SKScene{
var drawImageView:UIImageView!=UIImageView()
var lastPoint=CGPoint.zero
var swiped=false
覆盖func didMove(到视图:SKView){
drawImageView.frame=previewCamera.bounds
previewCamera.addSubview(drawImageView)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
刷卡=错误
如果让触摸=先触摸{
lastPoint=touch.location(在:self.view中)
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
刷卡=正确
如果让触摸=先触摸{
让currentPoint=touch.location(在:self.view中)
绘制(起点:最后一点,拓扑点:当前点)
lastPoint=currentPoint
}
}
func绘图(起点:CGPoint,地形点:CGPoint){
UIGraphicsBeginImageContext((self.view?.frame.size)!)
drawImageView.image?draw(in:CGRect(x:0,y:0,宽度:(self.view?.frame.width)!,高度:(self.view?.frame.height)!)
let context=UIGraphicsGetCurrentContext()
上下文?.move(到:CGPoint(x:fromPoint.x,y:fromPoint.y))
context?.addLine(to:CGPoint(x:toPoint.x,y:toPoint.y))
上下文?.setBlendMode(CGBlendMode.normal)
上下文?.setLineCap(CGLineCap.round)
上下文?.setLineWidth(7)
上下文?.setStrokeColor(UIColor(红色:红色,绿色:绿色,蓝色:蓝色,alpha:1.0).cgColor)
上下文?.strokePath()
drawImageView.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果!刷卡{
绘制(起点:最后一点,拓扑点:最后一点)
}
}
}

在屏幕上显示之前,您正在绘制一些东西并将其转换为UIImage,请参见此演示。转换需要太多时间。取而代之的是在一个图层内绘制,并将其连接到屏幕上的视图。