From 2a3c8b25cc881fe4c74c815c64ebb15a73bb6517 Mon Sep 17 00:00:00 2001 From: superk589 Date: Fri, 13 Jan 2017 15:36:18 +0800 Subject: [PATCH] due to gesture issue, not support have two sides at the same time --- ZKDrawerController.podspec | 2 +- ZKDrawerController/AppDelegate.swift | 2 +- .../Core/ZKDrawerController.swift | 152 +++++++++--------- .../Core/ZKDrawerScrollView.swift | 13 +- ZKDrawerController/ViewController.swift | 8 +- 5 files changed, 90 insertions(+), 87 deletions(-) diff --git a/ZKDrawerController.podspec b/ZKDrawerController.podspec index 0d4683e..8de8f3b 100644 --- a/ZKDrawerController.podspec +++ b/ZKDrawerController.podspec @@ -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. diff --git a/ZKDrawerController/AppDelegate.swift b/ZKDrawerController/AppDelegate.swift index 5267867..aa72275 100644 --- a/ZKDrawerController/AppDelegate.swift +++ b/ZKDrawerController/AppDelegate.swift @@ -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 diff --git a/ZKDrawerController/Core/ZKDrawerController.swift b/ZKDrawerController/Core/ZKDrawerController.swift index c3e31c0..527e35a 100644 --- a/ZKDrawerController/Core/ZKDrawerController.swift +++ b/ZKDrawerController/Core/ZKDrawerController.swift @@ -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 { @@ -88,6 +93,7 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate { /// 右侧抽屉视图控制器 open var rightVC: UIViewController? { didSet { + removeOldVC(vc: leftVC) removeOldVC(vc: oldValue) setupRightVC(vc: rightVC) } @@ -96,6 +102,7 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate { /// 左侧抽屉视图控制器 open var leftVC: UIViewController? { didSet { + removeOldVC(vc: rightVC) removeOldVC(vc: oldValue) setupLeftVC(vc: leftVC) } @@ -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() @@ -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) } } @@ -266,27 +282,18 @@ 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: @@ -294,30 +301,25 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate { /// - 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 @@ -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) +// } +// } } diff --git a/ZKDrawerController/Core/ZKDrawerScrollView.swift b/ZKDrawerController/Core/ZKDrawerScrollView.swift index 0ca841e..76c1f16 100644 --- a/ZKDrawerController/Core/ZKDrawerScrollView.swift +++ b/ZKDrawerController/Core/ZKDrawerScrollView.swift @@ -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) { @@ -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 +// } +// } } diff --git a/ZKDrawerController/ViewController.swift b/ZKDrawerController/ViewController.swift index 4048e9e..dbe0af5 100644 --- a/ZKDrawerController/ViewController.swift +++ b/ZKDrawerController/ViewController.swift @@ -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)) @@ -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() {