简体   繁体   English

使用Swift在iOS中平滑搜索栏动画

[英]Smooth Search Bar Animations in iOS with Swift

How do you achieve a smooth search bar animation like in the iOS 10 settings app? 您如何像在iOS 10设置应用中那样实现平滑的搜索栏动画? 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)
}

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. 在所有动画期间,如果您没有在动画内部调用view.layoutIfNeeded()的最高父视图,则它们看起来更容易跳动。

  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. 如果搜索栏嵌套在任何类型的滚动视图(包括tableViews / collectionViews)内,则可能符合delaysContentTouches设置。 If this is the case, turn this to false inside of the scrollView and see if this helps anything. 如果是这种情况,请在scrollView内部将其设置为false,看看是否有帮助。 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 . 有很多很好的信息, 这里的基本iOS的东西,你可能会发现有用的,我是一个巨大的风扇保罗赫加蒂的斯坦福iOS版雨燕过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM