在Swift / iOS中将UIViews拖放到Droparea

时间:2019-07-10 14:40:11

标签: ios swift xcode uiview drag-and-drop

我想编写一个容器管理应用程序。您可以使用它在2D空间中的指定区域上放置新的或现有的容器。 新容器从底部拖入。现有容器只能在长按它们之后才能移动。 我能够使容器移动并识别长按,但是我不知道如何使其紧贴液滴区域,以及是否需要将容器旋转到相同方向。

我已经尝试过: UIDrop / DragInteractions,它可以工作,但是在动画和其他手势的情况下会限制我。 通过touchesBegan等进行命中检测。 PanGestureRecognizer进行移动和检测。

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var exContainer: UIView!
    @IBOutlet var spots: [UIView]!

    var panGesture = UIPanGestureRecognizer()
    var longpressGesture = UILongPressGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()

        panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(conMoved))
        longpressGesture = UILongPressGestureRecognizer.init(target: self, action: #selector(conSelected))

        exContainer.addGestureRecognizer(panGesture)
        exContainer.addGestureRecognizer(longpressGesture)
    }

    @objc func conMoved() {
        exContainer.center = panGesture.location(in: self.view)
    }

    @objc func conSelected() {
        print("long")
    }
}

https://www.youtube.com/watch?v=ulB49kAIK1o 该视频显示了我的预期行为。我的问题最后是: 实现这个项目的最有效方法是什么。

0 个答案:

没有答案