0

I create in this code 7 buttons with different names, and set the tag and frame for each button.

If I click a particular button it calls buttonPresed: and goes into the switch statement, branching according to the tag. However, if I click one of button 1,2,3,4,5, or 6, then the last button moves up and down. I don't want that button to move; I want each button, according to tag, to move.

-(void)btnMethod
{
    for(int i1=0;i1<[characters11arrary count];i1++)
    {
        NSString *str=[[NSString alloc]init];
                  str=[characters11arrary objectAtIndex:i1];

        NSInteger idcard = [str integerValue];
        idcard--;

        btn=[[UIButton alloc]initWithFrame:CGRectMake(-15,140+w11,70,55)];
        [btn setBackgroundImage:[arrayPlayerCard objectAtIndex:idcard] forState:UIControlStateNormal];

        btn.tag=j11;
        [btn addTarget:self action:@selector(buttonPresed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];     
        w11+=20;
        j11+=1; 
        idcard=0;
    }

}
-(void)buttonPresed:(id)sender
{
    UIButton *btnTag=(UIButton*)sender;
    d =btnTag.tag;
    NSLog(@"tagc= %i",d);
  switch(d)
    {
        case 1:
            if(t==1)
            {

            [UIView beginAnimations:nil context:nil];           
            [UIView setAnimationDuration:0];
            [UIView setAnimationBeginsFromCurrentState:YES];
            CGAffineTransform transform1 =CGAffineTransformMakeTranslation(30,0);               

            [btn setTransform:transform1];              
             t=0;
             break;
            }
            else //if(t==0)
            {
                [UIView beginAnimations:nil context:nil];           
                [UIView setAnimationDuration:0];
                [UIView setAnimationBeginsFromCurrentState:YES];
                CGAffineTransform transform1 = CGAffineTransformMakeTranslation(0,0);   
                [btn setTransform:transform1];
                t=1;
                break;

            }
case 2:....................................
.......................................................
}

1 Answer 1

2

you use [btn setTransform:...]instead of [sender setTransform:...]
think 'btn' has the address of the last allocated Button, so your function moves everytime the last button.

and if you use sender you dont need the switch.

-(void)buttonPresed:(id)sender
{
if ([sender transform].ty == 0) {
    [UIView beginAnimations:nil context:nil];           
    [UIView setAnimationDuration:  1];
    [UIView setAnimationBeginsFromCurrentState:YES];
    CGAffineTransform transform1 =CGAffineTransformMakeTranslation(0,-30);               

    [sender setTransform:transform1];  
    [UIView commitAnimations];
}
else{
    [UIView beginAnimations:nil context:nil];           
    [UIView setAnimationDuration:  1];
    [UIView setAnimationBeginsFromCurrentState:YES];
    CGAffineTransform transform1 =CGAffineTransformMakeTranslation(0,0);               

    [sender setTransform:transform1];  
    [UIView commitAnimations];
}
}

this works, but your translation does a right-left animation not up-down, changed that in my code

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.