首页 > 解决方案 > 无法在其父视图之外平移手势较小的 UIView

问题描述

我有一个UIView包含许多UIView子视图的。当用户尝试平移其中一个子视图时,我希望他们能够将其拖动到屏幕上的任何其他位置。

但是,我只能在UIView它们包含的较大视图中拖动这些较小的视图。当用户第一次尝试平移较小的视图时,有没有办法以编程方式在顶部创建第二个 UIView,然后在第二个 UIView 周围拖动?第二个 UIView 将与第一次接触的较小视图相比是全新的。

这是我目前拥有的处理函数,仅供参考。不过,我不知道我会在哪里以编程方式创建第二个 UIView。任何帮助将不胜感激:

 @objc func handlePanGesture(sender: UIPanGestureRecognizer) {
    // get translation
    var translation = sender.translation(in: view)
    sender.setTranslation(CGPoint.zero, in: view)

    // drag around current UIView
    var newView = sender.view as! UIView
    newView.center = CGPoint(x: newView.center.x+translation.x, y: newView.center.y+translation.y)
    newView.isMultipleTouchEnabled = true
    newView.isUserInteractionEnabled = true

    if sender.state == UIGestureRecognizer.State.began {
        // add something you want to happen when the Label Panning has started
    }

    if sender.state == UIGestureRecognizer.State.ended {
        // add something you want to happen when the Label Panning has ended
    }

    if sender.state == UIGestureRecognizer.State.changed {
        // add something you want to happen when the Label Panning has been change ( during the moving/panning )
    } else {
        // or something when it's not moving
    }
}

我想在我的应用程序中做什么

标签: iosswiftuiviewuipangesturerecognizer

解决方案


有什么方法可以在任何地方拖动较小的视图,同时保持我现在的布局

由于触摸的工作方式,默认情况下,当视图到达其父视图的边界时,它是不可能被触摸的。但是您可以通过在超级视图中覆盖来克服该限制hitTest


推荐阅读