自定义UISearchController动画 [英] Custom UISearchController Animation

查看:142
本文介绍了自定义UISearchController动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何覆盖属于UISearchController的searchBar的默认动画?

How do I override the default animation for the dismissal of a searchBar belonging to a UISearchController?

好的所以我正在尝试创建一个自定义动画附加到UISearchController的UISearchBar变为活动状态。似乎标准动画期望searchBar以占据屏幕的宽度开始。当动画开始时,缩小searchBar并在其右侧的取消按钮中淡入淡出。

Okay so I am trying to create a custom animation for when the UISearchBar that is attached to a UISearchController becomes active. It seems the standard animation expects for the searchBar to begin with a width that takes up the screen. When the animation begins it shrinks the searchBar and fades in a cancel button to the right of it.

我想让我的searchBar开始在一个小的状态下,大约是屏幕宽度的一半,允许两个按钮放在旁边的导航栏中。

I want to have my searchBar begin in a small state, roughly half the screen width, to allow for two buttons to be placed in the navbar next to it as well.

当searchBar变为活动状态时,我希望动画能够展开searchBar和取消按钮淡入。

When the searchBar becomes active I want the animation to expand the searchBar and for the cancel button to fade in.

当searchBar被解雇时,我想要完全相反的动画:取消按钮淡出,searchBar缩小到原始大小。

When the searchBar is dismissed I want the exact opposite animation to occur: Cancel button fade out and searchBar to shrink to it's original size.

我找到了一种方法,通过使用UISearchControllerDelegate方法实现所需的呈现动画,presentSearchController:

I have found a way to achieve the desired presenting animation by using a UISearchControllerDelegate method, presentSearchController:

func presentSearchController(searchController: UISearchController) {

    // Animate Buttons
    UIView.animateWithDuration(0.1, animations: {

        // First Hide Buttons
        self.createMoxyButton.alpha = 0
        self.centerMapButton.alpha  = 0
    })

    // Animate Search Bar
    UIView.animateWithDuration(0.5, animations: {

        // Search Bar
        searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, self.wBounds - 40, searchController.searchBar.frame.height)
        self

    })

}

但是我无法实现解雇动画。我已经尝试使用didDismissSearchController:和willDismissSearchController:委托方法但它导致奇怪的行为,并且不使用我在这些相应的委托方法中设置的帧的动画。当searchBar被解除时,它会扩展到全屏宽度,同时淡出取消按钮,然后它会立即将searchBar的帧更改回原始大小,忽略我的动画。我也尝试使用removeAllAnimation()方法尝试阻止默认动画发生,但无济于事。

but I have not been able to achieve the dismissal animation. I have tried using the didDismissSearchController: and willDismissSearchController: delegate methods but it results in weird behavior and does not use the animation of frames that I set in these respective delegate methods. When the searchBar is dismissed it will expand to the full screen width, while fading out cancel button, then it will immediately change the frame of the searchBar back to the original size, ignoring my animation. I have also tried using the removeAllAnimation() method to try and stop the default animation from taking place, but to no avail.

func didDismissSearchController(searchController: UISearchController) {
    searchController.searchBar.layer.removeAllAnimations()

    // Animate
    UIView.animateWithDuration(0.5, animations: {

        // Show hidden buttons
        self.createMoxyButton.alpha = 1
        self.centerMapButton.alpha  = 1

        // Search Bar
        searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, self.wBounds - 10 - self.createMoxyButton.frame.size.width - 20 - self.centerMapButton.frame.size.width - 20, searchController.searchBar.frame.height)
        self

    })
}



解决问题的图像SearchBar



Gif动画从处于活动状态的searchBar,取消按钮可见

Image of Problem Dismissing SearchBar

Gif Animation begins with the searchBar in the Active state with the cancel button visible

推荐答案

子类UISearchController并实现可选的 UIViewControllerTransitioningDelegate
方法
- (可空id< UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController :( UIViewController *)被解雇;

Subclass UISearchController and implement the optional UIViewControllerTransitioningDelegate method - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;

返回动画对象以进行解散动画。

to return your animation object to do the animation for the dismiss.

这篇关于自定义UISearchController动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆