gpt4 book ai didi

ios - Swift:通过其 super View 窗口中的平移手势更改(翻译)UIView 位置

转载 作者:行者123 更新时间:2023-11-28 07:40:26 25 4
gpt4 key购买 nike

简介

上下文:

在我的主 ViewController 中,我有一个 scrollView,里面有一些对象(它们是 UIView)。当点击/选择其中一个 UIView 时,我会在 UIView 中动画转发 UITextView 以与所选对象一起使用。 (一次只能出现一个UIView)

这个出现在对象选择上的 UIView 被分成一个单独的类,称为 AdjunctiveTextView

问题/目标:

(下面提供的示例代码将清楚地说明这一点,我还评论了代码中的问题所在)

当一个对象被点击并且有一个带有文本的相邻 UIView 时,我想让那个相邻的 UIView 跟在 scrollView 之后。

  • 我正在使用 UIPanGestureRecognizer 来尝试执行此操作。但是当用户在 ScrollView 中拖动时,我不知道如何让它工作。它仅在用户拖动实际的 adjunctiveTextView 时才有效。
  • 除了 adjunctiveTextView 在 panGesture 期间没有改变其位置外,一切都按预期工作。
  • 我希望(如果可能的话)将 AdjunctiveTextView 作为一个单独的类。我的 ViewController 文件越来越大。

问题:

为什么 UIPanGestureRecognizer 没有按预期工作?需要什么才能正确翻译 backView?

代码

我的尝试:(如下图)

我的尝试只是让 backView 本身在 panGesture 中“可拖动”。当我滚动 scrollView 时,它没有任何反应。

(我只包含了我的代码的相关部分)

class ViewController: UIViewController {
let adjunctiveTextView = AdjunctiveTextView()

// this is a delegate method which gets called when an object is tapped in the scrollView
func scrollViewObjectIsTapped(_ objectScrollView: ObjectScrollView, object: AvailableObject) {

** adjunctiveTextView.scrollView = scrollView // **Edited! (scrollView is the name of the scrollView in this class too)
adjunctiveTextView.showView(passInObject: AvailableObject)
}

}

class AdjunctiveTextView: NSObject {
lazy var backView: UIView = {
//backView setup
}
lazy var textView: UITextView = {
//textView setup
}

//additional init and setup
** weak var scrollView : UIScrollView! // **Edited!
func showView(passInObject: AvailableObject) {
if let window = UIApplication.shared.keyWindow {

// the issue must either be here in the PanGesture setup
let panG = UIPanGestureRecognizer(target: self, action: #selector(translateView(sender:)))
panG.cancelsTouchesInView = false
// window.addGestureRecognizer(panG)
** scrollView.addGestureRecognizer(panG) // **Edited!
window.addSubview(backView)

textView.text = passInObject.information
backView.frame = CGRect(x: passInObject.frame.minX, y: passInObject.minY, width: window.frame.width - passInObject.maxX - 6, height: textView.bounds.height + 5)
backView.alpha = 0

//it animates a change of the backViews x position and alpha.
UIView.animate(withDuration: 0.42, delay: 0, options: .curveEaseInOut, animations: {
self.backView.alpha = 1
self.backView.frame = CGRect(x: passInObject.frame.minX + passInObject.frame.width, y: passInObject.minY, width: window.frame.width - passInObject.maxX - 6, height: textView.bounds.height + 5)

}, completion: nil)
}
}

// or the issue is here in the handle function for the PanGesture.
@objc private func translateView(sender: UIPanGestureRecognizer) {

if let window = UIApplication.shared.keyWindow {
let translation = sender.translation(in: window) //Have tried setting this to scrollView also
switch sender.state {
case .began, .changed:
backView.center = CGPoint(x: backView.center.x, y: backView.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: window) //Have tried setting this to sccrollView also
break
case .ended:
break
default:
break
}

}
}


}

感谢阅读我的问题。

最佳答案

我只是给你的 scrollView 添加了一个弱引用,然后将平移手势添加到 scrollView。它随心所欲地工作。如果您想要原始行为,您可以考虑向后 View 添加另一个平移手势。

class AdjunctiveTextView: NSObject {
lazy var backView: UIView = {
//backView setup
return UIView.init()
}()
lazy var textView: UITextView = {
//textView setup
return UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 100))
}()

weak var scrollView: UIScrollView!

//additional init and setup

func showView(passInObject: AvailableObject) {
if let window = UIApplication.shared.keyWindow {

// the issue must either be here in the PanGesture setup
let panG = UIPanGestureRecognizer(target: self, action: #selector(translateView(sender:)))
panG.cancelsTouchesInView = false
// passInObject.addGestureRecognizer(panG)

scrollView.addGestureRecognizer(panG)

window.addSubview(backView)

textView.text = passInObject.information
textView.backgroundColor = UIColor.blue
backView.addSubview(textView)

backView.frame = CGRect(x: passInObject.frame.minX, y: passInObject.frame.minY, width: window.frame.width - passInObject.frame.maxX - 6, height: textView.bounds.height + 5)
backView.alpha = 0

//it animates a change of the backViews x position and alpha.
UIView.animate(withDuration: 0.42, delay: 0, options: .curveEaseInOut, animations: {
self.backView.alpha = 1
self.backView.frame = CGRect(x: passInObject.frame.minX + passInObject.frame.width , y: passInObject.frame.minY , width: window.frame.width - passInObject.frame.maxX - 6, height: self.textView.bounds.height + 5)

self.backView.backgroundColor = UIColor.red

}, completion: nil)
}
}

// or the issue is here in the handle function for the PanGesture.
@objc private func translateView(sender: UIPanGestureRecognizer) {

if let window = UIApplication.shared.keyWindow {
let translation = sender.translation(in: window)
switch sender.state {
case .began, .changed:
backView.center = CGPoint(x: backView.center.x, y: backView.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: window)
break
case .ended:
break
default:
break
}

}
}
}


class ObjectScrollView: UIScrollView{
}

class AvailableObject: UIView{
var information: String!
}

class MySCNViewController: UIViewController {

@IBOutlet weak var oScrollView: ObjectScrollView!

// this is a delegate method which gets called when an object is tapped in the scrollView
func scrollViewObjectIsTapped(_ objectScrollView: ObjectScrollView, object: AvailableObject) {
adjunctiveTextView.showView(passInObject: object)
}

let adjunctiveTextView = AdjunctiveTextView()
let ao = AvailableObject.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 200))

override func viewDidLoad() {
super.viewDidLoad()
ao.information = "test"
adjunctiveTextView.scrollView = oScrollView
ao.backgroundColor = UIColor.yellow
}

@IBAction func tap(_ sender: Any?){
scrollViewObjectIsTapped(oScrollView, object: ao)}
}

关于ios - Swift:通过其 super View 窗口中的平移手势更改(翻译)UIView 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52414307/

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