touchesBegan 和 touchesMoved 没有在 UIView 上触发

touchesBegan and touchesMoved not getting triggered on UIView

我想要达到的目标:

正在尝试抓取屏幕上当前触摸区域的坐标,并将接收到的坐标绘制到屏幕上。简而言之,一个基本的绘图应用程序,全部以编程方式编写(用于我自己的练习)。

问题

touchesBegantouchesMoved for PaintingSubclass 根本没有被调用。

当前设置

到目前为止我尝试了什么

当前代码

(请忽略任何不必要的变量)

import UIKit

class PaintingViewController: UIViewController{

var _paintView: PaintingSubclass? = nil

override func loadView() {
    view = UILabel()
}

private var labelView: UILabel {
    return view as! UILabel
}

override func viewDidLoad() {

    _paintView = PaintingSubclass()
    _paintView?.frame = CGRect(x: view.bounds.minX, y: view.bounds.minY, width: 400, height: 750)
    _paintView?.backgroundColor = UIColor.white
    _paintView?.setNeedsDisplay()

    view.addSubview(_paintView!)
}

class PaintingSubclass: UIView{

    private var context: CGContext? = nil
    var styleSelection: String = "buttcap"
    var linewidth: Float = 5
    var lineCapStyle: CGLineCap? = nil
    var lineJoinStyle: CGLineJoin? = nil
    var lineWidthValue: CGFloat? = nil
    var colorValue: UIColor? = nil

    override init(frame: CGRect) {
        super.init(frame: frame)
        lineWidthValue = 0.5
        colorValue = UIColor(cgColor: UIColor.black.cgColor)
        self.isUserInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {

        context = UIGraphicsGetCurrentContext()!
        context?.move(to: CGPoint(x: 0.0, y: 110.0))
        context?.setStrokeColor((colorValue?.cgColor)!)
        context?.drawPath(using: CGPathDrawingMode.stroke)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        let touch: UITouch = touches.first!
        let touchPoint: CGPoint = touch.location(in: self)

        let _xValue = touchPoint.x
        let _yValue = touchPoint.y

        NSLog("coordinate: \(_xValue), \(_yValue)")
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)

        let touch: UITouch = touches.first!
        let touchPoint: CGPoint = touch.location(in: self)

        let _xValue = touchPoint.x
        let _yValue = touchPoint.y

        NSLog("coordinate: \(_xValue), \(_yValue)")
    }   
}
}

我在这里做错了什么?一直停留在这种状态几个小时没有取得任何进展。任何 help/feedbacks 将不胜感激。

根据您的代码,您的 PaintingViewController 的视图是一个 UILabel,属性 isUserInteractionEnabled 默认为 false.Try 将此 属性 设置为 true 可能会对您有所帮助。

最好将您的绘画视图作为视图控制器中的主视图,并将 UILabel 作为绘画视图的子视图。 UILabel 需要在您当前的布局中启用 userInteractionEnabled,而不是改变它来切换布局。

别的不说,估计你不希望绘画视图绘制在标签之上,而是希望标签绘制在上面,所以标签应该是子视图。

我不知道您为什么将默认视图设置为 UILabel(),但是 view 的默认 isUserInteractionEnabled 是错误的。将其设置为 true

override func loadView() {
    view = UILabel()
    view.isUserInteractionEnabled = true
}

问题出在:

//    override func loadView() {
//        view = UILabel()
//    }
//    
//    private var labelView: UILabel {
//        return view as! UILabel
//    }

您将视图控制器的自身视图对象转换为 UILabel 对象。