Question

I have used the below code to position a UIView which will hold various buttons ect. I am showing this view in an IBAction method for a button.

How would I have the user be able to click on the button to show the UIView in a default position, then, when the user clicks on the button again, move the UIView back off screen?

Am I doing the best thing moving the UIView on/off screen or would it be better to just hide/show the controller?

I like the idea of moving it as it kinda looks like its moving downward (not that its animated or anything). Actualy how would I animate this to move slower?

CGRect f = self.optionsuiview.frame;
f.origin.x = 0;
f.origin.y = 42;
self.optionsuiview.frame = f;
Was it helpful?

Solution

If you create a property of the button (ctrl+drag to header file) you can use this to change the state of the button, so you can dictate when the view moves up/down.

@property (strong, nonatomic) IBOutlet UIButton *yourButton;

Within your IBAction method could look something like this

 -(IBAction)yourButtonPressed:(id)sender{

if (self.yourButton.selected){

   self.yourButton.selected = NO;

   [UIView animateWithDuration:0.3 animations:^{
   CGRect f = self.optionsuiview.frame;
   f.origin.x = 0;
   f.origin.y = 42;
   self.optionsuiview.frame = f;
 }];
}

if (!self.yourButton.selected){
   self.yourButton.selected = YES;

   [UIView animateWithDuration:0.3 animations:^{
   CGRect f = self.optionsuiview.frame;
   f.origin.x = 0;
   f.origin.y = 0;
   self.optionsuiview.frame = f;
  }];
}

I hope this way helps with what you're looking for ,

Cheers, Jim.

OTHER TIPS

Am i doing the best thing moving the uiview on/off screen or would it be better to just hide/show the controller?

Only you or your UX advisor can answer that...

You would hide the view again in the same way you show it, by changing the view frame. It's just that to hide it you're setting the frame to a location that is off screen. And the same logic applies for animation too:

// to show

// calculate `f` here to a location off screen to animate to

[UIView animateWithDuration:0.2
                 animations:^{
        self.optionsuiview.frame = f;
    }
                 completion:^{
        self.optionsuiview.hidden = YES;
    }];

(note that we hide the view after the animation so it isn't considered a tap target)

Showing the view is the same in reverse. Set hidden to NO and then animate the frame to a visible location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top