0

这里我使用标签值来区分 uibuttons。我想在不使用标签值的情况下将 uibuttons 移动到桌子周围的不同位置(如 zynga poker)?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];
}
4

2 回答 2

1

您可以使用数组索引来执行类似的操作,而无需将标签设置为按钮。这是我的代码

 - (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];
}
于 2013-04-18T09:22:00.367 回答
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];
}
于 2013-04-18T09:29:25.537 回答