是否可以在不使用标签值的情况下将uibutton移动到不同的位置?

时间:2013-04-18 08:52:56

标签: iphone objective-c

这里我使用标签值来区分uibutton。我想在不使用标签值的情况下将uibuttons移动到桌子周围的不同位置(如zynga扑克)? X和Y的位置必须不同。 例如  如果我写x + = 100; Y + = 12; 每次x和y位置都会增加相同的值,但在圆桌x和y位置不会增加相同的值。

使用下面的代码,我可以使用标记值将uibutton(或uiimageviews)移动到该位置,但代码会变得更多(意味着它将占用更多内存)。

我只想分发35张图片卡。

有人能提供一些有关此事的信息吗?

代码:

for(int i=1 ;i<35;i++)
{
    NSLog(@"inside for loop");
    UIButton *btn_show=[UIButton buttonWithType:UIButtonTypeCustom];
    btn_show.frame=CGRectMake(190+(i/2), 20, 71, 96);
    btn_show.tag=i;
    [btn_show setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.view addSubview:btn_show];
    [self fun_Animations:btn_show];

}

-(void)fun_ViewAnimations
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

}
-(void)fun_Animations:(UIButton *)button
{
    [self fun_ViewAnimations];
    if(button.tag==1)
    {

        [UIView setAnimationDelay:1.0];

        button.frame=CGRectMake(290,10, 71, 96);

    }
    if(button.tag==2)
    {

        [UIView setAnimationDelay:1.2];

        button.frame=CGRectMake(390,110, 71, 96);

    }
    .....
    .....
    [UIView commitAnimations];
}

2 个答案:

答案 0 :(得分:1)

您可以使用数组索引执行此类操作而无需将标记设置为button.Here是我的代码

 - (void)viewDidLoad
{

    btnArray=[[NSMutableArray alloc] init];

    for(int i=1 ;i<10;i++)
    {
        NSLog(@"inside for loop");
        UIButton *btn_show=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn_show.frame=CGRectMake(190+(i/2), 20, 71, 96);
        [btnArray addObject:btn_show];

        [btn_show setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
        [self.view addSubview:btn_show];
        [self fun_Animations:btn_show];
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)fun_ViewAnimations
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
}

-(void)fun_Animations:(UIButton *)button
{
    [self fun_ViewAnimations];

    int index=[btnArray indexOfObject:button];

    switch (index) {
        case 0:
        {
            [UIView setAnimationDelay:1.0];
            button.frame=CGRectMake(290,10, 71, 96);
        }
            break;
            case 1:
        {
            [UIView setAnimationDelay:1.2];
            button.frame=CGRectMake(390,110, 71, 96);
        }
            break;
        default:
            break;
    }

    [UIView commitAnimations];
}

答案 1 :(得分:0)

请根据您的要求试试.... 在类级别声明此变量...

float animationDelay;

使用此代码:

animationDelay = 1.0;
for(int i=1 ;i<35;i++)
{
    NSLog(@"inside for loop");
    UIButton *btn_show=[UIButton buttonWithType:UIButtonTypeCustom];
    btn_show.frame=CGRectMake(190+(i/2), 20, 71, 96);
    btn_show.tag=i;
    [btn_show setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.view addSubview:btn_show];
    [self fun_Animations:btn_show];

}

-(void)fun_ViewAnimations
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

}

float animationDelay;
-(void)fun_Animations:(UIButton *)button
{
    [self fun_ViewAnimations];

    [UIView setAnimationDelay:animationDelay];

    CGRect frame = button.frame;
    frame.origin.x += 100;    // X which I set is based on your sample code on question, if any changes in the above sample, it is needs to adjust
    frame.origin.y += 90;     // Y which I set is based on your sample code on question, if any changes in the above sample, it is needs to adjust
    [button setFrame:frame];

    animationDelay += 0.2;    // The animationDelay which I set is based on your sample code on question, if any changes in the above sample, it is needs to adjust

    [UIView commitAnimations];
}