Swift animation not resetting when navigating back to that view - swift

I have two views and when you go from view 1 to view 2 through tapping of a button the buttons all animate off screen. If you leave the 2nd page and go back to the first page the elements are all still off screen. Is there a way to have those elements get put back to their original positions in the background after the transition?
This is an example of one of the animations
func loggedInAnimate5(){
UIView.animateWithDuration(3,
delay: 1.2,
options: .CurveEaseIn,
animations: {
self.logo.center.y += self.view.bounds.height
self.logo.alpha = 0.0
}, completion: nil
)
}

In viewDidDisappear just set the position and alpha value back to where they were initially.
Example:
override func viewDidDisappear(animated: Bool) {
self.logo.center.y = 100.0
self.logo.alpha = 1.0
}

Related

Showing ViewController's View when animating upper ViewController

I have ViewController(1) presented over another ViewController(2), I want to accomplish the effect that when I dismiss viewController1 I perform a reduction of size through CGAffineTransform and I dismiss it.
The effect I want to accomplish though is that when I reduce the size of ViewController1's view I want to see also the view of ViewController2 behind it, while now I'm only seeing a black background.
The code I'm using is really simple:
UIView.animate(withDuration: 2, animations: {
self.view.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}) { (_) in
self.dismiss(animated: true, completion: nil)
}
I don't know how to reach that effect
Make sure to set this presentation for the top animated vc
vc.modalPresentationStyle = .overCurrentContext
This will guarantee transparency behind when you change frame / transform the view of that top vc

Center UIView in container without jumping

I have this structure:
container (UIView)
Header (UIView)
TableView
CollectionView
CollectionView
CollectionView
When a favorite button is pressed I want a view to be displayed on top of everything and to be centered. Whenever it is added when the tableView has not been scrolled, it works fine. However, when added after it has been scrolled the whole layout kind of jumps into place. The more scroll, the more jumping. Visual representation of the layout:
https://gyazo.com/8ddc75ce1ac96ff00aa9a9d8b0d4b725
The view is loaded from a xib and centered in its container. The view height and width anchors are already set (and translatesAutoresizingMaskIntoConstraints to false). Here is the configure method of the view to be centered.
public func configure(viewToCenterItselfInside: UIView) {
self.centerXAnchor.constraint(equalTo: viewToCenterItselfInside.centerXAnchor).isActive = true
self.centerYAnchor.constraint(equalTo: viewToCenterItselfInside.centerYAnchor).isActive = true
UIView.animate(withDuration: 0.5) {
self.alpha = 1
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1500)) {
UIView.animate(withDuration: 0.5, animations: {
self.alpha = 0
}) { (_) in
self.removeFromSuperview()
}
}
}
Here is the code when the view is loaded
if let addedFavorite = Bundle.main.loadNibNamed("ConfirmationView", owner: nil, options: nil)?.first as? ConfirmationView {
view.addSubview(addedFavorite)
addedFavorite.configure(viewToCenterItselfInside: view)
}
In addition to trying to solve it with constraints I've also tried centering it using self.center = viewToCenterItselfInside and messing around with the UIScreen.main without any results. Any ideas?

After animation and keyboard close the animated image moves

I have a problem after animated a logo on the main login screen. The screen looks correct when visibly seeing the screen. Once entering the email address field and typing into the field (or closing the keyboard), the logo will go back to it's original location.
On viewDidAppear, I animate the logo.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animateScreen()
}
private func animateScreen() {
mainLogo.fadeIn()
UIView.animate(withDuration: 1.2, animations: {
self.mainLogo.frame.origin.y -= 150
}) { (isCompleted) in
self.showFields()
}
}
I am using a storyboard with centerX and top constraints on the image initially centered on the login screens.
If there's a better solution, I'll gladly look at alternatives.

Transition to a new ViewController embedded in an UINavigationController causes animation problem

I use a rootViewController and I want to move to another ViewController. The transition to the newViewController works with that code.
A problem occurs when the newViewController is embedded in an UINavigationController. Then the navigation bar is animating during the animation and changes the position.
The navigation bar is animating from the top left to the correct position.
fileprivate func animateTransition(to newViewController: UIViewController) {
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
transition(from: currentViewController, to: newViewController, duration: 2, options: [.transitionCrossDissolve, .curveEaseOut], animations: {
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
}, completion: nil)
}
How is it possible to move to another UINavigationController with a "fade" animation and how can the navigation bar be at the correct position right from the beginning of the animation?
First, you should move your view controller clean-up code call from the animations closure into the completion closure:
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
transition(from: currentViewController, to: newViewController, duration: 2, options: [.transitionCrossDissolve, .curveEaseOut], animations: {
// this is intentionally blank
}, completion: { _ in
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
})
You don't want to indicate the transition is done until the animation is complete.
To the navigation bar issue, rather than letting transition(from:to:duration:...) handle the manipulation of the view controller hierarchy, you can add it to your view hierarchy and then animate the unhiding of it. Usually you'd use the .showHideTransitionViews option, but transition is still doing something curious with the appearance methods that is confusing the navigation controller, so it's best to just animate it yourself:
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
newViewController.view.alpha = 0
view.addSubview(newViewController.view)
UIView.animate(withDuration: 2, delay: 0, options: .curveEaseOut, animations: {
newViewController.view.alpha = 1
}, completion: { _ in
self.currentViewController.view.removeFromSuperview()
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
})
That will allow it to present the navigation bar correctly from the beginning and just fade it in.
try putting this underneath the class declaration of the newViewController
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
if you already have a viewDidLoad() in your ViewController, then just use the last part.
If that doesn't work let me know.

How to open another view on animation completition in swift?

I am trying to design animated splash screen.
I created the the splash screen and added a segue to my first screen, but I am not sure how I can start the first screen when the animation is finished.
UIView.animate(withDuration: 0.5, animations: {
// your animation
}) { (completed) in
let yourController = YourController()
self.navigationController?.pushViewController(yourController, animated: true)
}