Stop UIView animation, without advancing to the end - ios

Is there anyway to stop a UIView animation, but not jump to the end values?
I'd like to continue from where I am, instead of having a jerky looking 'snap' before it starts moving to it's new position

Here's the answer for other people who stumble here. Quite simple really, just pass 'options:UIViewAnimationOptionBeginFromCurrentState' into the options, and it will resume from it's current locaiton
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.scrollView.contentOffset = CGPointMake(visibleFrame.origin.x, visibleFrame.origin.y);
}
completion:^(BOOL finished){}
];

Related

Flick a UIScrollView programmatically

When a user makes a flick gesture on a UIScrollView, the UIScrollView gets a momentum and starts moving, then slow down and finally stop.
But how can I make this happen programmatically? I mean without a finger flicking, the UIScrollView just start moving automatically and then slow down to a speed of 0.
In my app I have made my UIScrollView unlike a normal UIScrollView (say it looks like a roller), so I want make a hint to the user that he can scroll it (and then everything get started!)
I have googled a lot but there seemed no way to solve my problem. The setContentOffset just couldn't make the natural "slow down and stop at somewhere ahead" effect.
Any idea would be appreciated.
Thanks in advance.
Try, something like this >
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:.8];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:(abs(1-3)*0.3)];
self.myScroll.contentOffset = CGPointMake(0, 500);
[UIView commitAnimations];
It is not currently what you need, but you can customise this code, and may be all be ok)
or use this code>
[UIView animateWithDuration:2.
delay:0.3
usingSpringWithDamping:1.
initialSpringVelocity:7.
options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Animations
self.myScroll.contentOffset = CGPointMake(0, 500);
}
completion:^(BOOL finished) {
//Completion Block
}];
I think it is like you want(animation with damping like swipe effect)

Move UIView with animation and it rolls back by itself

I have a UIView drawn in Storyboard which holds some buttons and which is called viewHolder.
I get a higher position rectForAnimationBefore
and a lower position rectForAnimationAfter
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:nil completion:^(BOOL finish){
[self.viewHolder setFrame:rectForAnimationAfter];
}];
when this is excuted,the viewHolder do move down.After a second,it comes up as nothing was done.
I want to moveDown,but don't want moveUp Automatically.
Because autolayout in Storyboard?
By the way ,how to move it smoothly?
Thank you guys.
UPDATE:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.viewHolder setFrame:rectForAnimationAfter];
}
completion:^(BOOL finish){
}];
when I change code like this ,I can't move it down .
Here is my solution
unlock the autolayout!!!
First thing your animation block is nil. Here you should move your view down. At completion you are setting back you view without using any animation. For smoothness you need to move back your view animatedly.

Animation slows down as it ends

I have a translate animation, moving my button from A to B.
That route looks like this:
1) Button slowly accelerates
2) At the middle of animation gets to the peak of it's speed
3) Slows down as it approaches the end
This is my code
[UIView animateWithDuration:speed
delay:delay
options:UIViewAnimationOptionAllowUserInteraction
animations:^(void){
[UIView setAnimationRepeatCount:5];
cloud.frame = (CGRectMake(cloud.frame.origin.x,
cloud.frame.origin.y+900, cloud.frame.size.width, cloud.frame.size.height));
}completion:nil];
I want my animation to have a same speed all the time.
How can I achieve this?
Add the UIViewAnimationOptionCurveLinear to your animation options, like this:
[UIView animateWithDuration:speed
delay:delay
options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveLinear
animations:^(void){
[UIView setAnimationRepeatCount:5];
cloud.frame = (CGRectMake(cloud.frame.origin.x,
cloud.frame.origin.y+900, cloud.frame.size.width, cloud.frame.size.height));
}completion:nil];
I think you wanna try setting the "option" parameter to:
UIViewAnimationOptionCurveLinear

Animating two views with different start times and same finish time

I am trying to find a way to animate two views in this way: they will start animating at different times but end their animations at the same time.
Think of a horse race where one horse starts after the other, but they both reach the finish line at the same time.
Its not a grouped animation. Also, how to I tell an animation to wait until a certain time to start?
Does anyone have any pointers on how to do this?
TIA
Use [UIView animateWithDuration:delay:options:animations:completion:]
Example:
[UIView animateWithDuration:1.0f animations:^{
// first animation code here
}];
[UIView animateWithDuration:0.5f delay:0.5f options:UIViewAnimationCurveEaseIn animations:^{
// second animation code here
} completion:^(BOOL finished) {
}];

iOS - animation not repeating

I've made something like:
[UIView animateWithDuration:2.0
delay:0
options:UIViewAnimationOptionRepeat
animations:^{
NSLog(#"Repeating...");
/** code **/
}
completion:^(BOOL finished) {
/** code **/
}];
But the animation does not repeat itself.
Am I missing something?
I've removed the color animation, but the animation doesn't repeat yet. But I must be doing something really wrong, 'cause I tried the following simple code and the animation still doesn't repeat:
[UIView animateWithDuration:1.0f
delay:0
options: UIViewAnimationOptionRepeat
animations:^{
NSLog(#"Repeating...");
self.alpha = 1;
}
completion:nil];
Depending on the animation (still would like to see your code) you might have to reverse your animation in order for it to repeat. For example, if your animation changes an alpha value from 0 to 1, repeating that, without reversing the alpha back to 0, isn't going to do much. You may need to add UIViewAnimationOptionAutoreverse to your animation options.

Resources