swifttouchguardtouchesbeganuigraphicscontext

line drawing away from touch point


My swift code is attempting to draw a line. As you can see in the gif below. When the the user places a touch point you can the line being drawn a little away from the cursor. I don't know what's going on here. But I would assume whatever that is wrong is in the touches began func because this issue starts as soon as the user touches the image view.

enter image description here

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()
        
 
        

    }
}

///

Solution

  • the problem with your image view fame :

    please set like this and check :

    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()
                
         
                
    
            }
        
    
    }
    

    please check the code i updated your controller