【问题标题】:touchesBegan and touchesMoved not getting triggered on UIViewtouchesBegan 和 touchesMoved 没有在 UIView 上被触发
【发布时间】:2017-02-21 08:36:22
【问题描述】:

我想要达到的目标:

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

问题

touchesBegantouchesMoved for PaintingSubclass 根本没有被调用。

当前设置

  • 我有一个PaintingViewController,然后我也有一个PaintingSubclass

  • PaintingViewControllerUIViewControllerPaintingSubclassUIView

  • PaintingViewController 创建PaintingSubclass 的实例并将其添加到PaintingViewController子视图

  • PaintingSubclass 是实际绘图发生的地方。

到目前为止我已经尝试过什么

  • touchesMoved 中放置一个断点并touchesBegan (没有用)
  • 尝试添加UITapGestureRecognizer (无效)
  • 启用self.isUserInteractionEnabled = true (无效)
  • 使PaintingSubclass继承UIControl并在touchesMovedtouchesBegan的末尾调用sendActions(for: .valueChanged) (不起作用)

当前代码

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

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

我在这里做错了什么?被困在这种状态好几个小时没有取得任何进展。任何帮助/反馈将不胜感激。

【问题讨论】:

  • 你试过检查它的权限吗?根据我的经验,这通常会触发一些事情
  • view = UILabel() 这行说明了什么?
  • 你能添加你的 UI 图像吗?
  • 能否将autoresizingMask的flexibleWidth和flexibleHeight设置为子类view..然后添加super.init(coder:aDecoder)
  • 问题是一个非常简单的问题...我只需要在 VIEW 而不是 self 上启用 isUserInteractionEnabled 为 true。哇!

标签: ios swift uiview uiviewcontroller touchesbegan


【解决方案1】:

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

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

【讨论】:

  • isUerInteractionEnabled 是我尝试的第一件事之一,但事实证明我必须在视图而不是 self 上启用它(我认为出于某种原因它会相同)。已经是深夜了.. ;)
  • 太好了:)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    问题出在:

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

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

    【讨论】:

      【解决方案4】:

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

      除此之外,大概您不希望绘画视图绘制在标签上,而是将标签绘制在顶部,因此标签应该是子视图。

      【讨论】:

      • 我将 PaintingViewController 作为主类的唯一原因是因为这个视图是从另一个屏幕的 pushViewController 调用的,而我还没有找到使用 pushViewController 推送到新 UIView 的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多