After animation and keyboard close the animated image moves - swift

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.

Related

How can I always show the UIScrollBar indicator?

When a UIViewController with a UIScrollView is visible on screen, I want to show the UIScrollView scrollbar indicator.
Is there a way to show the indicator?
You can show the scrollbar momentarily with .flashScrollIndicators when the view controller has been added to the view hierarchy and is potentially visible to the user.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scrollView.flashScrollIndicators()
}

How do you animate a UICollectionView using auto layout anchors?

I'm trying to animate a collectionview by using layout constraints however I cannot get it to work. I have a collectionview that takes up the entire screen and on a button tap I want to essentially move the collectionview up to make room for another view to come in from the bottom - see image below
The incoming UIView animates just fine (the view coming up from the bottom) - The reason I want to move the collectionview is that the incoming UIView obscures the collection view so am just trying to move the collectionview up at the same time as the new view so that all of the content in the collectionview can be displayed without being hidden by the new view - I use a reference view to get the right layout constraints for the final position for the collectionview Image to show what I am trying to achieve Am I going about it the right way?
Nothing happens with the code example below and I am not sure where to go from here - the same approach is used for animating the incoming view and works just fine but doesn't seem to work for the collectionview...
Any help would be kindly appreciated
var colViewBottomToReferenceTop: NSLayoutConstraint?
var colViewBottomToViewBottom: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
colViewBottomToReferenceTop = musicCollectionView.bottomAnchor.constraint(equalTo: referenceView.topAnchor)
colViewBottomToViewBottom = musicCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
NSLayoutConstraint.active([colViewBottomToViewBottom!])
NSLayoutConstraint.deactivate([colViewBottomToReferenceTop!])
}
func playerShow() {
NSLayoutConstraint.activate([colViewBottomToReferenceTop!])
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
#IBAction func btnTapped(_ sender: UIButton) {
playerShow()
}
You want to deactivate the other before animation
NSLayoutConstraint.deactivate([colViewBottomToViewBottom!])
NSLayoutConstraint.activate([colViewBottomToReferenceTop!])
Look to this Demo

When I go back to a view controller after opening it previously it does not animate labels Xcode Swift

When I open my app I have this label that animates in from outside the screen. When I open the view controller for the first time it works. However, when I go to another view controller and then go back to the initial view controller this label will just be there and not animate in.
levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)
UIView.animate(withDuration: 2) {
self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)
}
Anyone have any suggestions?? Thank You!
Putting your block of code in viewWillAppear rather than viewDidLoad will make it work. However, according to Apple Doc.
viewDidAppear: Use this method to trigger any operations that need
to occur as soon as the view is presented onscreen, such as fetching
data or showing an animation.
The following is what I'd recommend you to do.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 2) {
self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)
}
}

Getting an object to reset to a fixed (x,y) coordinate when a button is pressed in Swift

I have a box that fills the screen, and a button. When I press the button, I want the box to move down slowly until it is no longer visible. If the button is pressed during this animation, I want the button to return to its original location, and resume the animation. I have the following code:
#IBOutlet weak var backgroundthing: UIView!
#IBAction func button(_ sender: UIButton) {
UIView.animate(withDuration: 0.0, animations: {self.backgroundthing.frame.origin.y = 0}, completion: nil)
UIView.animate(withDuration: 4.0, animations: {self.backgroundthing.frame.origin.y = 667}, completion: nil)
}
If I press the button once, it works "fine", and the box takes the 4 seconds to get off screen. However, if I press the button before the animation is over, the box snaps to a higher position than its original position. For example, if I pressed the button while the box is only half off screen, it would snap to the top, with half of the box above the screen to resume sliding down. The code I am looking for would snap the box to its ORIGINAL position, so no matter how fast or how many times I press the button, it won't go any higher than its original position.
Stop the animation first, reset the block, start a new animation:
#IBAction func button(_ sender: UIButton) {
backgroundthing.layer.removeAllAnimations()
backgroundthing.frame.origin.y = 0
UIView.animate(withDuration: 4.0) {
self.backgroundthing.frame.origin.y = 667
}
}

Swift animation not resetting when navigating back to that view

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
}