Hide all View containing By making height 0 of parent UiView - iphone

I am making one animation and in it i Have to make height 0 of one UIView named "subView".
now that subView contain one UIButton and UIImageView.
now i make height 0 by this method on button click
#interface ViewController : UIViewController
{
IBOutlet UIView *suvView;
IBOutlet UIButton *subButton;
IBOutlet UIImageView *imgview;
}
-(IBAction)hide:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:suvView];
[suvView addSubview:subButton];
[suvView addSubview:imgview];
}
-(IBAction)hide:(id)sender
{
[UIView beginAnimations:nil context:suvView];
[UIView setAnimationDuration:0.25];
[suvView setFrame:CGRectMake(0, 94, 320, 0)];
[UIView commitAnimations];
}
#end
but when i make height 0 than View named "subButton" and "imgview" are look as it is.
i want to make Them hidden when subView height is 0.
i am using XCode 4.5

If you want to hide them without resizing: [mainview setClipsToBounds:YES]

You have two methods for doing this :-
Firstly, If you are making the suvViews height 0 then you do not need to remove it. But simply hide the imgview and the subButton like
-(IBAction)hide:(id)sender
{
[UIView beginAnimations:nil context:suvView];
[UIView setAnimationDuration:0.25];
[suvView setFrame:CGRectMake(0, 94, 320, 0)];
[UIView commitAnimations];
subButton.hidden = YES;
imgview.hidden = YES;
}
#end
Also what you can do is reduce the heights of subButton and imgview with the suvView's height like
-(IBAction)hide:(id)sender
{
[UIView beginAnimations:nil context:suvView];
[UIView setAnimationDuration:0.25];
[suvView setFrame:CGRectMake(0, 94, 320, 0)];
[imgview setFrame:CGRectMake(0, x, y, z];
[subButton setFrame:CGRectMake(0, x, y, z];
[UIView commitAnimations];
}
where x,y,z are the coordinates of the imgview and the subButton. Hope this helps.

//try this code for view animation
-(IBAction)hide:(id)sender
{
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseOut
animations:^{
[suvView setFrame:CGRectMake(0, 94, 320, 0)];
}
completion:nil];
}

Add the following line before starting the animation:
subButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
Or you could do this from the interface builder also by setting the vertical and horizontal resize for each sub view (the button and image view)
Btw you should be using the apple recommended block based animation(if you are using iOS 4.0 and above) rather than beginAnimations: and commitAnimations:.

Related

Subviews are not removing while removing the superview in iPad

I created a UIView.A search bar is added as subview on the view.The search bar is also created through code.
I have given a UIAnimation to the view to get a dropdown like effect to the view.
Upto this point it is working fine.
The problem is when i remove the superview the view is go but the search bar is still there.
Here are some codes:
#interface ATViewController : UIViewController<UISearchBarDelegate>
{
UIView *dropView;
UISearchBar *searchBar;
}
- (void)viewDidLoad
{
dropView = [[UIView alloc]initWithFrame:CGRectMake(70, 70, 150, 100)];
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,150,0)];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)buttonClicked:(id)sender{
// self.searchBar.delegate = self;
[dropView addSubview:searchBar];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.9];
dropView.backgroundColor = [UIColor blueColor];
dropView.frame = CGRectMake(70, 70, 150, 550);
self.searchBar.frame=CGRectMake(0,0, 150, 40);
[UIView commitAnimations];
[self.view addSubview:dropView];
}
-(void)doSingleTap:(UITapGestureRecognizer *)sender
{
switch (sender.numberOfTapsRequired) {
case 1:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
dropView.frame = CGRectMake(70, 70, 150, 0);
self.searchBar.frame=CGRectMake(0, 0, 150, 0);
[UIView commitAnimations];
break;
default:
break;
}
}
Adding the subview
After removing the subView
You are not removing the super view in code. You are just reducing its height to zero. If you want the searchBar to be clipped along with the dropView then set dropView's clipToBounds to YES.
i.e;
dropView.clipToBounds = YES:
before you reduce its height to zero.
Noticed that you are reducing the search bars height to zero as well. But I think you are not allowed to edit the height of the UISearchBar.

UIAnimation - Show UIView from button.frame.origin.y

I have a UIButton somewhere on my view. On touch event of button I making a UIView appear. The UIAnimation I have used make the view appear from top of window. But I want it to appear from button.frame.origin.y . Before touching the button the view is not visible. On touching the button view should start appearing from above position.
Here is the code :
-(IBAction) ShowInfowView:(id)sender{
CGRect rect = viewInfo.frame;
rect.origin.y = rect.size.height - rect.size.height+58.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.70];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewInfo.frame = rect;
[UIView commitAnimations];
}
This is how I am hiding the view again :
-(IBAction) HideInfoView:(id)sender{
CGRect rect = viewInfo.frame;
rect.origin.y = -rect.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.70];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewInfo.frame = rect;
[UIView commitAnimations];
}
In viewDidLoad I am doing the following :
CGRect rect = viewInfo.frame;
rect.origin.y = -rect.size.height;
viewInfo.frame = rect;
UPDATE:
Please see this example. Here view is appearing from top of screen. I want it to appear from button y axis. For that matter please consider button y position a bit upwards.
So you want a slide-in effect, but not from the top of the screen, just some arbitrary value?
One way to do it:
1) You should create a view that has the dimensions and position of your desired view AFTER animation finishes, we'll call it baseview.
2) Set this baseview property clipsToBounds to YES. This will make all subviews that are outside of the baseview's frame invisible.
3) Add your animating view as a subview of the baseview, but set the frame so it is invisible (by plcacing it above the baseview):
frame = CGRectMake(0, -AnimViewHeight, AnimViewWidth, AnimViewHeight);
4) Animate the animview frame:
//put this inside of an animation block
AnimView.frame = CGRectMake(0, 0, AnimViewWidth, AnimViewHeight);
EDIT:
Example:
//define the tags so we can easily access the views later
#define BASEVIEW_TAG 100
#define INFOVIEW_TAG 101
- (void) showInfo
{
//replace the following lines with preparing your real info view
UIView * infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[infoView setBackgroundColor: [UIColor yellowColor]];
[infoView setTag: INFOVIEW_TAG];
//this is the frame of the info view AFTER the animation finishes (again, replace with real values)
CGRect targetframe = CGRectMake(100, 100, 100, 100);
//create an invisible baseview
UIView * baseview = [[UIView alloc] initWithFrame:targetframe];
[baseview setBackgroundColor: [UIColor clearColor]];
[baseview setClipsToBounds: YES]; //so it cuts everything outside of its bounds
[baseview setTag: BASEVIEW_TAG];
//add the nfoview to the baseview, and set it above the bounds frame
[baseview addSubview: infoView];
[infoView setFrame:CGRectMake(0, -infoView.bounds.size.height,
infoView.bounds.size.width, infoView.bounds.size.height)];
//add the baseview to the main view
[self.view addSubview: baseview];
//slide the view in
[UIView animateWithDuration: 1.0 animations:^{
[infoView setFrame: baseview.bounds];
}];
//if not using ARC, release the views
[infoview release];
[baseview release];
}
- (void) hideinfo
{
//get the views
UIView * baseView = [self.view viewWithTag: BASEVIEW_TAG];
UIView * infoView = [baseView viewWithTag: INFOVIEW_TAG];
//target frame for infoview - slide up
CGRect out_frame = CGRectMake(0, -infoView.bounds.size.height,
infoView.bounds.size.width, infoView.bounds.size.height);
//animate slide out
[UIView animateWithDuration:1.0
animations:^{
[infoView setFrame: out_frame];
} completion:^(BOOL finished) {
[baseView removeFromSuperview];
}];
}
I have done exactly what you're trying in my recent project.
The following steps should make this work:
1. In your viewDidLoad: you init your viewToFadeIn like this:
viewToFadeIn = [[UIView alloc] initWithFrame:CGRectMake(20,self.view.frame.size.height+10, self.view.frame.size.widh-40, 200)];
//its important to initalize it outside your view
//your customization of this view
[self.view addSubview:viewToFadeIn];
2.your ShowInfowView: Method should look like this:
` -(IBAction) ShowInfowView:(id)sender{
[UIView beginAnimations:#"fadeIn" context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished:finished:context:)];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, -290);
//290 is in my case, try a little for your correct value
[UIView commitAnimations];
}
3.yourHideInfoView:` Method looks like this
-(IBAction) HideInfoView:(id)sender{
[UIView beginAnimations:#"fadeOut" context:NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];
}
EDIT:
4. animationFinishedMethod:
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
if ([animationID isEqual:#"fadeIn"]) {
//Do stuff
}
if ([animationID isEqual:#"fadeOut"]) {
//Do stuff
}
}
SEE THIS
This should do the trick. Good luck
Your problem is not clear (for me).
What is this?
rect.origin.y = rect.size.height - rect.size.height+58.0;
Is 58 origin of your UIButton?
You should use sender.frame etc
Use blocks to animate like this
[UIView animateWithDuration:0.5f animations:^{
// Animation here
} completion:^(BOOL finished) {
// onComplete
}];
- (void) slideIn{
//This assumes the view and the button are in the same superview, if they're not, you'll need to convert the rects
//I'm calling the animated view: view, and the button: button...easy enough
view.clipToBounds = YES;
CGRect buttonRect = button.frame;
CGRect viewRect = CGRectMake(buttonRect.origin.x, buttonRect.origin.y + buttonRect.size.height, view.bounds.size.width, 0);
CGFloat intendedViewHeight = view.height;
view.frame = viewRect;
viewRect.size.height = intendedViewHeight;
[UIView animateWithDuration:.75 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
view.frame = viewRect;
}completion:nil];
}
This will cause the the view to appear to slide out from under the button. To slide the view back in, you just reverse the animations. If you wish to slide it up from the button, you'll need to make sure your starting 'y' is the 'y' value of the button (rather than the 'y' + 'height') and animate both the height and y values of the view that needs animating.

iPhone trying to use a block animation to simulate a card flip, but nothing happens no animation

I'm trying to do a card flip animation...but nothing happens.
this is what I do
1. create a container view, to hold the two views to animate from.
2. Create the first view.
3. add it to the container.
4. create the second view
3. start the animation block, beginAnimations
4. call setAnimationTransition put in the view container
5. add the second view to the view container
6. commit the animation,
my code
// create container view
CGRect viewRect = CGRectMake(10, 10, 100, 100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];
// create 2 viues to flip between for animation
UIImage *i= [ UIImage imageNamed : #"card1.jpg"];
UIImageView *topCard=[ [UIImageView alloc] initWithImage: i ];
topCard.frame= CGRectMake(0,0,100,100);
[myView addSubview:topCard];
[self.view addSubview:myView];
i= [ UIImage imageNamed : #"card2.jpg"];
UIImageView *butCard=[ [UIImageView alloc] initWithImage: i ];
butCard.frame= CGRectMake(0,0,100,100);
// set up the animation
[UIView beginAnimations:#"Flip Top Card" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut ];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES];
[myView addSubview:butCard];
// start the animation
[UIView commitAnimations];
-Ted
If this is iOS 4.0 or later you can do:
[UIView transitionWithView:myView
duration:0.3f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^(void) {
self.topCardIsShown = !self.topCardIsShown;
self.topCard.hidden = !self.topCardIsShown;
self.butCard.hidden = self.topCardIsShown;
}
completion:^(BOOL finished) {
// nothing
}];
This assumes that you have added both card views to the myView and have added a #property(nonatomic, assign) BOOL topCardIsShown; to your class.

iphone: expand and collapse a UIView programmatically

I am very new to iPhone/iPad development.
can you please help me create this programmatically. I want to expand/collapse a UIView programmatically.
This expandable/collapsable view will have some text field and lables which should appear and disappear with that view
Suppose you have a UIView instance in the UIViewController class like this:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, w1, h1)];
[self.view addSubview:view];
based on the requirement you set the view visibility as I did here .. I am not displaying the view when the controller is loading it's view ..
[view setHidden:YES];
Maintain a flag to check the visibility of the view instance .. lets say isViewVisible is my flag to check the visibility of the view .. I set it to NO in the begning ..
isHelpViewVisible = NO;
and I wrote an action method (viewClicked) here to expand and to collapse the view object , give this action method to a button instance and it will work.
- (void)viewClicked:(id)sender {
if (!isViewVisible) {
isViewVisible = YES;
[view setHidden:NO];
[UIView beginAnimations:#"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, w1, h1)];
[UIView commitAnimations];
} else {
isViewVisible = NO;
[view setHidden:NO];
[UIView beginAnimations:#"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, width, hight)];
[UIView commitAnimations];
}
}
and add the textfields and labels objects to the view object as subviews and set the animation to those objects as well .. it will work.
Replaces the following from above:
[UIView beginAnimations:#"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, w1, h1)];
[UIView commitAnimations];
With the New way of doing animations:
[UIView animateWithDuration:1.3f animations:^{
self.frame = CGRectMake( x1, x2, w1, h1);
} completion:^(BOOL finished) {
// this gives us a nice callback when it finishes the animation :)
}];
To change the size / position of a UIView within its parent just change its frame property:
CGRect newFrame = myView.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
myView.frame = newFrame;
You can try this
CGRect frame = [currentView frame];
frame.size.width = 999;//Some value
frame.size.height = 444;//some value
[currentView setFrame:frame];
You can follow this whenever you want to increase/decrease view size

Loading a view from the bottom of the window using animations

i wanted to use animations to load a view from the bottom of the window, i know this can be done via presentmodalViewController but in as per the requirement of my app its not valid as i only want to load the view at half of the window. So i used Animations and heres a look at what i did
-(void) displayPicker
{
UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[done setFrame:CGRectMake(197, 199, 103, 37)];
[done setBackgroundImage:[UIImage imageNamed:#"button_0.png"] forState:UIControlStateNormal];
[done setTitle:#"Done" forState:UIControlStateNormal];
[done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[done addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
[pickerDisplayView addSubview:datePicker];
[pickerDisplayView addSubview:done];
//animating here
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[self.view addSubview:pickerDisplayView];
[UIView commitAnimations];
}
The view called pickerDisplayView has two components called Picker and Button, but the problem is that Picker is not working correctly and neither the view (pickerDisplayView) is loading in a smooth way.
#Matt: I have followed ur instructions and did as u advised here's what i did
-(void) animationIn
{
CGPoint p = {x:0,y:185};
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
[pickerDisplayView setCenter:p];
[UIView commitAnimations];
}
But the prob is that the view is going at the top of the window and is not coming at exact y axis of 185. I took these coordinates from IB. Please help me sir
You will have to add the view offscreen and then animate it in.
// In your viewDidLoad or wherever you initialize the view.
pickerDisplayView = [[UIView alloc]initWithFrame:offscreenRect];
[self.view addSubview:pickerDisplayView];
[self performSelector:#selector(animateIn) withObject:nil afterDelay:0.25];
// Declare animateIn
- (void)animateIn
{
UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[pickerDisplayView setCenter:onscreenPosition];
[UIView commitAnimations];
}
The value offscreenRect is a CGRect for the size and position where you want the view to start. onscreenPosition is a CGPoint for the position where you want the animation to finish. Animating the view's center is adequate since you don't want to change the size of the view while animating.
Also, I placed the animation in a separate method that gets called with -performSelector:withObject:afterDelay to ensure that the animation with fire. Sometimes you won't see the animation run if you start it in the same run loop where you added the view you're animating.
You need to init and addSubview before beginAnimations:. When you init pickerDisplayView position it so that the top of it is inline with the bottom of the superview. The animation will then slide up pickerDisplayView. Ideally you should create the view in a nib or in the loadView method. (I strongly recommend using nibs over 'loadView').
Do this in loadView (or move to a nib):
CGRect offScreenFrame = ?????; //Figure this out from self.view
self.pickerDisplayView = [[UIView alloc]initWithFrame:offScreenFrame];
[self.view addSubview:self.pickerDisplayView];
This is the animation:
[UIView beginAnimations:nil context:NULL];
self.pickerDisplayView.frame = CGRectMake(0, 185, 320, 275);
//[UIView setAnimationDuration:0.5]; //this line is probably not needed
[UIView commitAnimations];
Also I'd advice against using 'magic numbers' in the frame. Instead you can derive the pickerDisplayView frame from the superviews frame. If you do use 'magic' numbers be sure explain them in some way (ie assigning them to a appropriately named variable or a comment).