画远离接触点的线

line drawing away from touch point

我的 swift 代码试图画一条线。正如您在下面的 gif 中看到的那样。当用户放置一个触摸点时,您可以将绘制的线稍微远离光标。我不知道这里发生了什么。但我会假设触摸开始时出现任何错误,因为一旦用户触摸图像视图,这个问题就会开始。

import UIKit
class ViewController: UIViewController {
    var startPoint: CGPoint?
    var statPoint = CGPoint.zero
    var swipe = false
    var pic = UIImageView()
  
    
    

 
    override func viewDidLoad() {
        super.viewDidLoad()
      
        pic.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(pic)
  


        pic.backgroundColor = .brown
        view.backgroundColor = .cyan
   
   
        pic.frame = CGRect(x: 100, y: 100, width: 250, height: 250)
       
     
    }
    

  
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return}
 
        swipe = false
        statPoint = touch.location(in: view)

        
     
       
    }
    
    func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint)  {
        UIGraphicsBeginImageContext( view.frame.size)
        
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        
        pic.image?.draw(in: view.bounds)
        context.move(to: fromPoint)
        context.addLine(to: toPoint)
        context.setLineCap(.round)
        context.setLineWidth(5)
        context.setBlendMode(.normal)
        context.setStrokeColor(UIColor.black.cgColor)
        context.strokePath()
        pic.image = UIGraphicsGetImageFromCurrentImageContext()
        pic.alpha = 1
        
        UIGraphicsEndImageContext()
        
        
        
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard var touch = touches.first else {
            return
            
        }
 
            swipe = true
            let currentPoint = touch.location(in: view)
            drawLine(from: statPoint, to: currentPoint)
            statPoint = currentPoint
      

        
        
  
    }
    
    
    
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !swipe {
            drawLine(from: statPoint, to: statPoint)
        }
        
        UIGraphicsBeginImageContext(view.frame.size)
        pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
        pic.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
 
        

    }
}

///

你的图片浏览名气问题:

请这样设置并检查:

class ViewController: UIViewController {

       var startPoint: CGPoint?
        var statPoint = CGPoint.zero
        var swipe = false
        var pic = UIImageView()
      
        
        

     
        override func viewDidLoad() {
            super.viewDidLoad()
          
            pic.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(pic)
      


            pic.backgroundColor = .brown
            view.backgroundColor = .cyan
       
       
            pic.frame = CGRect(x: 100, y: 100, width: 250, height: 250)
           
         
        }
        

      
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            guard let touch = touches.first else {
                return}
     
            swipe = false
            statPoint = touch.location(in: self.pic)

            
         
           
        }
        
        func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint)  {
            UIGraphicsBeginImageContext( pic.frame.size)
            
            guard let context = UIGraphicsGetCurrentContext() else {
                return
            }
            
            pic.image?.draw(in: pic.bounds)
            context.move(to: fromPoint)
            context.addLine(to: toPoint)
            context.setLineCap(.round)
            context.setLineWidth(5)
            context.setBlendMode(.normal)
            context.setStrokeColor(UIColor.black.cgColor)
            context.strokePath()
            pic.image = UIGraphicsGetImageFromCurrentImageContext()
            pic.alpha = 1
            
            UIGraphicsEndImageContext()
            
            
            
        }
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            guard var touch = touches.first else {
                return
                
            }
     
                swipe = true
                let currentPoint = touch.location(in: pic)
                drawLine(from: statPoint, to: currentPoint)
                statPoint = currentPoint
          

            
            
      
        }
        
        
        
        
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            if !swipe {
                drawLine(from: statPoint, to: statPoint)
            }
            
            UIGraphicsBeginImageContext(pic.frame.size)
            pic.image?.draw(in: pic.bounds, blendMode: .normal, alpha: 1)
            pic.image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
     
            

        }
    

}

请检查我更新你的控制器的代码