【问题标题】:Replace tags of selected buttons to the remaining buttons将选定按钮的标签替换为其余按钮
【发布时间】:2014-02-03 06:53:33
【问题描述】:

此处有n UIButtons,选择特定UIButtons它是从SuperView的删除。其余的按钮框架移动到这一点。同样试图用选定的按钮替换其他按钮的标签。这是我到目前为止所尝试的。

-(void)totesttheFunction
{
    for(int i=0; i<7; i++)
    {
        UIButton *testHere = (UIButton*)[self.view viewWithTag:i];
        if([testHere isSelected])
        {
            int backuptagFor = testHere.tag;
            CGFloat diff = 30.0;
            for(int j=i+1; j<7;j++)
            {
                UIButton *btnToReplace = (UIButton*)[self.view viewWithTag:j];
                 CGRect setRect = CGRectMake(btnToReplace.frame.origin.x-diff, btnToReplace.frame.origin.y, btnToReplace.frame.size.width, btnToReplace.frame.size.height);
                btnToReplace.tag = backuptagFor;
                [testHere removeFromSuperview];

            }
        }
    }
}

这里的整数变量difference是两个相邻UIButton之间的帧差。

【问题讨论】:

    标签: ios tags uibutton frame


    【解决方案1】:

    我不知道您使用此代码的目的。

    但 UICollectionView 将是解决您的问题的更好方法。 在 collectionview 单元格中添加多个按钮。 那么collection view会自己管理每个cell的删除和索引。

    【讨论】:

    • 我将其用于纸牌游戏。所以选择了 1 张牌,它被删除,剩余的牌被移动到这个框架[两个相邻 uibuttons 之间的差异]..跨度>
    • 对于您的问题,可以使用 UiCollectionView。在collectionView中,您可以为单元格提供不同的帧,您可以轻松地删除collectionview单元格中带有动画的单元格,并使用单元格索引进行进一步计算
    • 现在的问题是我已经完成了项目的剩余部分。它现在卡在了这一点上。此时更改为 collectionview 有点困难。
    【解决方案2】:

    据我了解,解决方案可能如下所示。

    添加按钮来查看

    for (int i=0; i<7; i++) {
            UIButton *btn = [[UIButton alloc]init];
            btn.tag=i;
            CGRect frame=CGRectMake(0, i*40, 90, 37);
            [btn addTarget:self action:@selector(btnDidPressed:) forControlEvents:UIControlEventTouchUpInside];
            [btn setFrame:frame];
            [btn setTitle:[NSString stringWithFormat:@"Button %d",i] forState:UIControlStateNormal];
    
            [self.view addSubview:btn];
    
        }
    

    移除按下的按钮

    -(IBAction)btnDidPressed:(id)sender
    {
        UIButton *btn =(UIButton *)sender;
    
        [btn removeFromSuperview];
    
    }
    

    您还可以使用视图子视图重新排列按钮或将按钮存储在数组中。按下按钮时删除选定的按钮并重绘剩余按钮。

    【讨论】:

    • 我能够将剩余按钮的框架移动到已删除的 btn 位置,但我将如何用剩余的 btn 替换那些已删除按钮的标签
    【解决方案3】:

    初始化一个存储按钮的数组。 viewdidload 方法中的 [array addObject:btn]。

    然后您可以删除选定的并替换其他按钮,如下所示。 btnDidPressed 方法:

    UIButton *btn =(UIButton *)sender; //Pressed button 
    
    CGRect rect = btn.frame; //pressed button frame
    CGRect temp ; 
    
    int tag =btn.tag; //pressed button tag
    [btn removeFromSuperview]; //remove from view
    [arr removeObjectAtIndex:tag]; //remove from array
    
    for (int i =0; i<arr.count; i++) {
        UIButton *btnNew = [arr objectAtIndex:i];
        NSLog(@"tags old %d new %d",tag,btnNew.tag);
        if (btnNew.tag>tag) { //be sure not to move previous buttons 
            temp = btnNew.frame;
            [btnNew setFrame:rect];
            btnNew.tag=i; //change tag
            rect = temp;
            [self.view addSubview:btnNew];
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多