UIView animation - objective-c

I am trying to animate a UIView here. It looks like a rectangle, and I just want to translate it to my coordinations.
So, how can I animate it? I tried to find some tutorials, but without success.

In iOS 4, the UIView block animation method is easiest:
[UIView animateWithDuration:1.0 animations:^{
myView.frame = myNewFrameRect;
}];
http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/animateWithDuration:animations:

Here is an example of animating a view to move off screen. You should be able to slightly adjust it for your needs:
[UIView beginAnimations:#"bucketsOff" context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
//position off screen
[bucketView setCenter:CGPointMake(-160, 377)];
[UIView setAnimationDidStopSelector:#selector(finishAnimation:finished:context:)];
//animate off screen
[UIView commitAnimations];

//try this AnimationTransitionCurlDown \UP
-(void)pageDown
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self cache:YES];
[UIView commitAnimations];
}
-(void)pageUP
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self cache:YES];
[UIView commitAnimations];
}
//flip uiviews using animation
-(void)FlipFromLeft
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
[UIView commitAnimations];
}
-(void)FlipFromRight
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[UIView commitAnimations];
}

Use "QuartzCore.framework"
With these animations.
-(void)viewSlideInFromRightToLeft:(UIView *)views
{
CATransition *transition = nil;
transition = [CATransition animation];
transition.duration = 0.5;//kAnimationDuration
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
transition.delegate = self;
[views.layer addAnimation:transition forKey:nil];
}
-(void)viewSlideInFromLeftToRight:(UIView *)views
{
CATransition *transition = nil;
transition = [CATransition animation];
transition.duration = 0.5;//kAnimationDuration
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[views.layer addAnimation:transition forKey:nil];
}
-(void)viewSlideInFromTopToBottom:(UIView *)views
{
CATransition *transition = nil;
transition = [CATransition animation];
transition.duration = 0.5;//kAnimationDuration
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromBottom ;
transition.delegate = self;
[views.layer addAnimation:transition forKey:nil];
}
-(void)viewSlideInFromBottomToTop:(UIView *)views
{
CATransition *transition = nil;
transition = [CATransition animation];
transition.duration = 0.5;//kAnimationDuration
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromTop ;
transition.delegate = self;
[views.layer addAnimation:transition forKey:nil];
}

[UIView beginAnimations:#"your text" context:nil];
[UIView setAnimationDuration:0.4]; //Your animation duration
[UIView setAnimationDelegate:self];
//
write your code here , what you want to animate
//
[UIView commitAnimations];
Working Fine definitely.

Related

cocoa touch trying to delay the animation of the uiview before it pops out again

I have a UIView that serves as the container for 2 tableviews. I have two buttons that controls how data is loaded on those tableviews. Basically when 1 button is tapped the uiview slides out to show the tableview related to that button, and when the other button gets tapped I need it to:
close
hide the 1st tableview
then unhides the 2nd tableview
then uiview slides back out
Here's what I have
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if(!isTableOpen){
[self.fighterTableView setHidden:YES];
[self.matchTableView setHidden:NO];
isTableOpen = YES;
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
viewTableContainer.frame = CGRectMake(-352, 0, 352, 700);
[UIView commitAnimations];
[self.fighterTableView setHidden:YES];
[self.matchTableView setHidden:NO];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[UIView commitAnimations];
}
The problem here is on the commitanimations in the else statement I'm trying to set the hidden properties then pop the uiview out again. What's happening is it just hides and unhides the tableview but the animation never happens. I feel like I need to use a delay, but Idk how, unless there's a more decent way of handling this??
Thoughts?
Instead of making use of setHidden method. Why don't you try using the setAlpha method.
It will be something like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if(!isTableOpen){
[self.fighterTableView setAlpha:0.0];
[self.matchTableView setAlpha:1.0];
isTableOpen = YES;
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
viewTableContainer.frame = CGRectMake(-352, 0, 352, 700);
[UIView commitAnimations];
[self.fighterTableView setAlpha:0.0];
[self.matchTableView setAlpha:1.0];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[UIView commitAnimations];
}
I would suggest you perform
[UIView setAnimationDidStopSelector:#selector(myAnimationMethod)]
Instead of setting the alpha to 1.0 of the matchTableView set it inside the myAnimationMethod.
So something like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(myAnimationMethodDidFinish:)]
if(!isTableOpen){
[self.fighterTableView setAlpha:0.0];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
viewTableContainer.frame = CGRectMake(-352, 0, 352, 700);
[self.fighterTableView setAlpha:0.0];
[UIView commitAnimations];
}
-(void) myAnimationMethodDidFinish:(id) sender {
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if(!isTableOpen){
[self.matchTableView setAlpha:1.0];
isTableOpen = YES;
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[self.view bringSubviewToFront:viewTableContainer];
[UIView commitAnimations];
}else{
//isTableOpen = NO;
[self.matchTableView setAlpha:1.0];
viewTableContainer.frame = CGRectMake(0, 0, 352, 700);
[UIView commitAnimations];
}
}

How to animate removeFromSuperview

I animated the appearance of my subview with:
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionReveal;
[webView.layer addAnimation:transition forKey:nil];
[self.view addSubview:webView];
But now I want to remove my subView. How can I add animation to do this? Like other CATransition? When to add this? Before or after addSubview?
Well you could do the animation first and on the animationEndListener call removeFromSuperView
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
yourView.alpha = 0;
}completion:^(BOOL finished){
[yourView removeFromSuperview];
}];

how to animate a UIView same as the working of presentModalViewController ( ie [self presentModalViewController:child animated:YES];)

I am new in ios development, Now i am doing some animation on my application.In my application has one menu on the bottom of main view and have two button one for hide the menu and other for show the menu .My needs is the show and hide function of menu is working like
the [self presentModalViewController:menuView animated:YES]; and [self dismissModalViewControllerAnimated:YES]; function(ie. click the show button ,pop the menuView from the bottom of main view and click the hide button ,move down the menuView ) . I know the basic animation like:
[UIView beginAnimations:#"ShowHideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationOptionOverrideInheritedCurve];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[menuView setAlpha:0];
[UIView commitAnimations];
If any body know ,please help me.
When you tap showMenuView, do as following,
- (IBAction)showView:(id)sender
{
[self.view addSubview: menuView];
CGRect rect = menuView.frame;
rect.origin.y = 480;
menuView.frame = rect;
[UIView beginAnimations:#"ShowView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
rect.origin.y = 0;
menuView.frame = rect;
[UIView commitAnimations];
}
And to hide,
- (IBAction)hideView:(id)sender
{
CGRect rect = menuView.frame;
[UIView beginAnimations:#"HideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
rect.origin.y = 480;
menuView.frame = rect;
[UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[menuView removeFromSuperview];
}

I can't animate UIView's border color

I try to make fade in/out effect for UIView border but it does'nt work.
When try to do the following effect for background color it works perfectly.
Here is a code I developed:
[UIView animateWithDuration:3.0f
animations:^ {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.layer.borderColor = [[UIColor redColor] CGColor];
self.layer.borderWidth = 1.5f;
[UIView commitAnimations];
}
completion:^(BOOL finished){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
self.layer.borderColor = [[UIColor whiteColor] CGColor];
self.layer.borderWidth = 1.5f;
[UIView commitAnimations];
}];
According to the answer to my question you can't animate CALayer properties in a UIView block.

Core Animation question about swapping views

-(IBAction)buttonPressed1:(id)sender
{
SignView *tempVC = [[SignView alloc] initWithNibName:#"SignView" bundle:Nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationDelay:0.0f];
[UIView setAnimationDuration:0.2f];
[self presentModalViewController:tempVC animated:YES];
[tempVC passDataWithString:button1.titleLabel.text andColor:currentlySelectedColor isNightModeOn:nightMode.on];
[UIView commitAnimations];
}
can anyone help me figure out why this code doesn't work?
This method will call beginAnimations: and commitAnimations, which cannot be nested.
[self presentModalViewController:tempVC animated:YES];
So move it to before beginAnimations: or after commitAnimations.