Me gustaría una animación fluida de esta vista cada vez que se selecciona y se deselecciona la barra de búsqueda. Ahora mismo está entrecortado:
Aquí está mi código a continuación en searchResultsUpdater. Por lo que entiendo, estas funciones deberían manejar las animaciones, no estoy seguro de qué está mal aquí:
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) } }¡Cualquier ayuda se agradece, gracias!
Encontré la solución. Originalmente tenía el CGRect para el marco mapView en 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) }Lo moví a viewDidLoad(), y ahora funciona:
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) }También agregué un reconocedor de gestos para que el searchResultsUpdater no estuviera tan abarrotado:
//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) }Si alguien sabe por qué no funcionó cuando tenía el CGRect en didLayoutSubViews() no dude en hacérmelo saber. ¡Gracias!