gpt4 book ai didi

ios - 如果涉及 super View ,则不排除带有 subview 的 UIScrollView

转载 作者:行者123 更新时间:2023-11-28 14:06:48 24 4
gpt4 key购买 nike

所以我正在尝试创建一个界面,其中基本上有 3 个可能的 View 级别。

  1. UIView 负责决定新单元格的元数据
  2. 细胞本身
  3. 后台UIScrollView

一切都是 UIScrollView 的子元素,但是 1 显示在 2 之上。

目标是所有单元格都是可拖动的, ScrollView 是可滚动的。然而,当点击 ScrollView 时,应该会触发新节点的创建,因此 View 1 的外观将在按下提交按钮时消失。

除了我有一个大问题之外,我已经设置了几乎所有的东西。如果我点击一个单元格,我仍然会得到创建 View ,这意味着 TapGestureRecognizer 不排除 subview 。所以这里大致设置了什么:

class Node: UILabel {

var recognizer:UIPanGestureRecognizer?
init() {
self.isExclusiveTouch = true
self.isUserInteractionEnabled = true

recognizer = UIPanGestureRecognizer(target: self, action: #selector(drag))
self.addGestureRecognizer(recognizer!)
}

@objc
func drag(rec: UIPanGestureRecognizer) {
goToPoint(point: rec.location(in: self.superview!))
}

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
var shouldReceive = false
if let clickedView = touch.view {
if clickedView == self {
shouldReceive = true;
}
}
return shouldReceive
}
}

class NodeCreator: UIView {

var submit: UIButton = UIButton(type: UIButton.ButtonType.roundedRect)

init() {
super.init(frame: CGRect.zero)

submit.setTitle("Submit", for: .normal)
submit.translatesAutoresizingMaskIntoConstraints = false
submit.addTarget(self, action: #selector(press), for: .touchUpInside)
self.addSubview(submit)
}

@objc func press() {
if let d = delegate {
if let t = inputField.text {
d.created(title: t, pt: storeP)
self.removeFromSuperview()
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class TaskView: UIScrollView, CreationDelecate{

func created(title: String, pt: CGPoint) {
let temp = Node()
view.addNode(temp, at: pt)
}

var tapRecognizer:UITapGestureRecognizer?

var nodeSet:[Node] = []
func addNode(_ n1: Node, at: CGPoint = CGPoint.zero) {
nodeSet.append(n1)
n1.goToPoint(point: at)
self.addSubview(n1)
self.bringSubviewToFront(n1)

self.isScrollEnabled = true
self.isUserInteractionEnabled = true
self.isExclusiveTouch = false

self.canCancelContentTouches = false

if (tapRecognizer == nil) {
tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tap))
self.addGestureRecognizer(tapRecognizer!)
}
}

@objc func tap(rec: UITapGestureRecognizer) {
let pos = rec.location(in: self)
let creator = NodeCreator(pt: pos)
creator.delegate = self
self.addSubview(creator)
}

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
var shouldReceive = false
if let clickedView = touch.view {
if clickedView == self {
shouldReceive = true;
}
}
return shouldReceive
}
}

为什么Nodes都是独占触摸时,TaskView仍然接收到点击手势。

最佳答案

代码不起作用,因为您没有为手势识别器分配委托(delegate)。委托(delegate)函数在 Node 和 TaskView 类中实现。但是您没有将类声明为委托(delegate),即

class TaskView: UIScrollView, CreationDelecate, UIGestureRecognizerDelegate

将委托(delegate)分配给手势识别器:

recognizer.delegate = self as! UIGestureRecognizerDelegate

应该可以

关于ios - 如果涉及 super View ,则不排除带有 subview 的 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52922078/

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