Question

I want a button that toggles the position of an element on click.

My current code:

-(IBAction)menuTouched{

// Push menu
UITableView * sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
sideMenu.backgroundColor = [UIColor redColor];

[self.view addSubview:sideMenu];

// Animate
[UIView animateWithDuration:1.25 animations:^{
    sideMenu.frame = CGRectMake(960, 88, 63, 661);
}];
}

When clicking the button it animates the tableview in just like I want to. But now I want to animate the tableview out by pressing the exact same button. So in short I want the button to toggle in and out a tableview by changing the X position. What is the best/easiest approach?

Thanks in advance.

Was it helpful?

Solution

inside your .h file add

UITableView * sideMenu;
 BOOL checkInOut;

inside .m file add

- (void)viewDidLoad
{
    [super viewDidLoad];
     checkInOut=False;
    // Push menu
    sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
    sideMenu.backgroundColor = [UIColor redColor];

    [self.view addSubview:sideMenu];
}


-(IBAction)menuTouched
{
 checkInOut=!checkInOut
 if(checkInOut)
  {
   // Animate
   [UIView animateWithDuration:1.25 animations:^{
    sideMenu.frame = CGRectMake(960, 88, 63, 661);
   }];
 }
 else
  {
   [UIView animateWithDuration:1.25 animations:^{
    sideMenu.frame = CGRectMake(1060, 88, 63, 661);
   }];
  }
}

OTHER TIPS

You will need a bool to store the state where you are, but you can just use the button.selected state for that.. Something like that:

- (IBAction)menuTouched:(id)sender
{
    sender.selected = !sender.selected;

    if (sender.selected)
    {
        // toggle on stuff
    }
    else
    {
        // toggle off stuff
    }
}

Try it....

 [UIView beginAnimations: nil context: nil];
 [UIView setAnimationBeginsFromCurrentState: YES];
 [UIView setAnimationDuration: 0.5];
 myButton.frame = CGRectMake(0,420,320,120);
 [UIView commitAnimations];

Hope i helped.

you can perform the same animation with different frame as you set previously

sideMenu.frame = CGRectMake(1060, 88, 63, 661);

with the check to see have you togged the table in or out to perform respective action.

Hope this helps.

You have to receive as UIButton reference instead of id

- (IBAction)menuTouched:(UIButton *)sender
    {
        sender.selected = !sender.selected;

        if (sender.selected)
        {
            [UIView animateWithDuration:1.25 animations:^
    {
       UITableView * sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
         sideMenu.backgroundColor = [UIColor redColor];
        [self.view addSubview:sideMenu];
        sideMenu.frame = CGRectMake(960, 88, 63, 661);
       }];
        }
        else
        {
            [UIView animateWithDuration:1.25 animations:^{
        [sideMenu removeForSuperView];
       }];
        }
    }

1.set a flag bit to represent UITableView's current state.The statement need 3 variables.

BOOL isShow;
UITableView *sideMenu;
CGFloat posX;

2.initial setting in viewDidLoad

-(void)viewDidLoad {
    isShow=NO;
    //to do something...
    sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
    sideMenu.backgroundColor = [UIColor redColor];
    [self.view addSubview:sideMenu];
}

3.What UITableView shows is just the change of X-coordinate of frame.so you can calculate posX depend on toggles 'state.and notice don't create UITableView at each time clicking.put the UITableView creat code on viewDidLoad function.

-(IBAction)menuTouched{
   posX=isShow ? 1060 : 960       
   // Animate
   [UIView animateWithDuration:1.25 animations:^{
       sideMenu.frame = CGRectMake(posX, 88, 63, 661);
   }];
   isShow=!isShow;  //boggle button
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top