Trev14
Trev14

Reputation: 4116

Smooth Search Bar Animations in iOS with Swift

How do you achieve a smooth search bar animation like in the iOS 10 settings app? I've tried different methods but all seem to be jumpy and jittery, especially when editing ends or the cancel button is pressed. Is there some code that I'm missing or is my code doing something wrong? I suspect the code below has something to do with the animation jumping frames:

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    self.view.endEditing(true)
    searchBar.setShowsCancelButton(false, animated: true)
}

Upvotes: 0

Views: 1797

Answers (1)

Sethmr
Sethmr

Reputation: 3074

A few things here.

  1. During all animations, they are more likely to look jumpy if you dont have the highest parent view calling view.layoutIfNeeded() inside of the animation.

  2. If the search bar is nested inside of any type of scroll view (this includes tableViews/collectionViews) then if probably is conforming to the delaysContentTouches setting. If this is the case, turn this to false inside of the scrollView and see if this helps anything. It could also make the scrolling less simple to understand, so it doesn't always make things better.

  3. Animations don't ever occur at the exact moment that they are told to happen. They are put in a queue and then every so often all animations in the queue are triggered. This normally appears to happen at the same moment, but it can differ enough to make things different than you would expect.

There is lots of good information here on basic iOS stuff that you might find useful, and I am a huge fan of Paul Hegarty's Standford iOS Swift course.

Upvotes: 1

Related Questions