Swift iOS : 抽屉效果

栏目: Swift · 发布时间: 8年前

内容简介:Swift iOS : 抽屉效果

可以使用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 : DrawerController?

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow()
        drawerController = DrawerController(centerViewController: CenterPage(), leftDrawerViewController: LeftPage(), rightDrawerViewController: RightPage());
        window!.rootViewController = drawerController
        drawerController!.openDrawerGestureModeMask=OpenDrawerGestureMode.panningCenterView
        drawerController!.closeDrawerGestureModeMask=CloseDrawerGestureMode.all;
        window!.rootViewController!.view.backgroundColor = .blue
        window!.makeKeyAndVisible()
        return true
    }
}
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)。也可以使用手势拉出和推入抽屉。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP 6与MySQL 5基础教程

PHP 6与MySQL 5基础教程

(美)厄尔曼 / 陈宗斌 等 / 人民邮电出版社 / 2008-11-1 / 65.00元

本书是一部经典的入门级著作,采用基于任务的方法来讲授PHP和MySQL,使用大量图片指导读者深入学习语言,并向读者展示了如何构造动态Web站点。书中用简洁、直观的步骤和讲解提供了学习任务和概念的最快方式。通过学习本书,读者可以快速、高效地掌握PHP和MySQL,成为一位构建Web站点的高手。 本书适合初中级Web应用开发和设计人员阅读。 本书是讲述PHP和MySQL技术的畅销书,以深入......一起来看看 《PHP 6与MySQL 5基础教程》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具