1
votes

I have a UIButton set up in a storyboard that when pressed animates a UIVIew. I would like the same button to animate/move the UIView back to it's original point when it is pressed a second time.

What is the best way to do this?

thanks for any help or pointers.

this is the code I'm using to animate and move the UIVIew:

I'm using a BOOL named buttonCurrentStatus.

-(IBAction)sortDeals:(UIButton*)sender {

if (buttonCurrentStatus == NO)
{
    buttonCurrentStatus = YES;

CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;


[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

self.menuTop.frame = menuTopFrame;

[UIView commitAnimations];
    NSLog(@"Sort Deals touched button status = yes");


} else {
        buttonCurrentStatus = NO;

        CGRect menuTopFrame = self.menuTop.frame;
        menuTopFrame.origin.y = 30;


        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

        self.menuTop.frame = menuTopFrame;

        [UIView commitAnimations];


NSLog(@"Sort Deals touched button status = no");

    }
3
It is not enough a CGRect ivar to keep the last frame? - Mat
I don't understand. Do you have an example where it's set up correctly? I'm really interested in learning how to have a button on the first touch perform one action then on the 2nd touch perform a different action. thanks - hanumanDev
I mean that you can use a CGRect class variable to store temporarily the last frame. Then when you retap on the button, just reset the button position to the last frame used. Or otherwise, if the end frame is always the same, chek for the sender.frame and reset the frame based on your requirements. (sorry i am on the iPad, if no one answers you, i'll do asap). - Mat
@hanumanDev you can use a BOOL to store the current state and flip the bool each time the button is pressed. - user529758
When I get back home I'll give you a code example if no one has by then. - Josiah

3 Answers

1
votes

Another more general way is to use an IBAction that will execute one of two methods. Like previous answers, you can use a BOOl value, or int in order to determine what action to use. This is a more general answer, and can be used for many purposes.

First, you will need a BOOL variable. For this example I put the Bool variable as boolVarible (Yes, I know I spelled it wrong). By default I have it set to YES.

bool boolVarible = YES;

I made that a class variable.

-(IBAction)buttonAction:(id)sender {
    if (boolVarible == YES)
    {
        //Execute first action
        [self firstAction];
    }
    else
    {
        //Execute second action
        [self secondAction];
    }
}

-(void)firstAction {
    //Do something here...
}

-(void)secondAction {
    //Do something here...
}

Hopefully, you get the idea. You can then swap actions whenever you want. Just simply change the BOOl value, and then when the button is pressed, it will execute another method. I find this cleaner than doing it all in one action. Good luck.

1
votes
-(IBAction)sortDeals:(UIButton*)sender {

sender.selected = !sender.selected;

int moveby = (sender.selected) ? 30: -30;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
menuTop.frame = CGRectMake(menuTop.frame.origin.x, menuTop.frame.origin.y + moveby, menuTop.frame.size.width, menuTop.frame.size.height);
[UIView commitAnimations];
}
1
votes

Very Simply Solution for your Question is if you Declare two Boolean Variables in Class where you need to implement this method.your Code will be look like this

In .m file

BOOL Action1;
BOOL Action2;

Now In ViewDidload method

 (void)viewDidLoad 
 {
   Action1=TRUE;
   Action2=FALSE;
   [super viewDidLoad];

  }

And now your Method

  (IBAction)sortDeals:(UIButton*)sender {
   if (Action1 == TRUE)
 {

   CGRect menuTopFrame = self.menuTop.frame;
   menuTopFrame.origin.y = 30;
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:0.5];
   [UIView setAnimationDelay:0.0];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
   self.menuTop.frame = menuTopFrame;
   [UIView commitAnimations];
   NSLog(@"Sort Deals touched button status = yes");
     Action1=FALSE;
     Action2=TRUE;
    }
   else 
    if (Action2 == TRUE)
   {

   CGRect menuTopFrame = self.menuTop.frame;
    menuTopFrame.origin.y = 30;
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.menuTop.frame = menuTopFrame;
   [UIView commitAnimations];
   NSLog(@"Sort Deals touched button status = no");
   Action1=TRUE;
   Action2=FALSE;
   }

Hope it Works for you.Thanks