下拉列表中迅速 [英] Dropdown list in swift

查看:144
本文介绍了下拉列表中迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有,当我点击栏按钮显示的导航栏下的筛选器列表(下拉列表)的iPhone应用程序。请建议我怎么能做到这一点。

I am working on an iPhone app that has a filter list (dropdown list) under the navigation bar that appears when I click on the bar button. Please suggest me how can I do it.

推荐答案

有许多方法可以做到这一点,我的建议将是类似如下的内容:

There are a number ways to do it, my suggestions would be something similar to as follows:

在初始化视图控制器,你的下拉列表视图偏移和隐藏的导航栏的后面。做到这一点无论是与布局约束或使用视图的框架,根据您的preferred成立。

When you initialise the view controller, your dropdown view is offset and hidden behind the navigation bar. Do this either with Layout Constraints or using the view's frame, depending on your preferred set up.

var isAnimating: Bool = false
var dropDownViewIsDisplayed: Bool = false

func viewDidLoad() {
    super.viewDidLoad()
    CGFloat height = self.dropDownView.frame.size.height
    CGFloat width = self.dropDownView.frame.size.width
    self.dropDownView.frame = CGRectMake(0, -height, width, height)
    self.dropDownViewIsDisplayed = false
}

然后一个动作连接起来的BarButtonItem,当pressed,显示视图,如果隐藏或可见,如果使用动画隐藏。

Then link up an action to the BarButtonItem that, when pressed, displays the view if hidden or hides if visible using an animation.

@IBAction func barButtonItemPressed(sender: UIBarButtonItem?) {
    if (self.dropDownViewIsDisplayed) {
        self.hideDropDownView()
    } else {
        self.showDropDownView()
    }
}

func hideDropDownView() {
     CGRect frame = self.dropDownView.frame
     frame.origin.y = -frame.size.height
     self.animateDropDownToFrame(frame) {
         self.dropDownViewIsDisplayed = false
     }
}

func showDropDownView() {
    CGRect frame = self.dropDownView.frame
    frame.origin.y = self.navigationBar.frame.size.height
    self.animateDropDownToFrame(frame) {
        self.dropDownViewIsDisplayed = true
    }
}

func animateDropDownToFrame(frame: CGRect, completion:() -> Void) {
    if (!self.animating) {
        self.animating = true
        UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: { () -> Void in
            self.dropDownView.frame = frame
            }, completion: (completed: Bool) -> Void in {
                self.animating = false
                if (completed) {
                     completion()
                }
            })
    }
}

所有这一切都留给你是定义dropDownView和正确链接起来。

All that is left for you is to define your dropDownView and link it up correctly.

我希望帮助,请评论,如果您有什么不明白

I hope that helps, please comment if there is anything you don't understand

这篇关于下拉列表中迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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