Animation is happening in reverse - ios

I'm making a simple animation where when a button is tapped it will change from current position to a certain point. But what's happening is the opposite: it goes from the destination point to the original position. Here's my code:
[UIView animateWithDuration:1.0
delay:0.0
usingSpringWithDamping:0.2
initialSpringVelocity:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self.view layoutIfNeeded];
CGFloat amount = 100;
self.checkButton.frame = CGRectOffset(self.checkButton.frame, amount, 0);
} completion:^(BOOL finished) {
}];

Make an outlet to the constraint that determine the horizontal position of the checkButton. e.g."hPos".
- (IBAction)tapHandler:(id)sender {
self.hPos.constant = self.hPos.constant + 100;
[UIView animateWithDuration:1.0
delay:0.0
usingSpringWithDamping:0.2
initialSpringVelocity:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{ [self.view layoutIfNeeded]; }
completion:nil];
}

Related

When UIView height set zero, view's child control not hide

I want drawer animation. So I take 1 UIView and add 4 control inside UIView.
Now when I click on drawer button, set view height zero when drawer is close and set view height 200 when drawer is open.
But when I set zero height, button is not hide . All buttons are visible.
Auto layout is not in my project.
How to solved this problem.?
-(IBAction)Onclick_drawer:(id)sender
{
if(is_open)
{
is_open=false;
[UIView animateWithDuration:0.3
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:4.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.drawer_view.frame=CGRectMake(0, 64, 320,200);
}
completion:^(BOOL finished){
}];
[UIView commitAnimations];
}
else
{
is_open=true;
[UIView animateWithDuration:0.3
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:4.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.drawer_view.frame=CGRectMake(0, 64, 320, 0);
}
completion:^(BOOL finished){
}];
[UIView commitAnimations];
}
}
check in xib ... select view -> attribute inspector -> check clip Subviews like below image
or programatically use
self.yourview.clipsToBounds = YES;
Just select the view and go to right side of screen into attribute inspector
Check the check box for Clip Subviews
-(IBAction)Onclick_drawer:(id)sender
{
if(is_open)
{
is_open=false;
[UIView animateWithDuration:0.3
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:4.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.drawer_view.frame=CGRectMake(0, 64, 320,200);
}
completion:^(BOOL finished){
}];
[UIView commitAnimations];
}
else
{
is_open=true;
[UIView animateWithDuration:0.3
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:4.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.drawer_view.frame=CGRectMake(0, 64, 320, 0);
self.drawer_view.clipsToBounds = YES; // Need to add this line. This will clip all sub views into parent view
}
completion:^(BOOL finished){
}];
[UIView commitAnimations];
}
}

Show and hide UITableView with animation

I'm trying to show and hide a tableView with an animation "transition from left to right & right to left"
This what i already tired it worked but after using a random values in the CGRectMake,
Do anyone can help me to fix it please
-(void)Display:(UITableView *)tableView :(UIView *)view{
tableView.alpha=1;
[UIView animateWithDuration:0.35
animations:^{
tableView.frame = CGRectMake(-209, 440, 180, 209);
}
completion:^(BOOL finished){
[tableView setHidden:YES];
}
];
}
-(void)Hide:(UITableView *)tableView :(UIView *)view :(int )tb{
tableView.alpha=1;
[tableView setHidden:NO];
[UIView animateWithDuration:.45
animations:^{
tableView.frame = CGRectMake(3, 442, 180, 209);
}
];
}
You can use this code:
[UIView transitionFromView:firstView
toView:secondView//table view
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished) {
[firstView.superView addSubview:secondView];
[firstView.superView sendSubviewToBack:firstView];
}];
It's hard to tell from your question but by "transition from left to right & right to left"
do you want something like this...?
For animating out left
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
animations:^{
self.tableView.frame = CGRectMake(0.0f, -self.tableView.frame.size.width, self.tableView.frame.size.width, self.tableView.frame.size.height);
}
completion:NULL];
For animating out right
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
animations:^{
self.tableView.frame = CGRectMake(0.0f, self.view.bounds.size.width, self.tableView.frame.size.width, self.tableView.frame.size.height);
}
completion:NULL];
For animating in
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
animations:^{
self.tableView.frame = CGRectMake(0.0f, 0.0f, self.tableView.frame.size.width, self.tableView.frame.size.height);
}
completion:NULL];
You can put table reloads etc. in your completion blocks? and then animate the tableView back in?
If you want animation like Facebook then use :- ECSlidingViewController
http://kingscocoa.com/tutorials/slide-out-navigation/
Or
If you want normal animation then set frame and reduce alpha from 1.0 to 0.0 with duration.

Different animation duration for subviews of an UIView

I have the following code
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
mainPage.frame=CGRectMake(0, -[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width);
} completion:nil];
Is there any way to control the animation duration of the subviews of mainPage.
You can use UIView:animateKeyframesWithDuration then use UIView:addKeyframeWithRelativeStartTime to set its start time and duration
[UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.32 relativeDuration:0.68 animations:^{
view.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:1 animations:^{
subview.frame = frame2;
}];
} completion:^(BOOL finished) {
}];
If you want the subviews to animate with a different duration then you'll need to animate each of them separately. You can loop through the mainPage.subviews array and issue an animateWithDuration:delay:options:animations:completion: call to each subview in turn, each with a different duration.

Lag between 2 animations

I am trying to animate one of my image form bottom to Top of my screen , for that i am implementing an animation that the current width and height of image will become half during the transition stage and will take its original size once animation completes.
The issue is once the 1st animation is done it hold's for a while and then starts another animation , i want a smooth animation .
Here is the code :
[UIView animateWithDuration:1.0
delay:0.5
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[_myImageView setFrame:CGRectMake(20.0, 220.0, currentframe.size.width/2, currentframe.size.height/2)];
} completion:^(BOOL finished)
{
if (finished)
{
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[_myImageView setFrame:CGRectMake(20.0, 20.0, currentframe.size.width, currentframe.size.height)];
} completion:^(BOOL finished) {
if (finished) {
NSLog(#"2 nd log ");
}
}];
NSLog(#"animation fisnished !!");
}
}];
What should be done ?
try this:
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionCurveLinear// use CurveLinear option
animations:^{
} completion:^(BOOL finished) {
}];

How do I have text fade in to a UILabel?

Right now I have an IBAction for a button which generates a random number with each number assigned to display text in a UILabel. Rather than just appear, I would like the text to fade in or any other interesting animation would be cool too. Does anyone know a simple way to make this happen?
Right now I just use
labelMain.text = #"my text";
I know this is a bit stale (11 months old), but I think it's worth mentioning an alternative to the other approaches posted, which I feel is a better solution.
Make use of the UIView transitionWithView:duration:options:animations:completion class-level method, like this:
NSTimeInterval duration = 0.5f;
[UIView transitionWithView:labelMain
duration:duration
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
labelMain.text = #"new value";
} completion:nil];
This will cross-fade from the previous value of labelMain.text to "new value".
This approach works on other view values as well, such as changing the image of a UIImageView, for instance.
See Apple's docs on the topic.
The following code will give you fading effect on your label.
- (IBAction)buttonClicked:(UIButton *)sender
{
labelMain.alpha = 0;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^{ labelMain.alpha = 1;}
completion:nil];
}
label.text = #"old text";
[UIView animateWithDuration:0.4 animations:^{
label.alpha = 0.0f;
} completion:^(BOOL finished) {
label.text = #"next text";
[UIView animateWithDuration:0.4 animations:^{
label.alpha = 1.0f;
} completion:^(BOOL finished) {
NSLog(#"finished transition");
}];
}];
Try this:
[labelMain setAlpha:0];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[labelMain setAlpha:1];
[UIView commitAnimations];
Changing the duration is pretty simple - just adjust the value 0.8 in the code.
// fade out the current value
[UIView animateWithDuration:0.2
delay:0
options:0
animations:^{
labelMain.alpha = 0.0f;
}
completion:^(BOOL finished) {
// set the new value and fade in
labelMain.text = newValue;
[UIView animateWithDuration:0.2
delay:0
options:0
animations:^{
labelMain.alpha = 1.0f;
}
completion:nil];
}];
This will look like cross fade animation
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut
animations:^{
labelMain.alpha = 0;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^{ labelMain.alpha = 1;}
completion:nil];
} completion:nil];

Resources