以编程方式将UITableView行从一个位置移动到另一个位置 [英] Programmatically move/animate a UITableView row from one position to another

查看:73
本文介绍了以编程方式将UITableView行从一个位置移动到另一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,我想以编程方式将一行从位置N1移动到位置N2,我希望它从旧位置动画到新位置。我查看了UITableView文档,我只看到了插入,重新加载和删除。您是否知道我可以通过编程方式执行此操作?

I have a UITableView and I want to programmatically move one row from position N1 to position N2 and I would like it to animate from the old location to the new. I've looked through the UITableView documentation and I'm only seeing inserts, reloads, and deletes. Do you know of a way I can do this programmatically?

一些注释:


  • 我知道我可以从位置N1设置删除动画并同时将插入动画设置为位置N2。这是我的后备,但我希望用户明白它真的从N1移动到N2。

  • I know that I can animate the deletion from location N1 and animate the insertion to location N2 at the same time. That's my fallback, but I'd like the user to understand that it is truly moving from N1 to N2.

我不是在谈论允许用户将它从一个地方拖到另一个地方。我知道如何做到这一点,我正在寻找一种方法来以编程方式启动和动画移动。

I'm not talking about allowing the user to drag it from one place to another. I understand how to do that, I'm looking for a way to initiate and animate the move programmatically.

推荐答案

我过去曾经处理过类似的事情,解决方案并不漂亮,但确实有效。

I've dealt with something kind of like this in the past and the solution is not pretty but it does work.

基本上你必须做一些数学来计算屏幕上行的位置。然后根据 UITableView contentOffset y 属性进行调整$ C>。

Basically you have to do some math to calculate where on the screen the row is. Then adjust that based on the y property of the contentOffset of your UITableView.

你可以这样做:

[myView convertPoint:localPosition toView:nil];

然后你会构建一个新的 UIView 这是与您的观点相同。将其框架设置为直接位于您尝试移动的 UITableViewCell 之上。将其添加为应用主要 UIWindow 的子视图。

Then you would construct a new UIView that's identical to your view. Set its frame to be directly on top of the UITableViewCell you're trying to move. Add it as a subview of your app's main UIWindow.

您需要设置 UITableView上的> userInteractionEnabled 或 scrollEnabled 虽然所有这一切都在进行,所以用户不能在整个过程中弄清楚事物的位置(只需记住将其设置回 YES 当一切都完成了动画)。

You'll want to set userInteractionEnabled or scrollEnabled to NO on your UITableView while all of this is going on so that the user can't muck with the position of things throughout the process (just remember to set it back to YES when everything is done animating).

然后将它设置为新位置然而最有意义。

Then animate it to the new position however makes the most sense.

我知道你可能需要在所有这些过程中滚动 UITableView ,这会使事情变得相当复杂,但你也可以为滚动设置动画。

I understand you may have to scroll the UITableView in the process of all of this, which complicates things considerably, but you can animate that scrolling as well.

如果您的应用中有必要,您当然必须在 动画的行上执行类似的操作。

You'll of course have to do similar operations on the row you're animating to if it is necessary in your app.

就像我说它不容易或漂亮。但是我了解你想要完成什么,我认为当它正确拉出时效果非常棒。

Like I said its not easy or pretty. But I do understand what you're trying to accomplish and I think the effect is super awesome when it's pulled off properly.

如果有人有我更想知道如何在不疯狂的情况下实现这一目标,我很想知道,但这种实施方式是我能做到的最好的。

If anyone has a better idea of how to pull this off without going insane I'd love to know, but this type of implementation is the best I've been able to do.

这篇关于以编程方式将UITableView行从一个位置移动到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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