trying to slide a new UIView out from behind a UIButton ising UIAnimation
-
15-02-2021 - |
Question
I'm attempting to push a button menu (a UIView containing UIButtons) out from behind a main UIButton
I've included an link to an image to give a better idea of what I'm getting at (the blue part containing the buttons slides out from behind the main button when the main button is pressed) -
http://imageshack.us/photo/my-images/27/screenshot20111027at419.png/
I created my UIView & placed my buttons on it & it animates without any problems. My plan was to place the buttons "off screen" (in relation to the coordinate system of my UIView) and then animate it so that the UIView slides into place (like a chest of drawers, the UIView is completely hidden initially)
However the buttons appear overlayed on the original UIButton which isn't what I wanted ;)
Any suggestions?, is there any way of hiding a UIView in certain parts of the main view?
Sorry for the long-windedness of my Question but I'm new to this :) Any help is much appreciated!
T*
Solution
@Octave1
I am unable to comment on your question, but think it would help if you mocked up an image of what is happening vs what you want to see.
Alternatively, some of your code would help us solve the problem.
You can hide the view that you want don't want to see while animating the menu into place. Here is an example(Sorry if I got your views mixed up)
/*
assume buttonMenu is defined
assume showMenuButton is defined
assume menuEndFrame is defined
*/
[UIView beginAnimations:@"ButtonMenu" context:nil];
[UIView setAnimationDuration:0.25];
buttonMenu.frame = menuEndFrame
showMenuButton.alpha = 0.0
[UIView commitAnimations];
If this isn't the issue and your view hierarchy is the issue, then you can use these 2 methods to help with the layering
- (void)bringSubviewToFront:(UIView *)view
- (void)sendSubviewToBack:(UIView *)view
OTHER TIPS
Thanks for the useful suggestions, in the end it was the view hierarchy which was the issue, this proved useful:
- (void)sendSubviewToBack:(UIView *)view
To make clearer what I was trying to achieve I've included a video to demonstrate (note I needed to use another view to sit behind my main menu button so that the sliding menu isn't partially visible behind the menu button, this is the dark grey lower half view. I used the darker shade of grey to show the position of this view)
http://www.youtube.com/watch?v=nllXGh9sSls
Here is some of my code for anyone that might have a similar issue:
- (void) setupInterface {
//setting up the main button & the sliding button menu
[self addMyButton];
buttonMenu = [[ButtonMenu alloc] initWithFrame:(CGRectMake(100, 310, 120, 200)) ];
[self.view addSubview:buttonMenu];
[self.view sendSubviewToBack:buttonMenu];
}
- (void)MenuButtonClicked:(id)sender
{
[self rollOutIn]; //Call Flip BOOL
if (!rollOutMenuButton)
{
//pushing out the menu
buttonMenu.alpha = 0.0f;
[UIView beginAnimations:@"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.8f];
self.buttonMenu.frame = CGRectMake(100, 110, 120, 200);
buttonMenu.alpha = 1.0f;
[UIView commitAnimations];
}
else
{
//pulling the menu back in
[UIView beginAnimations:@"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.8f];
self.buttonMenu.frame = CGRectMake(100, 310, 120, 200);
buttonMenu.alpha = 0.0f;
[UIView commitAnimations];
}
}
Hope this helps someone else out :)
This is exactly the use case that UIPopOverController was designed to address. I propose that you're trying to reinvent the wheel.
Here's an excellent tutorial on UIPopOverController from Ray Wenderlich:
http://www.raywenderlich.com/1056/ipad-for-iphone-developers-101-uipopovercontroller-tutorial
EDIT: Per the comments and tags, this is a question about iPhone, not iPad. The UIPopOverController only exits for iPad, but there are a couple third party libraries that bring similar functionality to the phone.
Here's one: http://iosdevelopertips.com/open-source/ios-open-source-popover-api-for-iphone-wepopover.html
Never used it, can't recommend it, but it seems to have some fans out there. Googling "popover for iphone" will get you several tutorials and suggestions.