【问题标题】:line disagreeing when touch end when drawing line画线时触摸结束时线不一致
【发布时间】:2020-12-02 05:49:49
【问题描述】:

我的快速代码目标是在图像视图上画线。现在,当您从图像视图中拿起手时,绘图就会消失。当你的手被按下时,它就在你的手或点被释放时工作,没有任何东西被保存。我不知道我的代码中缺少什么来解决这个问题,它可能只是一行。

import UIKit
class ViewController: UIViewController {
    
    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: 100, height: 100)
    }
    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 let 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(pic.frame.size)
        pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
        pic.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
}

【问题讨论】:

    标签: swift cgpoint cgpath uigraphicscontext touchesended


    【解决方案1】:

    我已经检查了您的代码。我认为问题在于您在 touchEnded 中使用的 frame

    用下面的代码替换你的touchedEnded

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2014-11-24
      相关资源
      最近更新 更多