gpt4 book ai didi

ios - touchesBegan 和 touchesMoved 没有在 UIView 上触发

转载 作者:可可西里 更新时间:2023-11-01 00:57:30 33 4
gpt4 key购买 nike

我想要实现的目标:

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

问题

PaintingSubclass

touchesBegantouchesMoved 根本没有被调用。

当前设置

  • 我有一个 PaintingViewController 然后我还有一个 PaintingSubclass

  • PaintingViewController 是一个 UIViewControllerPaintingSubclass 是一个 UIView

  • PaintingViewController 创建一个 PaintingSubclass 的实例,并将其添加到 PaintingViewControllersubview

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

到目前为止我尝试了什么

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

当前代码

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

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(),但是您的 view 的默认 isUserInteractionEnabled 是 false .将其设置为真

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

关于ios - touchesBegan 和 touchesMoved 没有在 UIView 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42362315/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com