iOS>>拖动视图正在跳回原始位置>>自动布局结合 UIPanGestureRecognizer 问题 [英] iOS >> Dragged View is Jumping Back to Original Position >> Auto Layout Combined with UIPanGestureRecognizer Issue

查看:16
本文介绍了iOS>>拖动视图正在跳回原始位置>>自动布局结合 UIPanGestureRecognizer 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 UIViews 用户应该能够拖动 &降低.我使用 UIPanGestureRecognizer 来管理 d&d 操作(参见下面的代码).d&d 动作运行良好,但是当我开始拖动另一个 UIView 时,我刚刚放下的那个正在跳回原来的位置.

I have several UIViews that the user should be able to drag & drop. I use UIPanGestureRecognizer to manage the d&d action (see code below). The d&d action is working fine, but when I start dragging another UIView, the one that I just dropped is jumping back to its original position.

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            for (UIView* checkView in dragAndDropImageViewsArray) {
                if (checkView == recognizer.view) {
                    [checkView.superview bringSubviewToFront:checkView]; //this is done to make sure the currently dragged object is the top one
                }
            }
            break;

        case UIGestureRecognizerStateChanged:
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
            break;

        case UIGestureRecognizerStateEnded:
            NSLog(@"UIGestureRecognizerStateEnded");
            break;

        default:
            break;
    }

    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

我尝试了几种解决"问题但导致其他问题的方法:

I tried several things that "sloved" the problem but caused other problems:

  • 取消自动布局:这完全解决了它,但现在我必须开始计算和管理不同设备和方向的对象位置和大小.
  • 使用 layer.zPosition 而不是bringSubviewToFront:使对象出现在其他对象之上,同时显然避免了跳回",但不允许我更改不同可拖动对象之间的层次结构.
  • Cancel Auto-Layout: That totally solves it but now I have to start calculate and manage object locations and sizes for different devices and orientations.
  • Use layer.zPosition instead of bringSubviewToFront: that makes objects appear above other objects while apparently avoiding the "jump back", but does not allow me to change the hierarchy between the different draggable objects.

我开始加深对自动布局的理解,但是当我觉得我要寻找的东西应该非常简单时,它变得非常复杂;我看到了许多处理执行重新自动布局"的方法,但我不清楚如何使用它们.

I started deepening my understanding about Auto Layout, but it gets very complicated when it seems to me that what I'm looking for should be very simple; I saw many methods that deal with performing a "re-auto-layouting", but I'm not clear about how to work with them.

这里有简单的解决方案吗?喜欢调用覆盖 IB Auto-Layout 约束并根据 UIView 新位置重新定义它们的方法?

非常感谢任何帮助.

推荐答案

我认为如果你使用自动布局并且你想改变视图的位置,你需要更新视图的约束而不是手动设置视图的中心.从描述中听起来您正在设置中心,但是当应用程序接下来布置视图(例如,您拖动另一个视图)时,它会将中心重新设置为自动布局所说的任何内容.

I think that if you are using autolayout and you want to change the position of a view, you need to be updating the constraints on the view instead of manually setting the center of a view. From the description it sounds like you are setting the center, but when the app next lays out the views (eg you drag another) it re-sets the center to whatever autolayout says it should be.

我不确定您应用的其余部分是什么样的,但您可能能够找到附近的视图并为其添加约束,或者您可能更喜欢只更新拖动视图的约束并设置前导空间和手动到超级视图的顶部空间.如果您有一个方便的集合,您还可以清除所有约束,然后重新添加它们,因为您希望在视图之间保持填充.

I'm not sure what the rest of your app looks like, but you may be able to find nearby views and add constraints to it, or you might prefer to just update the constraints of the dragged view and set the leading space and top space to the superview manually. If you've got a collection handy you could also clear all the constraints and then re-add them as you'd like to keep padding in between the views.

要在代码中设置约束,您可以清除移动视图的约束并重新添加它们,或者如果您在 IB 中有视图,您可以将约束拖到代码中以创建插座.在代码中查看 removeConstraints 和 addConstraints 方法.

To set the constraints in code you can clear the constraints for the moved view and re-add them, or if you have a view in IB you can drag the constraints into code to create an outlet. Look at the removeConstraints and addConstraints methods to do it in code.

最后,如果它在没有自动布局的情况下按您希望的方式工作,您可以尝试在移动的视图上设置 translatesAutoresizingMaskIntoConstraints = YES,但请注意这可能会导致其他地方发生冲突.

Lastly, if it works how you'd like without autolayout, you could try setting translatesAutoresizingMaskIntoConstraints = YES on the moved views, but be aware that may cause conflicts elsewhere.

这篇关于iOS>>拖动视图正在跳回原始位置>>自动布局结合 UIPanGestureRecognizer 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆