Skip to content

Commit

Permalink
Fix tabsView addHeader (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseevpg authored Feb 4, 2019
1 parent 0e21811 commit 0978def
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 53 deletions.
80 changes: 73 additions & 7 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@ import ParallaxPagerView

class ViewController: UIViewController {

var containerView: ContainerView?
var headerHeight: CGFloat = 300
var parallaxView: ParallaxPagerView!
@IBOutlet weak var buttonsView: UIView!

@IBAction func plusClicked(_ sender: Any) {
headerHeight += 30
parallaxView.setHeaderHeight(headerHeight, animated: true)
//headerHeight += 30

//parallaxView.setHeaderHeight(headerHeight, animated: true)

// self.parallaxView.addTabsHeader(UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100)))
containerView?.showBanner()
parallaxView.setTabsHeight(100)
}

@IBAction func minusClicked(_ sender: Any) {
guard headerHeight > 0 else { return }
headerHeight -= 30
parallaxView.setHeaderHeight(headerHeight, animated: true)
// headerHeight -= 30
// parallaxView.setHeaderHeight(headerHeight, animated: true)
// self.parallaxView.removeTabsHeader()
containerView?.hideBanner()
parallaxView.setTabsHeight(50)
}

override func viewDidLoad() {
Expand Down Expand Up @@ -78,16 +87,25 @@ class ViewController: UIViewController {
)
view.addSubview(parallaxView)

let tabsView = TabsView.tabsView(with: tabsConfig)
let containerView = ContainerView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 50), tabsView: tabsView)
self.containerView = containerView
Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { [weak self] (_) in
guard let `self` = self else { return }
self.parallaxView.setupPager(
with: viewControllers,
tabsViewConfig: tabsConfig,
tabsView: containerView,
pagerDelegate: self as? PagerDelegate,
animated: true,
completion: {
self.headerHeight += 100
self.parallaxView.setHeaderHeight(self.headerHeight, animated: true)
// self.headerHeight += 100
// self.parallaxView.setHeaderHeight(self.headerHeight, animated: true)
// self.para
// let duration = DispatchTime.now() + DispatchTimeInterval.seconds(3)
// DispatchQueue.main.asyncAfter(deadline: duration, execute: {
// containerView.hideBanner()
// self.parallaxView.setTabsHeight(50)
// })
}
)
}
Expand All @@ -103,3 +121,51 @@ extension ViewController: ParallaxViewDelegate {

}
}

class ContainerView: UIView, PagerTab {
var tabsView: TabsView

init(frame: CGRect, tabsView: TabsView) {
tabsView.frame = CGRect(x: 0, y: 0, width: frame.width, height: tabsView.frame.height)
self.tabsView = tabsView
self.onSelectedTabChanging = { oldTab, newTab in
tabsView.onSelectedTabChanging(oldTab, newTab)
}
super.init(frame: frame)
addSubview(tabsView)
backgroundColor = .red
clipsToBounds = true
}

required init?(coder aDecoder: NSCoder) {
fatalError()
}

func hideBanner() {
self.tabsView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: tabsView.frame.height)
self.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: tabsView.frame.height)
}

func showBanner() {
self.tabsView.frame = CGRect(x: 0, y: 50, width: self.frame.width, height: tabsView.frame.height)
self.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: tabsView.frame.height + 50)
}

var onSelectedTabChanging: (Int, Int) -> Void = { _, _ in } {
didSet {
tabsView.onSelectedTabChanging = onSelectedTabChanging
}
}

func currentSelectedIndex() -> Int {
return tabsView.currentSelectedIndex()
}

func numberOfTabs() -> Int {
return tabsView.numberOfTabs()
}

func setSelectedTab(at index: Int) {
tabsView.setSelectedTab(at: index)
}
}
4 changes: 2 additions & 2 deletions ParallaxPagerView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Sources/ParallaxPagerView.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = "com.ParallaxPagerView.ParallaxPagerView-iOS";
Expand All @@ -450,7 +450,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Sources/ParallaxPagerView.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.ParallaxPagerView.ParallaxPagerView-iOS";
PRODUCT_NAME = ParallaxPagerView;
Expand Down
58 changes: 33 additions & 25 deletions Sources/ParallaxPagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation
import UIKit

@available(iOSApplicationExtension 9.0, *)
public final class ParallaxPagerView: UIView {

private var tabsHeight: CGFloat = 0
Expand Down Expand Up @@ -135,8 +134,7 @@ public final class ParallaxPagerView: UIView {
pagerDelegate: PagerDelegate? = nil,
animated: Bool,
completion: (() -> Void)? = nil
) {

) {
self.pagerDelegate = pagerDelegate
self.tabsView = tabsView
tabsHeight = tabsView.frame.size.height
Expand Down Expand Up @@ -177,28 +175,37 @@ public final class ParallaxPagerView: UIView {
let heightToBe = headerHeightConstraint?.constant ?? 0 + heightDiff
if heightToBe > minimumHeaderHeight {
headerHeightConstraint?.constant = heightToBe
guard
let currentDisplayController = currentDisplayController,
let currentScrollView = scrollViewInViewController(vc: currentDisplayController)
else { return }
guard let currentDisplayController = currentDisplayController,
let currentScrollView = scrollViewInViewController(vc: currentDisplayController)
else { return }
let offsetToBe = currentScrollView.contentOffset.y - heightDiff
currentScrollView.setContentOffset(CGPoint(x: internalScrollView.contentOffset.x, y: offsetToBe), animated: animated)
}
}

public func setTabsHeight(_ height: CGFloat, animated: Bool = false) {
guard
let currentDisplayController = currentDisplayController,
let currentScrollView = scrollViewInViewController(vc: currentDisplayController),
let constraint = tabsHeightConstraint, height >= 0
else { return }
guard let constraint = tabsHeightConstraint,
height >= 0 else {
return
}
let diff = height - tabsHeight
tabsHeight = height
constraint.constant = tabsHeight
let headerHeightConstant = headerHeightConstraint?.constant ?? 0.0
currentScrollView.contentOffset = CGPoint(x: 0.0, y: -tabsHeight - headerHeightConstant)

guard let vc = currentDisplayController else { return }
let scrollView = scrollViewInViewController(vc: vc) ?? internalScrollView
originalTopInset += diff

var offset = scrollView.contentOffset
var insets = scrollView.contentInset
insets.top = originalTopInset
offset.y -= diff
ignoreOffsetChanged = true
scrollView.contentInset = insets
scrollView.contentOffset = offset
applyMinimumContentHeight(for: scrollView)
}

@available(iOSApplicationExtension 9.0, *)
private func layoutInternalScrollView() {
insertSubview(internalScrollView, at: 0)
internalScrollView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -221,15 +228,16 @@ public final class ParallaxPagerView: UIView {
}

private func layoutContentViewControllers() {
if let firstVC = viewControllers.first {
let scrollView = scrollViewInViewController(vc: firstVC) ?? internalScrollView
layoutChildViewController(vc: firstVC, position: 0)
internalScrollView.layoutIfNeeded()
layoutSubviews()

addObserver(for: scrollView)
currentDisplayController = firstVC
guard let firstVC = viewControllers.first else {
return
}
let scrollView = scrollViewInViewController(vc: firstVC) ?? internalScrollView
layoutChildViewController(vc: firstVC, position: 0)
internalScrollView.layoutIfNeeded()
layoutSubviews()

addObserver(for: scrollView)
currentDisplayController = firstVC
}

private func initialLayoutsHeaderView() {
Expand Down Expand Up @@ -349,7 +357,7 @@ public final class ParallaxPagerView: UIView {
let isMovingDown = translation.y > 0.0;
let deltaOfOffsetY = newOffset.y - oldOffset.y

// Results from debuging, found that there some awkward values are coming randomly on scrolling.
// Results from debugging, found that there some awkward values are coming randomly on scrolling.
// This condition below guarantee that this value are eliminated.
if deltaOfOffsetY == 0.0 { return true }
if (isMovingDown && deltaOfOffsetY > 0.0 && newOffset.y > 0.0) { return true }
Expand Down Expand Up @@ -461,7 +469,7 @@ public final class ParallaxPagerView: UIView {
guard
let firstItem = constraint.firstItem as? NSObject,
let secondItem = constraint.secondItem as? NSObject
else { return false }
else { return false }
return (constraint.firstAttribute == .right &&
constraint.secondAttribute == .right &&
firstItem == view &&
Expand Down
4 changes: 1 addition & 3 deletions Sources/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ public protocol PagerDelegate {
}

public protocol PagerTab {
var onSelectedTabChanging:(_ oldTab: Int, _ newTab: Int) -> Void { set get }
var onSelectedTabChanging: (_ oldTab: Int, _ newTab: Int) -> Void { set get }
func currentSelectedIndex() -> Int
func numberOfTabs() -> Int
func setSelectedTab(at index: Int)
}


21 changes: 12 additions & 9 deletions Sources/Tabs/TabsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fileprivate class TabView: UIView {
selectedFont: UIFont,
height: CGFloat,
tabsView: TabsView
) {
) {
self.index = index
self.button = UIButton(type: .custom)
self.tabsView = tabsView
Expand All @@ -47,7 +47,7 @@ fileprivate class TabView: UIView {
let buttonFrame = button.frame
let frame = CGRect(x: 0, y: 0, width: buttonFrame.size.width, height: height)
super.init(frame: frame)
button.addTarget(self, action:#selector(tabClicked), for: .touchUpInside)
button.addTarget(self, action: #selector(tabClicked), for: .touchUpInside)
addSubview(button)
}

Expand Down Expand Up @@ -76,19 +76,22 @@ fileprivate class TabView: UIView {
}
}

class TabsView: UIView {
public class TabsView: UIView {

public var onSelectedTabChanging:(_ oldTab: Int, _ newTab: Int) -> Void = {_, _ in }
public var onSelectedTabChanging: (_ oldTab: Int, _ newTab: Int) -> Void = { _, _ in }

@IBOutlet fileprivate weak var scrollView: UIScrollView!
@IBOutlet fileprivate weak var scrollViewWidthConstraint: NSLayoutConstraint!
fileprivate let selectionIndicatorView = UIView()
fileprivate let selectionIndicatorView = UIView()
fileprivate var tabsConfig: TabsConfig!
fileprivate var tabsList = [TabView]()
fileprivate var tabsList = [TabView]()

private(set) var selectedIndex: Int = 0
private(set) var tabsHeaderView: UIView?

public static func tabsView(with config: TabsConfig) -> TabsView {

static func tabsView(with config: TabsConfig) -> TabsView {
let bundle = Bundle(identifier:"com.ParallaxPagerView.ParallaxPagerView-iOS")
let bundle = Bundle(identifier: "com.ParallaxPagerView.ParallaxPagerView-iOS")
let tabsView = bundle!.loadNibNamed("TabsView", owner: nil, options: nil)?.first as! TabsView
tabsView.tabsConfig = config
tabsView.frame = CGRect(x: 0, y: 0, width: tabsView.frame.size.width, height: config.height)
Expand Down Expand Up @@ -163,7 +166,7 @@ class TabsView: UIView {
width: selectedTabFrame.size.width,
height: tabsConfig.selectionIndicatorHeight
)
UIView.animate(withDuration: 0.3) {[weak self] in
UIView.animate(withDuration: 0.3) { [weak self] in
self?.selectionIndicatorView.frame = frame
}
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/Tabs/TabsView.xib
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="TabsView" customModule="ParallaxPagerView" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="375" height="70"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="172"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="n1r-Wd-YDF">
<rect key="frame" x="0.0" y="0.0" width="375" height="70"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="172"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</scrollView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="n1r-Wd-YDF" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="BbC-0O-mjc"/>
<constraint firstItem="n1r-Wd-YDF" firstAttribute="width" secondItem="iN0-l3-epB" secondAttribute="width" id="JhT-BR-54M"/>
<constraint firstAttribute="bottom" secondItem="n1r-Wd-YDF" secondAttribute="bottom" id="Kbb-iF-m30"/>
<constraint firstItem="n1r-Wd-YDF" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="Nwv-h6-mHY"/>
<constraint firstItem="n1r-Wd-YDF" firstAttribute="height" secondItem="iN0-l3-epB" secondAttribute="height" id="glT-ZL-7kx"/>
<constraint firstItem="n1r-Wd-YDF" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="R88-MJ-SXE"/>
</constraints>
<nil key="simulatedTopBarMetrics"/>
<nil key="simulatedBottomBarMetrics"/>
Expand All @@ -34,7 +34,7 @@
<outlet property="scrollView" destination="n1r-Wd-YDF" id="MEs-K7-nw6"/>
<outlet property="scrollViewWidthConstraint" destination="JhT-BR-54M" id="zUZ-CG-bFW"/>
</connections>
<point key="canvasLocation" x="-850" y="-939"/>
<point key="canvasLocation" x="-850.39999999999998" y="-985.00749625187416"/>
</view>
</objects>
</document>

0 comments on commit 0978def

Please sign in to comment.