From 2699be3a8028f9e9b15fb45c9e1009356291b5b1 Mon Sep 17 00:00:00 2001 From: Anton Skopin Date: Mon, 11 Feb 2019 19:22:04 +0300 Subject: [PATCH] added support of landscape orientation and transition --- liquid-swipe.podspec | 13 +--- .../LiquidSwipeContainerController.swift | 71 ++++++++++++++++++- liquid-swipe/Classes/WaveLayer.swift | 16 +++++ 3 files changed, 86 insertions(+), 14 deletions(-) diff --git a/liquid-swipe.podspec b/liquid-swipe.podspec index af01da2..c7c4d59 100644 --- a/liquid-swipe.podspec +++ b/liquid-swipe.podspec @@ -8,20 +8,9 @@ Pod::Spec.new do |s| s.name = 'liquid-swipe' - s.version = '0.7.0' + s.version = '0.8.0' s.summary = 'An page conroller with liquid animation' s.swift_version = '4.2' - -# This description is used to generate tags and improve search results. -# * Think: What does it do? Why did you write it? What is the focus? -# * Try to keep it short, snappy and to the point. -# * Write the description between the DESC delimiters below. -# * Finally, don't worry about the indent, CocoaPods strips it! - -# s.description = <<-DESC -#TODO: Add long description of the pod here. -# DESC - s.homepage = 'https://github.com/Cuberto/liquid-swipe' # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' s.license = { :type => 'MIT', :file => 'LICENSE' } diff --git a/liquid-swipe/Classes/LiquidSwipeContainerController.swift b/liquid-swipe/Classes/LiquidSwipeContainerController.swift index 2e240d9..b1ef135 100644 --- a/liquid-swipe/Classes/LiquidSwipeContainerController.swift +++ b/liquid-swipe/Classes/LiquidSwipeContainerController.swift @@ -51,7 +51,12 @@ open class LiquidSwipeContainerController: UIViewController { private var maxVertRadius: CGFloat { return view.bounds.height * 0.9 } - private var initialSideWidth: CGFloat = 15.0 + private var initialSideWidth: CGFloat { + if #available(iOS 11.0, *) { + return 15.0 + view.safeAreaInsets.right + } + return 15.0 + } private var initialWaveCenter: CGFloat { return view.bounds.height * 0.7167487685 } @@ -206,6 +211,10 @@ open class LiquidSwipeContainerController: UIViewController { self.delegate?.liquidSwipeContainer(self, didFinishTransitionTo: viewController, transitionCompleted: false) } } + if let mask = nextViewController?.view?.layer.mask as? WaveLayer { + mask.frame = self.view.bounds + mask.updatePath() + } currentPage?.pop_add(animation, forKey: "animation") } } @@ -282,9 +291,14 @@ open class LiquidSwipeContainerController: UIViewController { } if self.shouldCancel, let viewController = self.previousViewController { + viewController.view.isHidden = true self.delegate?.liquidSwipeContainer(self, didFinishTransitionTo: viewController, transitionCompleted: false) } } + if let mask = previousViewController?.view?.layer.mask as? WaveLayer { + mask.frame = self.view.bounds + mask.updatePath() + } previousViewController?.view.pop_add(previousViewAnimation, forKey: "animation") guard nextViewController != nil else { return @@ -496,6 +510,10 @@ open class LiquidSwipeContainerController: UIViewController { guard let page = nextVC.view else { return } + if let mask = page.layer.mask as? WaveLayer { + mask.frame = view.bounds + mask.updatePath() + } if let currentPage = currentPage { view.insertSubview(page, belowSubview: currentPage) } else { @@ -519,6 +537,10 @@ open class LiquidSwipeContainerController: UIViewController { guard let page = previousVC.view else { return } + if let mask = page.layer.mask as? WaveLayer { + mask.frame = view.bounds + mask.updatePath() + } if let currentPage = currentPage { view.insertSubview(page, aboveSubview: currentPage) } else { @@ -529,7 +551,7 @@ open class LiquidSwipeContainerController: UIViewController { } private func apply(mask: WaveLayer, on view: UIView) { - mask.frame = view.bounds + mask.frame = self.view.bounds mask.updatePath() view.layer.mask = mask } @@ -560,6 +582,51 @@ open class LiquidSwipeContainerController: UIViewController { } currentPage?.pop_add(animation, forKey: "animation") } + + override open func viewSafeAreaInsetsDidChange() { + if let mask = self.currentPage?.layer.mask as? WaveLayer { + if mask.sideWidth > 0 { + mask.sideWidth = initialSideWidth + mask.updatePath() + csBtnNextLeading?.constant = -(mask.waveHorRadius + mask.sideWidth - 8.0) + view.layoutIfNeeded() + } + } + + } + + override open func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { + let btnNextWasHidden = btnNext.isHidden + btnNext.isHidden = true + currentPage?.layer.mask = nil + previousViewController?.view?.layer.mask = nil + nextViewController?.view?.layer.mask = nil + + coordinator.animate(alongsideTransition: { (_) in + }) { (_) in + if let currentPage = self.currentPage { + let hasNextPage = self.nextViewController != nil + let maskLayer = WaveLayer(waveCenterY: self.initialWaveCenter, + waveHorRadius: hasNextPage ? self.initialHorRadius : 0, + waveVertRadius: self.initialVertRadius, sideWidth: hasNextPage ?self.initialSideWidth : 0) + self.apply(mask: maskLayer, on: currentPage) + } + if let nextPage = self.nextViewController?.view { + let maskLayer = WaveLayer(waveCenterY: self.initialWaveCenter, waveHorRadius: 0, waveVertRadius: self.initialVertRadius, sideWidth: 0) + self.apply(mask: maskLayer, on: nextPage) + } + if let prevPage = self.previousViewController?.view { + let maskLayer = WaveLayer(waveCenterY: self.initialWaveCenter, waveHorRadius: 0, waveVertRadius: self.initialVertRadius, sideWidth: prevPage.bounds.height) + self.apply(mask: maskLayer, on: prevPage) + } + self.csBtnNextCenterY?.constant = self.initialWaveCenter + self.csBtnNextLeading?.constant = -(self.initialHorRadius + self.initialSideWidth - 8.0) + self.btnNext.isHidden = btnNextWasHidden + self.btnNext.transform = CGAffineTransform.identity + self.view.layoutIfNeeded() + } + super.viewWillTransition(to: size, with: coordinator) + } } //MARK: Animation helpers diff --git a/liquid-swipe/Classes/WaveLayer.swift b/liquid-swipe/Classes/WaveLayer.swift index b9ae4bb..3cd594e 100644 --- a/liquid-swipe/Classes/WaveLayer.swift +++ b/liquid-swipe/Classes/WaveLayer.swift @@ -13,6 +13,22 @@ internal class WaveLayer: CAShapeLayer { var waveHorRadius: CGFloat var waveVertRadius: CGFloat var sideWidth: CGFloat + + override public init(layer: Any) { + if let waveLayer = layer as? WaveLayer { + waveCenterY = waveLayer.waveCenterY + waveHorRadius = waveLayer.waveHorRadius + waveVertRadius = waveLayer.waveVertRadius + sideWidth = waveLayer.sideWidth + } else { + waveCenterY = 0 + waveHorRadius = 0 + waveVertRadius = 0 + sideWidth = 0 + } + super.init(layer: layer) + } + init(waveCenterY: CGFloat, waveHorRadius: CGFloat, waveVertRadius: CGFloat, sideWidth: CGFloat) { self.waveCenterY = waveCenterY self.waveHorRadius = waveHorRadius