Animation doesnt work - Swift - ios

Everytime I try to implement an animation in my viewDidLoad, it doesn't work. Any suggestions?
UIView.animate(withDuration: 2.0, delay: 0, options:
[.repeat,.autoreverse], animations: {
self.viewpost.transform = CGAffineTransform(rotationAngle: 360)
}, completion: nil)

Just move your animation to ViewDidAppear.
In ViewDidLoad the ViewController will load but is not shown yet. Because of that the animations wont show or may partially show if the animation is longer than the time for the View Controller to Appear after loading.
In ViewDidAppear the ViewController is on the screen so your animations will fully take place
Hope this helps!

Related

Animation freeze screen's other objects and scrolling. How to handle mutiple continues animation in same controller

I'm performing a rotation animation in home screen and also added card view and collection in same screen.
Now, All working fine but screen and animation freeze when i move card or scroll collection. It cause problem animation while doing a repeat rotation animation only.
Here is my code which i used for rotation animation.
Also, tried with dispatch queue but it doesn't worked for me.
UIView.animate(withDuration: 5.0, delay: 0.0, options: [.repeat , .autoreverse], animations: { self.Img_Rotate.transform=self.Img_Rotate.transform.rotated(by: .pi - 0.00001)}) { (complete) in }
Is there any best way to handle multiple animations in the same screen or app.
Thanks in advance for help.

How to ensure UITableView header cell subview with repeating animation continues animating after a reloadData, user scroll, etc?

I have a ViewController with a header cell that is working well, except that one view in the header cell has a repeating animation that should continue animating endlessly when particular conditions are set in the view controller. I can start the animation sometimes successfully in viewDidLoad and restart it in my handler for the .appDidBecomeActive notification. However when the user scrolls or pulls to refresh the tableview data, the animation stops. I did override the scrollViewDidEndDragging & scrollViewDidEndDecelerating methods to restart it, but its not actually reliably restarting the animation. Clues?
UIView.animate(withDuration: 2.0,
delay: 0.0,
options: [.curveEaseInOut, .autoreverse, .repeat, .beginFromCurrentState],
animations: { self.headerCellAnimatedSubview?.alpha = 1.0 },
completion: { _ in self.headerCellAnimatedSubview?.alpha = 0.0 }
)
After some help I found that adding the animation call to the UITableViewHeaderCell class's override of didMoveToWindow the animation will restart after scroll. However as expected I also had to respond to the appDidBecomeActive notification to restart the animation when the app returns fro background.

Swift - removeAllAnimations() - One animation after the other

So I animated a button to move to a specific point (CGPoint) on the screen when the view loads. I also added a swipeGestureRecogniser to make it that if the user swipes left, than the animation will stop and a new one will start that takes the button to a different CGPoint. I did this by calling the removeAllAnimations() method on the button. The problem is that when the user swipes left, the button finishes the first animation without animating. So it doesn't stop and start the new animation, but rather it jumps to the end of the first animation and then continues to the next animation. Is there a way to stop the first animation and seamlessly start the next?
This is the code:
It's working as expected. At the time you call removeAllAnimations(), there's only one animation to be removed - the second one will be added in first's completion block. So instead of nesting animations, you can try something like that:
// first animation
UIView.animate(withDuration: 5, delay: 0, options: .curveLinear, animations: { /... }, completion: { //... })
// second animation
UIView.animate(withDuration: 5, delay: 5, animations: { /... })

stop animateWithDuration in navigationItem

i am trying to animate my navigationItem.rightBarButtonItem to blink repeatedly from green to white... So I do this
UIView.animateWithDuration(1.0, delay:0, options: [.Repeat, .Autoreverse], animations: {
self.navigationItem.rightBarButtonItem?.tintColor = .greenColor()
}, completion: nil)
as i see in this question
and it work
the problem is that i am using UITabView and every time i switch between views and return to the first view the blink is stop
and i think its because i stuck layer after layer of animation somewhere
i understand that to remove the animation i should use layer.removeAllAnimations()
the problem is that i don't know which view to do it on
i tried self.view.layer.removeAllAnimations()
and it didn't work.
how do i get to this layer that i create for the navigationItem animation
so i can remove it?
i solve this issue with relocating the creation of the animation
in the beginning i placed that in the viewWillAppear function
and also tried to init the button to white color just before that
but its looks like it didn't work that way so i relocate the animation to
the viewDidAppear function and leave the init of the button to white color
in the viewWillAppear and its seems to work ok now :)
thanks

How to recognize tap gesture while a view is animating

Just wondering is there way to have a view recognize tap gestures while it is being animated? I am working on a view that has a cashapelayer line tethered to it. When the user pans the view (pan gesture) the line follows accordingly until the user stops panning. At this point an animation is executed that brings the view back to its original position and the tether layer back as well. Now my only real problem is that while the view and the tether are animating the view doesnt respond to tap gestures…
Anyone know some tricks? I hope my explanation was understandable and thanks in advance!
(if the tethered view concept is not clear there is a free app called discovr apps which will give an example).
I'm assuming that you are using the [UIView animateWithDuration: delay: options: animations: completion:]; method of animating.
If so, you need to pass UIViewAnimationOptionAllowUserInteraction as an option to get the animated view to respond to touches while it is animating.
(Swift 3) Pass .allowUserInteraction option
UIView.animate(withDuration: 0.75, delay: 0.0, options: [.allowUserInteraction], animations: {
// Desired animation(s)
}, completion: { (finished: Bool) in
// Completion
})
You need to set two options - UIViewAnimationOptionAllowUserInteraction and UIViewAnimationOptionAllowAnimatedContent. First lets you interact with views during animation, second forces to redraw views on every frame of animation and not use snapshots of beginning and ending frames.

Resources