0

Here I used tag values to differentiate the uibuttons. I want to move the uibuttons to different positions around table (like zynga poker)without using tag values? X and Y positions has to differ. for example if i write x+=100; y+=12; every time x and y positions will be incremented by same value but in round table x and y positions wont be incremented by same values.

With the below code i can move the uibutton(or uiimageviews) to that positions using tag values but code will become more(means it'll occupy more memory).

I just wanna distribute 35 image cards.

Can anyone provide me some information regarding this?

Code:

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];
}
Sahara Pune
  • 129
  • 2
  • 9
  • You could for instance create a subclass of UIButton including the target frame destination of you animation. – Javier Quevedo Apr 18 '13 at 08:56
  • @sEnC Subclassing `UIButton` is not a create idea because of it being a class cluster, see the comments on this question http://stackoverflow.com/questions/6443639/objective-c-buttons-created-from-subclass-of-uibutton-class-not-working – Popeye Apr 18 '13 at 09:00
  • Interesting, thanks for that, I had no clue about it. – Javier Quevedo Apr 19 '13 at 09:20

2 Answers2

1

You can use array index to do something like this without setting tag to button.Here is my code

 - (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];
}
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
0

Try this for your requirement.... Declare this variable at class level...

float animationDelay;

Use this code:

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];
}
Bhanu Prakash
  • 1,493
  • 8
  • 20