iphone - 如何将对象移动到排列?

标签 iphone objective-c ios

大家

我有一个问题,我想实现一个UI,比如iOS桌面,目前我可以安排

通过下面的代码对象:

int k=0;
    int j;
    for (int i=0; i<[items count]; i++) {
       //k is row     
        j=i%3;
        if (j==0) {
            k++;
            if (k%3 == 1) {
                k = 1;
            }
        }
 [button setFrame:CGRectMake(25+(i%3*95), 35+((k-1)*140), 100, 100)]; 

所以, View 看起来像这样

A B C
D E F
G H I

现在,让它进入编辑模式,在E和F之间拖放A

所以有很多事情要做

B和C会向左移动,D会在B C之后上升

E左移。(这些动画怎么实现???)

B C D
E A F
G H I

我搜索Apple样本,我只找到Moveme这个样本,但它只有一个对象

任何建议或示例代码都会有很大帮助:)

谢谢

韦伯

最佳答案

当用户移动一个按钮时,你必须先找到它的新索引。

int j = (button.frame.origin.x - 25) / 95;
int k = (button.frame.origin.x - 35) / 140;
int newIndex = 3 * k + j;

然后您必须将它放在 items 数组中的那个索引处。

[items insertObject:button atIndex:newIndex];

然后您应该调用您提供的代码来重新定位按钮。

for (int i = 0; i < items.count; i++) {
    UIButton *button = (UIButton *)[items objectAtIndex:i];
    [UIView animateWithDuration:0.5 animations:^{
       button.frame = CGRectMake(25+(i%3)*95, 35+(i/3)*140, 100, 100); 
    }];
}

关于iphone - 如何将对象移动到排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9287341/

相关文章:

iphone - 在 iPhone 模拟器上调用 AudioUnit 渲染回调,但在手机上不调用

objective-c - 是否有一种可靠的方法来检测 subview 中的第一响应者更改?

ios - 初始化后不显示带有 Nib 的 UITableViewCell

iphone - POST 请求中的 & 符号造成严重破坏

ios - Swift 版本 NativeScript

iphone - 将NSInteger传递给方法不起作用

iPhone/仪器 : what's about the "malloc" entries in object summary?

iphone - UIButton的边界和框架

objective-c - 每个 tableviewcells 中的 uiwebview

iphone - Passbook 的权利文件