I have some nested UIView animations like so:
[UIView animateWithDuration:1.0 animations:^{
//Animation
}completion:^(BOOL success){
[UIView animateWithDuration:1.0 animations:^{
//More animation
}completion:^(BOOL success){
}];
}];
The inside animation occasionally has a delay before starting when there's other activity on the thread.
I was wondering if there was a way around this, and if GCD can be used here to run this on a background thread? It's UI code, so perhaps not.
If it's not too obvious an answer, a way around your problem would be:
[UIView
animateWithDuration:1.0
animations:^{
//Animation
}];
[UIView
animateWithDuration:1.0
delay:1.0
options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionTransitionNone
animations:^{
//More animation
}
completion:^(BOOL success) {}];
i.e. just schedule the second animation at the same time as you schedule the first, but tell it not to start until you know the first will be finished.
Once things are scheduled, Core Animation isn't blocked by main queue activity so you can be busy a second later and the second thing will still start immediately after the first has finished.
Related
Sometimes when I run my app on the device, I guess about 20% of the time, animations such as:
[tableView setEditing:NO animated:YES];
[tableView setContentOffset:rect animated:YES];
and
[viewController0 pushViewController:viewController1 animated:YES];
do not animate but instantly change. But animations such as:
[UIView animateWithDuration:0.2
animations:^{
// do something
}
completion:^(BOOL finished) {
// do something else
}];
Work fine. Has this happened to anybody else? Not sure what could be causing it, it only happens on this one app, never happened on any other app and it only happens sometimes. Any help appreciated.
Edit:
Just want to clarify.
Animations that i create with 'animateWithDuration' work fine.
Sometimes animations from cocoa don't play for the entire time the app is running.
Try to set the UIView animation parameters before you push your viewcontroller.
Here's an example:
[UIView beginAnimation:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:viewController1.view cache:NO];
[viewController0 pushViewController:viewController1 animated:YES];
[UIView commitAnimations];
Maybe it's because of the boolean u used. My animation is quite the same but it's working fine:
[UIView animateWithDuration:4.0 animations:^(void) {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
pfeil.alpha = 1.00;
}];
thats my code...any questions?
Maybe you have a lot of data to load in your VC? if you load your data in main thread it stops all user interactions and UI responses for a while.
I've searched loads of SO stuff and in Apple's references, but still unable to manage my problem.
What I have:
A screen with 2 UIImageViews and 2 UIButtons connected to them
2 kinds of animation:
Scaling up and then down of each image, one after another, only once in viewDidLoad
When a button pressed (a custom button hidden 'inside' of each UIImageView) it triggers animation of appropriate UIImageView–only one, not both–(also scale up, then down).
As I am writing for iOS4+ I'm told to use block based animations!
What I need:
How do I cancel a running animation? I've managed to cancel after all but the last one... :/
Here is my code snippet:
[UIImageView animateWithDuration:2.0
delay:0.1
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
isAnimating = YES;
self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 2.0, 2.0);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 0.5, 0.5);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 2.0, 2.0);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 0.5, 0.5);
}
completion:^(BOOL finished){
if (!finished) return;
//block letter buttons
[self.bigLetterButton setUserInteractionEnabled:YES];
[self.smallLetterButton setUserInteractionEnabled:YES];
//NSLog(#"vieDidLoad animations finished");
}];
}];
}];
}];
Somehow the smallLetter UIImageView is not working properly, because when pressed (through button) bigLetter is canceling animations properly...
EDIT:
I've used this solution, but still having problem with scaling down smallLetter UIImageView - not cancelling at all...
solution
EDIT2: I've added this at the beginning of next/prev methods:
- (void)stopAnimation:(UIImageView*)source {
[UIView animateWithDuration:0.01
delay:0.0
options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)
animations:^ {
source.transform = CGAffineTransformIdentity;
}
completion:NULL
];
}
problem stays... :/ no idea how to interrupt last animation for letters in animation chain
You can stop all animations on a view by calling:
[view.layer removeAllAnimations];
(You'll need to import the QuartzCore framework to call methods on view.layer).
If you want to stop a specific animation, not all animations, your best best bet is to use CAAnimations explicitly rather than the UIView animation helper methods, then you will have more granular control and can stop animations explicitly by name.
The Apple Core Animation documentation can be found here:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html
For iOS 10 use UIViewPropertyAnimator to animate.
It provides methods to start, stop and pause UIView animations.
let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeOut){
self.view.alpha = 0.0
}
// Call this to start animation.
animator.startAnimation()
// Call this to stop animation.
animator.stopAnimation(true)
I'd add to Nick's answer that to make removeAllAnimations smooth next idea be very handy.
[view.layer removeAllAnimations];
[UIView transitionWithView:self.redView
duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[view.layer displayIfNeeded];
} completion:nil];
You can try this (in Swift):
UIView.setAnimationsEnabled(false)
UIView.setAnimationsEnabled(true)
Note: you can put code between those two calls if necessary, for example:
UIView.setAnimationsEnabled(false)
aview.layer.removeAllAnimations() // remove layer based animations e.g. aview.layer.opacity
UIView.setAnimationsEnabled(true)
I have a UIView nested in a UIView. When I animate the frame and alpha of the inner view, touches in the outer view get canceled. Can I stop that from happening?
You can use block based animation methods.
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
| UIViewAnimationOptionCurveEaseInOut
animations:^{
//your animation code
} completion:^(BOOL finished){
//your code when animation finished..
}];
I'm keen on the new block animations in iOS 4. That is, the syntax
[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^
{
someview.alpha = 0
} completion::^(BOOL finished)
{
focusAndExposureBox.hidden = true;
}];
I have a case where I am using gesture recognizers and animating a view at the end of a gesture. I have a completion handler which sets someview to hidden (for performance reasons I need to do this). Often interaction is blocked due to the hidden property being set. My previous solution is to use the previous style of animation with an animationDidStop handler
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.25];
someview.alpha = 0;
[UIView commitAnimations];
However, I'd like to use the newer style if possible, because it has a cleaner method of performing actions after the animation finishes, and is recommended by Apple. So, currently I am using this hackery:
[UIView animateWithDuration:1 delay:0
options:UIViewAnimationOptionAllowUserInteraction animations:^{
someview.alpha = 0;
} completion:^(BOOL finished) {
[someview performSelector:#selector(setHidden:) withObject:[NSNumber numberWithBool:true] afterDelay:1];
}];
Does anyone know of a way to prevent blocking in this case?
You should be able to avoid the Blocking issue if you're using Gesture Recognizers with delegates, as the delegate function will be invoked independent of the main thread.
I have the following code:
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
imageView.bounds = endBounds;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
imageView.bounds = startBounds;
}
completion:^(BOOL finished) {
[imageView removeFromSuperview];
}];
}];
Additionally I have:
[imageView setUserInteractionEnabled:YES];
and a tap gesture recognizer set that will handle the user tapping on imageView. While the first animation is happening, the gesture recognizer fires as I would expect. But if I try and tap imageView during the chained animation from the completion block, nothing happens even though I have set the appropriate option.
Anyone have any thoughts? I've googled and can't find an answer.
When using the new animation blocks, if you want user interaction to be enabled during the animation, you have to set it in the options mask. For example:
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{ myView.alpha = 0.5; }
completion:NULL];
I came up with a solution:
I wrap the UIImageView in a UIView (I subclass UIView) with the same bounds/center point as the image. Then I attach the gesture recognizer to the wrapper, instead of the image. Because the wrapper's bounds rectangle/center point never change for the duration of the animation, it's always available as the target of a gesture.
This works quite well.
-j
Do you see the same behaviour if you use:
+ [UIView setAnimationDidStopSelector:]
instead of using blocks?