ios - 快速下拉列表

标签 ios xcode swift animation

我正在开发一个 iPhone 应用程序,它在导航栏下方有一个过滤器列表(下拉列表),当我单击导航栏按钮时会出现该列表。请建议我该怎么做。

enter image description here

最佳答案

有很多方法可以做到这一点,我的建议类似于以下内容:

当您初始化 View Controller 时,您的下拉 View 会偏移并隐藏在导航栏后面。使用布局约束或使用 View 的框架来执行此操作,具体取决于您的首选设置。

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

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

然后将一个 Action 链接到 BarButtonItem,当按下该按钮时,如果隐藏则显示 View ,如果使用动画可见则隐藏。

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

func hideDropDownView() {
     var frame: CGRect = 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 并正确链接它。

希望对你有帮助,有不明白的地方欢迎留言

关于ios - 快速下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28828882/

相关文章:

ios - 在 Swift 中为 UI 通知创建方法

iphone - 触摸后,Objective C 注释 View 显示缓慢

iOS Domain=kCLErrorDomain Code=1 在请求位置权限之前

objective-c - 使用 respondsToSelector 的性能损失

javascript - 将 PFQueries 的上限更改为 1000 以上

swift - Carthage更新后无法找到接口(interface)声明错误Xcode 10.2

ios - 获取没有 keyboardWillAppear 用户信息通知的键盘框架,iOS

ios - 如何在 iOS 上仅在模糊 View 底部获得圆角且颜色清晰?

ios - libz.dylib 与 libz.1.2.3.dylib 与 libz.1.2.5.dylib

ios - 在 SwiftUI 中使用像 MVC 这样的设计模式