Restart UIView repeat animation - ios

I've set up a a repeating animation in iOS:
[UIView animateKeyframesWithDuration:duration
delay:0.0
options:UIViewKeyframeAnimationOptionRepeat
animations:^{
...
}];
As it turns out though, when I present a view controller and later dismiss it - the animation seems to have frozen.
I've researched this and found that animations are stopped when the app goes to the background. But here it's a view controller presented on top. Could this be the same case here?
I've found the solution is to stop the animation before the app exits to the background, as shown on this article by Apple: https://developer.apple.com/library/archive/qa/qa1673/_index.html
I've tried that solution but that hasn't worked for me. Once the view controller is shown, the animation's completion block is called.
How can I resolve this animation freeze situation?

To restart animation after present another view, put it in viewWillAppear
If your animation start after a while or with condition, simply add a Bool like animationHasStarted, check for it in viewWillAppear to see if you should run the animation or not

Related

transitionFromViewController stops animations in the from view

I have animations playing in a view controller (via the UIView.animateWithDuration method). When I transition from the view controller to another using transitionFromViewController, the animations stop while the transition to the new view is taking place.
Is there a way to stop this happening?
Ive tried setting
UIViewAnimationOptions.AllowUserInteraction and UIViewAnimationOptions.AllowAnimatedContent in the options, but as soon as the transition started all animations in the current view controller stop.
Edit
Also the fromViewController seems to be being removed from the container as soon as the method is called unless you specify an animation transition, where as I was the animated it myself.

Can my UIView animation also have an interactive transition?

I have a UIView animation such as :
[UIView animateWithDuration:0.5 animations:^{
//properties to animate
}];
Is it possible to have a gesture connected to this animation so you can have an interactive transition?
For example, I have a square, when I touch it, it transitions to double it's size. However, when I pinch it, I like it to become interactively bigger or smaller depending on the pinch scale. When a user let's go and the square is 150% larger it will finish the transition else it will cancel the transition and animates back to it's previous state. Hope this makes sense.
I think percent-driven interactive transition is only enabled for view controller transitions right now. That's not to say that the technology is not there, it obviously is (see here, namely startInteractiveTransition:containerViews:animation:), but Apple has chosen not to expose this method for the time being. Right now, it is called only when performing view controller transitions (push/pop and presentation).
There is an update since iOS 10, everything is now possible with UIViewPropertyAnimator: you can make the animation interactive or automatic, or even change the parameters on the fly.

Animating container view controllers

I have 2 container view controllers on my main screen, one which acts as a global toolbar at the top. This is great most of the time, but I need to animate this off screen, to give more room for the user to see lots of information on the screen. Is this possible? I've been looking at the documentation, but I'm not sure if I need to use the transformation animation, or change the frame/bounds. Any suggestions would be grateful.
You should just be able to change the frame/bounds in a standard UIView animation (block or otherwise). No need to reference the "contained" views.
Update: Here's a block animation example.
[UIView animateWithDuration:1.0f
animations:^{
view1.frame = CGRectMake(blah...);
}
completion:^(BOOL finished){
// do something here if you wish.
}];

iOS Restart animation after switching tab

In this post the author said that we could restart animation after switching tab in viewWillAppear.
I called my startAnimation in both viewWillAppear and viewDidAppear, but still failed.
Below is part of my code.
- (void)startAnimation {
[UIView setAnimationsEnabled:YES];
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat
animations:^(void){
self.foreground.transform = CGAffineTransformMakeTranslation(0.0f, 5.0f);
}
completion:nil];
}
The animation works perfectly when first shown by calling startAnimation in viewDidLoad, but it never works after switching to other tabs.
Neither does it work after the application restarts from the background, even if I've registered an observer of UIApplicationWillEnterForegroundNotification for startAnimation.
Please help me, I'm new to iOS development, thanks very much.
The method viewDidLoad only gets called when your view controller is first loaded.
If you want your animation to run when you return from other view controllers to this one, you should call the animation method in viewWillAppear, not viewDidLoad.
To make the animation go back to it's previous value and then animate again, you would first set your view's transform to identity (setting it to the starting point) and then invoke your animationWithDuration method.
You might find it easier to animate your view's frame.origin or center property rather than changing the transform. Changing the transform gets complicated when you combine translations with rotations, scale changes, etc. Also, the frame property is no longer valid when the transform property is not == the identity transform.

View animations are suddenly gone after using layer animations

I'm using CABasicAnimation and CATransaction to animate some layers in my custom UIView.
However, when after that returning to the rest of my app using a navigation controller's back button, the navigation controller does not animate anymore. Not even when then going to any other view.
I am using an iPad with iOS5.
I was able to workaround this by calling
[UIView setAnimationsEnabled:YES];
in my viewDidDissapear and viewWillDisappear methods.
Really strange since I am never disabling view animations anywhere in my code.

Resources