1

I would like a smooth animation of this view whenever the search bar is selected and deselected. Right now it's choppy:

enter image description here

Heres my code below in the searchResultsUpdater. From what I understand, these functions should handle the animations, I'm not sure what's wrong here:

func updateSearchResults(for searchController: UISearchController) {
    
    //MapView moves up when search bar is selected
    if searchController.isActive == true{
        UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
            self.mapView.frame.origin.y=self.view.safeAreaLayoutGuide.layoutFrame.origin.y

        },completion: nil)
    }
    
    //MapView moves down when cancel button is selected
    if searchController.isActive == false{
        UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
            self.mapView.frame.origin.y=self.view.safeAreaLayoutGuide.layoutFrame.origin.y
            
        },completion: nil)
    }
}

Any help is appreciated, thank you!

jmsapps
  • 388
  • 2
  • 17
  • Hi jmsapps, Please refer the [link](https://developer.apple.com/documentation/uikit/view_controllers/displaying_searchable_content_by_using_a_search_controller) for smooth animation – Abishek Thangaraj Feb 01 '22 at 06:11
  • I'm sorry I don't see where that refers to the smooth animation. Can you elaborate? – jmsapps Feb 01 '22 at 06:52
  • https://developer.apple.com/documentation/uikit/view_controllers/displaying_searchable_content_by_using_a_search_controller are you refer this link? Not means please refer – Abishek Thangaraj Feb 01 '22 at 06:55
  • Yes I read the document, I don’t see where it refers to the solution. – jmsapps Feb 01 '22 at 10:20

1 Answers1

0

I found the solution. Originally I had the CGRect for the mapView frame in viewDidLayoutSubviews():

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
     //layout to fit frame of view
    mapView.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: 
    view.frame.size.width, height: view.frame.size.height - 
    view.safeAreaInsets.top)
  
}

I moved it to viewDidLoad(), and now it works:

override func viewDidLoad() {
    super.viewDidLoad()
    
    //add gesture recognizer for mapView
    mapView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(mapViewAnimation)))
    //layout to fit frame of view
    mapView.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: 
    view.frame.size.width, height: view.frame.size.height - 
    view.safeAreaInsets.top)

}

I also added a gesture recognizer instead so that the searchResultsUpdater wouldn't be so cluttered:

//handle the animation for mapview
@objc func mapViewAnimation(){
    UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: 
    UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
        
    self.mapView.frame.origin.y = 
    self.view.safeAreaLayoutGuide.layoutFrame.origin.y
        
    },completion: nil)
    
}

enter image description here

If anyone knows why it didn't work when I had the CGRect in didLayoutSubViews() feel free to let me know. Thanks!

jmsapps
  • 388
  • 2
  • 17