Skip to content

Commit

Permalink
Another approach (negative inset on views).
Browse files Browse the repository at this point in the history
  • Loading branch information
BohdanOrlov committed Oct 14, 2016
1 parent 6001d7f commit 9e9123a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
55 changes: 31 additions & 24 deletions BouncyPageViewController/Classes/BouncyPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import CoreGraphics
open class BouncyPageViewController: UIViewController {
//MARK: - VLC
fileprivate let scrollView = UIScrollView()
fileprivate var topBackgroundView = UIView()
fileprivate var bottomBackgroundView = UIView()
fileprivate var backgroundViews: [UIView]!
open fileprivate(set) var viewControllers: [UIViewController?] = [nil]
public init(viewControllers: [UIViewController]) {
Expand All @@ -25,6 +27,8 @@ open class BouncyPageViewController: UIViewController {

open override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.topBackgroundView)
self.view.addSubview(self.bottomBackgroundView)
self.scrollView.panGestureRecognizer.addTarget(self, action: #selector(didPan(recognizer:)))
self.scrollView.delegate = self
self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast
Expand All @@ -44,8 +48,8 @@ open class BouncyPageViewController: UIViewController {
}
self.appendPlaceholdersIfNeeded()
let rects: (slice: CGRect, remainder: CGRect) = self.view.bounds.divided(atDistance: self.view.bounds.midY, from: .minYEdge)
// self.topBackgrounView.frame = rects.slice
// self.bottomBackgrounView.frame = rects.remainder
self.topBackgroundView.frame = rects.slice
self.bottomBackgroundView.frame = rects.remainder
self.baseOffset = self.pageSize().height
self.scrollView.frame = self.view.bounds
self.scrollView.contentSize = CGSize(width: self.view.bounds.width, height: self.pageSize().height * CGFloat(self.maxNumberOfPages()))
Expand All @@ -64,19 +68,22 @@ open class BouncyPageViewController: UIViewController {
for idx in (0..<self.maxNumberOfPages()) {
var pageOffset = self.pageSize().height * CGFloat(idx)
let origin = CGPoint(x: 0, y: pageOffset)
var rect = CGRect(origin: origin, size: self.pageSize())
var pageRect = CGRect(origin: origin, size: self.pageSize())


var viewController = self.viewControllers[idx];
let vcIsVisible: Bool = self.scrollView.bounds.intersects(rect)
let vcIsVisible: Bool = self.scrollView.bounds.intersects(pageRect)
if viewController == nil && vcIsVisible {
viewController = self.requestViewController(index:idx)
self.viewControllers[idx] = viewController
}
self.backgroundViews[idx].frame = rect;
let backgroundView = self.backgroundViews[idx]
backgroundView.frame = pageRect.insetBy(dx: -overlapDelta, dy: 0);
backgroundView.isHidden = !vcIsVisible
if let viewController = viewController {
self.addChildPageViewController(viewController: viewController)
self.backgroundViews[idx].backgroundColor = viewController.view.backgroundColor
backgroundView.backgroundColor = viewController.view.backgroundColor
// backgroundView.alpha = 0.5
}
let vcShouldWigle = viewController != self.viewControllers.first! && viewController != self.viewControllers.last!
if let vc = viewController {
Expand All @@ -89,12 +96,16 @@ open class BouncyPageViewController: UIViewController {
let velocity = self.scrollView.panGestureRecognizer.velocity(in: self.scrollView.panGestureRecognizer.view).y
// let velocity:CGFloat = 500.0

vc.view.frame = rect.insetBy(dx: 0, dy: -overlapDelta)
if let mask = viewController?.view.layer.mask as? CAShapeLayer {
vc.view.frame = pageRect.insetBy(dx: 0, dy: overlapDelta)

if backgroundView.layer.mask == nil {
let mask = CAShapeLayer()
mask.fillRule = kCAFillRuleEvenOdd

// print(CATransform3DIsIdentity(mask.transform))
if mask.path == nil {
mask.frame = vc.view.bounds
var maskRect = vc.view.bounds.insetBy(dx: -overlapDelta, dy: overlapDelta)
// if mask.path == nil {
mask.frame = backgroundView.bounds
var maskRect = backgroundView.bounds.insetBy(dx: -overlapDelta, dy: overlapDelta)
// if idx == 0 {
// maskRect.origin.y -= overlapDelta
// maskRect.size.height += overlapDelta
Expand All @@ -103,7 +114,8 @@ open class BouncyPageViewController: UIViewController {
// }

mask.path = UIBezierPath(rect: maskRect).cgPath
}
// backgroundView.layer.mask = mask
// }
// let angle:CGFloat = atan(overlapDelta / (vc.view.bounds.width / 2)) * abs(velocity) / 500.0
//// mask.transform = CATransform3DIdentity
//
Expand Down Expand Up @@ -152,9 +164,6 @@ open class BouncyPageViewController: UIViewController {
viewController.willMove(toParentViewController: self)
self.addChildViewController(viewController)
self.scrollView.addSubview(viewController.view)
let mask = CAShapeLayer()
mask.fillRule = kCAFillRuleEvenOdd
viewController.view.layer.mask = mask
viewController.didMove(toParentViewController: parent)
}

Expand Down Expand Up @@ -222,8 +231,8 @@ extension BouncyPageViewController: UIScrollViewDelegate {
UIView.animate(withDuration: 0.3, animations: { self.scrollView.contentOffset = CGPoint(x:0, y:self.baseOffset) })

}
// self.topBackgrounView.backgroundColor = self.vivibleControllers().first!.view.backgroundColor
// self.bottomBackgrounView.backgroundColor = self.vivibleControllers().last!.view.backgroundColor
self.topBackgroundView.backgroundColor = self.vivibleControllers().first!.view.backgroundColor
self.bottomBackgroundView.backgroundColor = self.vivibleControllers().last!.view.backgroundColor
}

private func vivibleControllers() -> [UIViewController] {
Expand All @@ -244,10 +253,6 @@ extension BouncyPageViewController: UIScrollViewDelegate {
@objc fileprivate func didPan(recognizer: UIPanGestureRecognizer) {

switch recognizer.state {
case .changed:
for vc in self.childViewControllers {
let mask = vc.view.layer.mask!
}
case .ended, .cancelled, .failed:
let offset:CGFloat
if abs(self.baseOffset - self.contentOffset()) < self.baseOffset / 2 {
Expand All @@ -271,8 +276,10 @@ extension BouncyPageViewController: UIScrollViewDelegate {
self.lastVelocity = velocity
self.lastLocationInView = locationInView

for vc in self.childViewControllers {
let mask = vc.view.layer.mask!
for view in self.backgroundViews {
// guard let mask = view.layer.mask else {
// continue;
// }
// var bounce = RBBSpringAnimation(keyPath: "transform")
// bounce.fromValue = NSValue(caTransform3D: CATransform3DMakeRotation(fromAngle, 0.0, 0.0, 1.0))
// bounce.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(toAngle, 0.0, 0.0, 1.0))
Expand All @@ -287,7 +294,7 @@ extension BouncyPageViewController: UIScrollViewDelegate {
bounce.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(toAngle, 0.0, 0.0, 1.0))
bounce.easing = RBBEasingFunctionEaseOutBounce
bounce.duration = 1
mask.add(bounce, forKey: "bounce")
view.layer.add(bounce, forKey: "bounce")
}
}

Expand Down
4 changes: 2 additions & 2 deletions Example/BouncyPageViewController/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="BR2-5c-aDO" firstAttribute="centerY" secondItem="ppF-Mp-aoy" secondAttribute="centerY" constant="-30" id="94c-Lt-GGB"/>
<constraint firstItem="BR2-5c-aDO" firstAttribute="centerY" secondItem="ppF-Mp-aoy" secondAttribute="centerY" id="94c-Lt-GGB"/>
<constraint firstItem="Kdr-We-MmS" firstAttribute="centerX" secondItem="ppF-Mp-aoy" secondAttribute="centerX" id="AU3-hr-4Mh"/>
<constraint firstItem="BR2-5c-aDO" firstAttribute="centerX" secondItem="ppF-Mp-aoy" secondAttribute="centerX" id="JNc-DL-7YQ"/>
<constraint firstItem="Kdr-We-MmS" firstAttribute="width" secondItem="ppF-Mp-aoy" secondAttribute="width" id="NhJ-NB-kIp"/>
<constraint firstItem="HdQ-lZ-LIS" firstAttribute="top" secondItem="Kdr-We-MmS" secondAttribute="bottom" constant="30" id="aEC-iM-Ncj"/>
<constraint firstItem="HdQ-lZ-LIS" firstAttribute="top" secondItem="Kdr-We-MmS" secondAttribute="bottom" id="aEC-iM-Ncj"/>
</constraints>
</view>
<connections>
Expand Down

0 comments on commit 9e9123a

Please sign in to comment.