SwiftDropMenuView
一個(gè)自定義下拉菜單的實(shí)現(xiàn)纱兑,可用于一些篩選菜單艳汽。
GitHub: https://github.com/tuxi/SwiftDropMenuView
根據(jù)項(xiàng)目中經(jīng)常使用的幾種情況(比如地圖上的范圍篩選、商品的分類(lèi)及價(jià)格篩選等等),封裝的下拉菜單腥例,可查看示例了解其使用原理
已實(shí)現(xiàn)兩種菜單:
- 1.通用的下拉菜單
SwiftDropMenuControl
,可通過(guò)初始化方法中傳入listView
自定義其展開(kāi)內(nèi)容
func createMenuControl() {
/// 使用`frame`方式
let menuControl = SwiftDropMenuControl(frame: CGRect(x: 100, y: 280, width: 120, height: 44), listView: MapFilterDropMenuListView())
menuControl.setTitle("自定義菜單", for: .normal)
menuControl.setTitleColor(.gray, for: .normal)
menuControl.setTitleColor(.gray, for: .normal)
menuControl.setTitleColor(.red, for: .selected)
menuControl.setImage(UIImage(named: "icon_sort_arrow_down_1"), for: .normal)
menuControl.setImage(UIImage(named: "icon_sort_arrow_up_1"), for: .selected)
menuControl.contentHorizontalAlignment = .right
menuControl.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 50)
menuControl.animateDuration = 0.1
menuControl.delegate = self
menuControl.sizeToFit()
self.view.addSubview(menuControl)
}
-
SwiftDropMenuListView
是基于SwiftDropMenuControl
酝润,使用CollectionView
作為下拉菜單的listView
的下拉菜單
-
func createMenuListView() {
let menuView = SwiftDropMenuListView(frame: .zero)
menuView.setTitle("menu", for: .normal)
self.view.addSubview(menuView)
menuView.delegate = self
menuView.dataSource = self
menuView.backgroundColor = UIColor.orange
menuView.translatesAutoresizingMaskIntoConstraints = false
menuView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 60.0).isActive = true
menuView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
menuView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
menuView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.6).isActive = true
}
通過(guò)SwiftDropMenuControl
的show()
和dismiss()
方法燎竖,展開(kāi)和收起下拉菜單,通常情況要销,我們不需要主動(dòng)調(diào)用show ()
展開(kāi)菜單构回,因?yàn)樵谟脩酎c(diǎn)擊SwiftDropMenuControl
時(shí),show()
方法會(huì)被執(zhí)行,如果你不需要點(diǎn)擊時(shí)展開(kāi)纤掸,可實(shí)現(xiàn)代理方法`shouldTou
下拉菜單展開(kāi)或收起的代理方法
/// 反內(nèi)容饋顯示或消失的回調(diào)
public protocol SwiftDropMenuControlContentAppearable {
func on(appear element: SwiftDropMenuControl.AppearElement, forDropMenu menu: SwiftDropMenuControl)
}
@objc public protocol SwiftDropMenuControlDelegate: NSObjectProtocol {
@objc optional func shouldTouchMe(forDropMenu menu: SwiftDropMenuControl) -> Bool
}