Skip to content

Commit

Permalink
due to gesture issue, not support have two sides at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
superk589 committed Jan 13, 2017
1 parent ec25e19 commit 2a3c8b2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 87 deletions.
2 changes: 1 addition & 1 deletion ZKDrawerController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ZKDrawerController"
s.version = "0.0.5"
s.version = "0.0.6"
s.summary = "An iOS drawer controller in swift."
s.description = <<-DESC
A light-weighted iOS drawer controller in swift.
Expand Down
2 changes: 1 addition & 1 deletion ZKDrawerController/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let left = UIViewController()
left.view.backgroundColor = UIColor.clear

let drawer = ZKDrawerController(main: nav, right: right, left: left)
let drawer = ZKDrawerController.init(main: nav, left: left)
drawer.mainScale = 0.8
drawer.containerView.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
drawer.drawerStyle = .plain
Expand Down
152 changes: 77 additions & 75 deletions ZKDrawerController/Core/ZKDrawerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public enum ZKDrawerStatus {
case center
}

public protocol ZKDrawerControllerDelegate: class {
func drawerController(_ drawerVC: ZKDrawerController, willShow vc: UIViewController)
func drawerController(_ drawerVC: ZKDrawerController, didHide vc: UIViewController)
}

open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate {

open var defaultRightWidth: CGFloat = 300 {
Expand Down Expand Up @@ -88,6 +93,7 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate {
/// 右侧抽屉视图控制器
open var rightVC: UIViewController? {
didSet {
removeOldVC(vc: leftVC)
removeOldVC(vc: oldValue)
setupRightVC(vc: rightVC)
}
Expand All @@ -96,6 +102,7 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate {
/// 左侧抽屉视图控制器
open var leftVC: UIViewController? {
didSet {
removeOldVC(vc: rightVC)
removeOldVC(vc: oldValue)
setupLeftVC(vc: leftVC)
}
Expand All @@ -104,8 +111,21 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate {
/// 主视图在抽屉出现后的缩放比例
open var mainScale: CGFloat = 1

weak var delegate: ZKDrawerControllerDelegate?

public convenience init(main: UIViewController, right: UIViewController) {
self.init(main: main, right: right, left: nil)
}

public init(main: UIViewController, right: UIViewController?, left: UIViewController?) {
public convenience init(main: UIViewController, left: UIViewController) {
self.init(main: main, right: nil, left: left)
}

public convenience init(main: UIViewController) {
self.init(main: main, right: nil, left: nil)
}

init(main: UIViewController, right: UIViewController?, left: UIViewController?) {
super.init(nibName: nil, bundle: nil)
containerView = ZKDrawerScrollView()
backgroundImageView = UIImageView()
Expand Down Expand Up @@ -242,18 +262,14 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate {
}

func drawerCoverViewTapped(_ view: ZKDrawerCoverView) {
if status == .right {
self.hideRight(animated: true)
} else if status == .left {
self.hideleft(animated: true)
}
hide(animated: true)
}

/// 弹出预先设定好的右侧抽屉ViewController
/// 弹出预先设定好的抽屉ViewController
///
/// - Parameter animated: 是否有过度动画
open func showRight(animated: Bool) {
if let frame = rightVC?.view.frame {
open func show(animated: Bool) {
if let frame = rightVC?.view.frame ?? leftVC?.view.frame {
self.containerView.scrollRectToVisible(frame, animated: animated)
}
}
Expand All @@ -266,58 +282,44 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate {
/// - animated: 是否有过度动画
open func showRightVC(_ vc: UIViewController, animated: Bool) {
rightVC = vc
showRight(animated: animated)
show(animated: animated)
}


/// 隐藏右侧抽屉
///
/// - Parameter animated: 是否有过度动画
open func hideRight(animated: Bool) {
open func hide(animated: Bool) {
self.containerView.setContentOffset(CGPoint.init(x: self.leftWidth, y: 0), animated: animated)
}

/// 弹出预先设定好的左侧抽屉ViewController
///
/// - Parameter animated: 是否有过度动画
open func showLeft(animated: Bool) {
if let frame = leftVC?.view.frame {
self.containerView.scrollRectToVisible(frame, animated: animated)
}
}



/// 传入一个新的ViewController并从左侧弹出
///
/// - Parameters:
/// - vc: ViewController
/// - animated: 是否有过度动画
open func showleftVC(_ vc: UIViewController, animated: Bool) {
leftVC = vc
showLeft(animated: animated)
show(animated: animated)
}

/// 隐藏左侧抽屉
///
/// - Parameter animated: 是否有过度动画
open func hideleft(animated: Bool) {
hideRight(animated: animated)
}


}

extension ZKDrawerController: UIScrollViewDelegate {

open func scrollViewDidScroll(_ scrollView: UIScrollView) {

// 左右同时有抽屉时 在滑动回到正中时停止
if lastStatus != .center && lastStatus != status {
lastStatus = status
scrollView.setContentOffset(CGPoint.init(x: leftWidth, y: 0), animated: false)
} else {
lastStatus = status
if lastStatus == .center && lastStatus != status {
if let vc = rightVC ?? leftVC {
delegate?.drawerController(self, willShow: vc)
}
} else if lastStatus != .center && lastStatus != status {
if let vc = rightVC ?? leftVC {
delegate?.drawerController(self, willShow: vc)
}
}
lastStatus = status

let width = scrollView.frame.size.width
let offsetX = scrollView.contentOffset.x
Expand Down Expand Up @@ -373,45 +375,45 @@ extension ZKDrawerController: UIScrollViewDelegate {
}
}

public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
addPageAnimation(scrollView)
}
}
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
addPageAnimation(scrollView)
}

public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
scrollView.isUserInteractionEnabled = true
}


// 用于添加类似pageEnabled的效果
func addPageAnimation(_ scrollView: UIScrollView) {
let width = scrollView.frame.size.width
let offsetX = scrollView.contentOffset.x

/// 0 to 1 progress of the drawer showing
let progress: CGFloat = {
if status == .left {
return (leftWidth - offsetX) / leftWidth
} else if status == .right {
return (width + rightWidth - scrollView.contentSize.width + offsetX) / rightWidth
}
return 0
}()

if progress >= 0.5 {
if status == .left {
scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
} else if status == .right {
scrollView.setContentOffset(CGPoint.init(x: scrollView.contentSize.width - width, y: 0), animated: true)
}
} else {
scrollView.setContentOffset(CGPoint.init(x: leftWidth, y: 0), animated: true)
}
}
// public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
// if !decelerate {
// addPageAnimation(scrollView)
// }
// }
// public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
// addPageAnimation(scrollView)
// }
//
// public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
// scrollView.isUserInteractionEnabled = true
// }
//
//
// // 用于添加类似pageEnabled的效果
// func addPageAnimation(_ scrollView: UIScrollView) {
// let width = scrollView.frame.size.width
// let offsetX = scrollView.contentOffset.x
//
// /// 0 to 1 progress of the drawer showing
// let progress: CGFloat = {
// if status == .left {
// return (leftWidth - offsetX) / leftWidth
// } else if status == .right {
// return (width + rightWidth - scrollView.contentSize.width + offsetX) / rightWidth
// }
// return 0
// }()
//
// if progress >= 0.5 {
// if status == .left {
// scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
// } else if status == .right {
// scrollView.setContentOffset(CGPoint.init(x: scrollView.contentSize.width - width, y: 0), animated: true)
// }
// } else {
// scrollView.setContentOffset(CGPoint.init(x: leftWidth, y: 0), animated: true)
// }
// }

}

Expand Down
13 changes: 7 additions & 6 deletions ZKDrawerController/Core/ZKDrawerScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ZKDrawerScrollView: UIScrollView {
self.backgroundColor = UIColor.clear
self.showsHorizontalScrollIndicator = false
self.bounces = false
self.isPagingEnabled = true
}

public required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -64,10 +65,10 @@ public class ZKDrawerScrollView: UIScrollView {
}
return false
}
public override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
super.setContentOffset(contentOffset, animated: animated)
if animated && contentOffset != self.contentOffset {
self.isUserInteractionEnabled = false
}
}
// public override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
// super.setContentOffset(contentOffset, animated: animated)
// if animated && contentOffset != self.contentOffset {
// self.isUserInteractionEnabled = false
// }
// }
}
8 changes: 4 additions & 4 deletions ZKDrawerController/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class ViewController: UIViewController {
super.viewDidLoad()
view.backgroundColor = UIColor.blue

navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "show", style: .plain, target: self, action: #selector(showRight))
//navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "show", style: .plain, target: self, action: #selector(showLeft))
//navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "show", style: .plain, target: self, action: #selector(showRight))
navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "show", style: .plain, target: self, action: #selector(showLeft))
// Do any additional setup after loading the view, typically from a nib.

let button = UIButton.init(frame: CGRect.init(x: 200, y: 200, width: 150, height: 100))
Expand All @@ -27,10 +27,10 @@ class ViewController: UIViewController {
}

func showLeft() {
drawerController.showLeft(animated: true)
drawerController.show(animated: true)
}
func showRight() {
drawerController.showRight(animated: true)
drawerController.show(animated: true)
}

func push() {
Expand Down

0 comments on commit 2a3c8b2

Please sign in to comment.