Swift iOS : 抽屜效果

RecoReco發表於2017-05-24

可以使用DrawerController框架實現抽屜的效果。看起來還是蠻帥氣的。

需要做的就是一個DrawerController,3個ViewController。3個ViewController分別用於左側的抽屜,當前的抽屜,右側的抽屜。DrawerController用來管理所有的抽屜。然後就可以通過手勢左滑、右滑拉出和推入抽屜介面了。首先使用Pod引入此框架:

target 'four' do
  use_frameworks!
  pod 'DrawerController', '~> 3.2'
end複製程式碼

記得去執行下安裝:

pod install --verbose --no-repo-update複製程式碼

如下程式碼可以直接執行:

import UIKit
import DrawerController

var drawerController : DrawerPage?

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow()
        drawerController = DrawerPage()
        window!.rootViewController = drawerController
        window!.rootViewController!.view.backgroundColor = .blue
        window!.makeKeyAndVisible()
        return true
    }
}
class DrawerPage : DrawerBase{
    init(){
        super.init(CenterPage(),LeftPage(),RightPage())
    }
    // 哄編譯器開心的程式碼
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
class DrawerBase : DrawerController{
    init(_ center : UIViewController,_ left : UIViewController,_ right : UIViewController){
        super.init(centerViewController: center, leftDrawerViewController: left, rightDrawerViewController: right)
        openDrawerGestureModeMask=OpenDrawerGestureMode.panningCenterView
        closeDrawerGestureModeMask=CloseDrawerGestureMode.all;
    }
    // 從入門到入門: 
    // 1. What exactly is init coder aDecoder?
    // 2. What does the question mark means in public init?(coder aDecoder: NSCoder)?
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class LeftPage: UIViewController {
    var count = 0
    var label : UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        label   = UILabel()
        label.frame = CGRect(x: 100, y: 100, width: 120, height: 50)
        label.text =  "Left"
        view.addSubview(label)
        let button   = UIButton(type: .system)
        button.frame = CGRect(x: 120, y: 150, width: 120, height: 50)
        button.setTitle("Close",for: .normal)
        button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
        view.addSubview(button)
    }
    func buttonAction(_ sender:UIButton!){
        drawerController?.toggleLeftDrawerSide(animated: true, completion: nil)
    }
}
class RightPage: UIViewController {
    var count = 0
    var label : UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        label   = UILabel()
        label.frame = CGRect(x: 100, y: 100, width: 120, height: 50)
        label.text =  "Right"
        view.addSubview(label)
        let button   = UIButton(type: .system)
        button.frame = CGRect(x: 120, y: 150, width: 120, height: 50)
        button.setTitle("Close",for: .normal)
        button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
        view.addSubview(button)
    }
    func buttonAction(_ sender:UIButton!){
        drawerController?.toggleRightDrawerSide(animated: true, completion: nil)
    }
}
class CenterPage: UIViewController {
    var label : UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        label   = UILabel()
        label.frame = CGRect(x: 100, y: 100, width: 120, height: 50)
        label.text =  "Center"
        view.addSubview(label)
        let button   = UIButton(type: .system)
        button.frame = CGRect(x: 120, y: 150, width: 120, height: 50)
        button.backgroundColor = .blue
        button.setTitle("Left Page Drawer",for: .normal)
        button.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
        view.addSubview(button)
        let button1   = UIButton(type: .system)
        button1.frame = CGRect(x: 120, y: 200, width: 220, height: 50)
        button1.contentHorizontalAlignment = .left
        button1.setTitle("Right Page Drawer",for: .normal)
        button1.addTarget(self, action: #selector(buttonAction1(_:)), for: .touchUpInside)
        button1.backgroundColor = .red
        view.addSubview(button1)
    }
    func buttonAction(_ sender:UIButton!){
        drawerController?.toggleLeftDrawerSide(animated: true, completion: nil)
    }
    func buttonAction1(_ sender:UIButton!){
        drawerController?.toggleRightDrawerSide(animated: true, completion: nil)
    }
}複製程式碼

程式碼執行起來後,可以看到首頁標籤顯示為Center,還有兩個按鈕,分別為Left Page Drawer ,Right Page Drawer。點選即可拉出左側或者右側的抽屜(一個ViewController)。也可以使用手勢拉出和推入抽屜。

相關文章