How to unhide view with animations - iphone

Say I have a hidden view in Xcode for iOS. Now, when I set the view to not hidden (view.hidden=NO), how can I make it so that it now appears, but with animations?

What you probably want is not to set view.hidden, but to set view.alpha to 0 (corresponds to hidden = YES) or 1 (hidden = NO).
You can then use implicit animations to show the view, e.g
[UIView animateWithDuration:0.3 animations:^() {
view.alpha = 1.0;
}];

If you want other animations than only fading then use this method
[UIView transitionWithView:_yourView duration:1.0 options:UIViewAnimationOptionTransitionCurlDown animations:^(void){
[_yourView setHidden:NO];
} completion:nil];

For a fade, you can adjust the alpha property of the view.
myView.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
myView.alpha = 1;
}];
That will apply a fade in over 0.5 seconds to the view myView. Many UIView properties are animatable; you aren't just limited to alpha fades. You can change background colours, or even rotate and scale a view, with animation. If you need further control and advanced animation, you can then move into Core Animation - a much more complex animation framework.

-(void)showView{
[UIView beginAnimations: #"Fade Out" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:.5];
//show your view with Fade animation lets say myView
[myView setHidden:FALSE];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(hideView) userInfo:nil repeats:YES];
[UIView commitAnimations];
}
-(void)hideView{
[UIView beginAnimations: #"Fade In" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:.5];
//hide your view with Fad animation
[myView setHidden:TRUE];
[UIView commitAnimations];
}
OR you can try this way
self.yourView.alpha = 0.0;
[UIView beginAnimations:#"Fade-in" context:NULL];
[UIView setAnimationDuration:1.0];
self.yourView.alpha = 1.0;
[UIView commitAnimations];

Related

How to move the view up on button click

When i click on button it shows up the picker view and hide the button,User have no idea what's going on behind the picker view.
Any Solution:-
Like we have solution for textfield, the view goes up on selection of textfield with animateTextField.
you can use the method animate view upward to move the view upward and can move the view downward using the methods below :
-(void)animateViewUpward
{
[UIView beginAnimations: #"upView" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.4];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.view.frame = CGRectMake(0,-110, 320,460);
[UIView commitAnimations];
}
-(void)animateViewDownward
{
[UIView beginAnimations: #"downView" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.4];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.view.frame = CGRectMake(0,0, 320,460);
[UIView commitAnimations];
}
On button click use animations
[UIView animateWithDuration:2.0
animations:^{
self.view.frame = CGRectMake(0,-30,320,400);
}
completion:^(BOOL finished){
;
}];
And when you press done or cancel of toolbar OR you are done with picker selection
[UIView animateWithDuration:2.0
animations:^{
self.view.frame = CGRectMake(0,0,320,400);
}
completion:^(BOOL finished){
;
}];
You can add the UIKeyboardScrollView on view. It will move up the components whenever you will click the button or anything else.

Fade animation when scrolling horizontal table view

I have a horizontal table view and want to change images each 5 secs. I want to change images with fade animation so the old image fades out and the new one fades in. So i call this method:
self.slideTimer = [NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:#selector(slideToNextImage)
userInfo:nil
repeats:YES];
And here is my slideToNextImage:
self.lastIndexPath = indexPath;
[UIView beginAnimations:#"FadeAnimations" context:nil];
[UIView setAnimationDuration:2];
self.horizontalView.alpha = 0.1f;
[self.horizontalView.tableView scrollToRowAtIndexPath:self.lastIndexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
[UIView commitAnimations];
[UIView beginAnimations:#"FadeAnimations" context:nil];
[UIView setAnimationDuration:2];
self.horizontalView.alpha = 1.0f;
[UIView commitAnimations];
With my realisation the image fades too fast and i see the second image scrolling with no fade animation
the second animation starts without waiting for the first to end.
Try something like this instead:
[UIView animateWithDuration:2.0 animations:^{
self.horizontalView.alpha = 0.1f;
[self.horizontalView.tableView scrollToRowAtIndexPath:self.lastIndexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
} completion:^(BOOL finished) {
[UIView animateWithDuration:2 animations:^{
self.horizontalView.alpha = 1.0f;
}];
}];

Beginner: Multiview app doesn't switch views correctly

I wrote a basic two-view application that switches from one view to another every time I press a button.
But for some reason when I run it on simulator, both views are always few pixels above the MainWindow.xib view, always being on top of it. And what's strange is that there is no animation when I switch between Views.
What is the problem???
This is what I have in my AppDelegate.m
-(void)switchView:(UIView *)view1 toView:(UIView *)view2{
[UIView beginAnimations:#"Animation" context:nil];
[UIView setAnimationDuration:1.75];
[UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromLeft forView:self.window cache:YES];
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView commitAnimations];
}
Here are two very nice examples:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13427-uiview-slide-transition.html
http://www.dizzey.com/development/ios/simple-uiview-transitions-animation-using-blocks-in-ios-4/
Or this one:
iOS 4.2: Flip image using block animations
Hi Try This,
-(void)switchView:(UIView *)view1 toView:(UIView *)view2
{
view2.frame = self.window.bounds;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:NO];
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView commitAnimations];
}
Also try this
[UIView transitionWithView:view2 duration:0.5
options:UIViewAnimationTransitionFlipFromLeft //change to whatever animation you like
animations:^ { [window addSubview:view2]; }
completion:nil];
Can you post some code regarding this .Because if you click on Segment change
it will change the Views.IF you take two views on same interface Builder.
Try out this
IBOutlet UIView *viewA;
IBOutlet UIView *viewB;
in .m file
-(IBAction)SegmentChange
{
if(segment.selectedSegmentIndex==0)
{
viewA.hidden=NO;
viewB.hidden=YES;
}
else if(segment.selectedSegmentIndex==1)
{
[self.View addSubview:viewB];
viewA.hidden=YES;
viewB.hidden=NO;
}
}

fliping view continiously

this my code
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
if ([sender tag] == 1) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:placeholder cache:YES];
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
}
if (view1OnTop) {
[view1 removeFromSuperview];
[placeholder addSubview:view2];
}
else {
[view2 removeFromSuperview];
[placeholder addSubview:view1];
}
[UIView commitAnimations];
view1OnTop = !view1OnTop;
i want to continuously flip the view, for say some duration 1 min.
it should be continuously flipping.
how can i do it.
what i m trying to is, i want a view, which should be continuously flipping for some particular amount of time.
how can i achieve this?
regards
Apart from the flipping animation, which I presume you have working, you need to initiate a new animation when the current one is finished.
Before [UIView commitAnimations], do this:
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDone:finished:context:)];
add a function
-(void)animationDone:(NSString*)id finished:(NSNumber*)n context:(void*)context
and let that fire the next round.
edit: you do this by putting in the code to fire an animation, so the typical block from [UIView beginAnimations...] to [UIView commitAnimations]. The better solution of course is to put the animation starting code in a separate function, so the outline will look like:
...
[self startAnimationLoop];
...
-(void)startAnimationLoop
{
[UIView beginAnimtions...];
// do the animation stuff
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDone:finished:context:)];
[UIView commitAnimations];
}
-(void)animationDone:(NSString*)id finished:(NSNumber*)n context:(void*)context
{
[self startAnimationLoop];
}
to make it go back/forth, add some state variable, or create 2 sets of these functions which call eachother (startAnimationLoop1 and startAnimationLoop2, each one firing the other when done)

Is it possible to removeFromSuperview with Animation?

I have 2 layer is top and bottom on my program
how can i remove top layer with animation or bring top layer to back is it possible?
The easiest is playing with frame and alpha before removing it.
You can get some cool effects
-(void)removeWithEffect:(UIView *)myView
{
[UIView beginAnimations:#"removeWithEffect" context:nil];
[UIView setAnimationDuration:0.5f];
//Change frame parameters, you have to adjust
myView.frame = CGRectMake(0,0,320,480);
myView.alpha = 0.0f;
[UIView commitAnimations];
[myView performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:0.5f];
}
iOS Update
You can now use blocks to perform your animation
[UIView animateWithDuration:0.5f
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
If you're targetting iOS 4.0 upwards you can use animation blocks:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
(above code comes from Apple's UIView documentation)