Skip to content

Commit

Permalink
Merge pull request #6 from tamanyan/feature-swipe-gesture
Browse files Browse the repository at this point in the history
Enable page swiping along with UITableView scrolling
  • Loading branch information
tamanyan authored Nov 1, 2017
2 parents 6897543 + a6f508c commit d0bd134
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 24 deletions.
2 changes: 1 addition & 1 deletion PageMenuExample/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// AppDelegate.swift
// PagerExample
// PageMenuExample
//
// Created by Tamanyan on 3/7/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
33 changes: 27 additions & 6 deletions PageMenuExample/Sources/ChildViewController.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
//
// ChildViewController.swift
// PagerExample
// PageMenuExample
//
// Created by Tamanyan on 3/7/29 H.
// Copyright © 29 Heisei Tamanyan. All rights reserved.
//

import UIKit

class ChildViewController: UITableViewController {
class SwiftPageMenuTableView: UITableView, UIGestureRecognizerDelegate {

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return otherGestureRecognizer.isKind(of: UIPanGestureRecognizer.self)
}
}

class ChildViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var tableView: UITableView = SwiftPageMenuTableView()

var fruits = [String]()

Expand All @@ -25,26 +34,38 @@ class ChildViewController: UITableViewController {
super.viewDidLoad()
self.edgesForExtendedLayout = []
self.view.backgroundColor = .white
self.view.addSubview(self.tableView)

self.tableView.translatesAutoresizingMaskIntoConstraints = false
self.tableView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
self.tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
self.tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
self.tableView.panGestureRecognizer.delaysTouchesBegan = true

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.reloadData()
}

// MARK: - UITableViewDataSource

override func numberOfSections(in tableView: UITableView) -> Int {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.fruits.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = self.fruits[indexPath.row]
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
5 changes: 3 additions & 2 deletions PageMenuExample/Sources/PageTabMenuViewController.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// PageTabMenuViewController.swift
// PagerExample
// PageMenuExample
//
// Created by Tamanyan on 3/7/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down Expand Up @@ -72,9 +72,10 @@ extension PageTabMenuViewController: PageMenuControllerDelegate {

func pageMenuController(_ pageMenuController: PageMenuController, scrollingProgress progress: CGFloat, direction: PageMenuNavigationDirection) {
// The page view controller did complete scroll to a new page.
print("scrollingProgress progress: \(progress)")
}

func pageMenuController(_ pageMenuController: PageMenuController, didSelectMenuItem index: Int, direction: PageMenuNavigationDirection) {
print("didSelectMenuItem \(index)")
print("didSelectMenuItem index: \(index)")
}
}
2 changes: 1 addition & 1 deletion PageMenuExample/Sources/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RootViewController: UITableViewController, UIGestureRecognizerDelegate {
let items: [[String]] = [
["Apple", "Apricot", "Avocado", "Banana", "Blackberry"],
["Blueberry", "Cantaloupe", "Cherry", "Cherimoya", "Clementine", "Coconut", "Cranberry", "Cucumber",
"Custard apple", "Damson", "Date", "Dragonfruit", "Durian", "Elderberry", "Feijoa"],
"Custard apple", "Damson", "Date", "Dragonfruit", "Durian", "Elderberry", "Feijoa",],
["Fig", "Grape", "Grapefruit", "Guava", "Udara", "Honeyberry", "Huckleberry", "Jabuticaba"],
["Jackfruit", "Juniper berry", "Kiwi fruit", "Lemon", "Lime", "Lychee", "Mandarine",],
["Mango", "Marionberry"],
Expand Down
2 changes: 1 addition & 1 deletion PageMenuExample/Sources/RoundRectPagerOptions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// RoundRectPageMenuOptions.swift
// PagerExample
// PageMenuExample
//
// Created by Tamanyan on 3/10/29 H.
// Copyright © 29 Heisei Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion PageMenuExample/Sources/Theme.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Theme.swift
// PagerExample
// PageMenuExample
//
// Created by Tamanyan on 3/10/29 H.
// Copyright © 29 Heisei Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion PageMenuExample/Sources/UnderlinePagerOption.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// UnderlinePagerOption.swift
// PagerExample
// PageMenuExample
//
// Created by Tamanyan on 3/10/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Enum/PageMenuOptions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// PageMenuOptions.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/9/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/PageMenuController.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// PageMenuController.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/9/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/PageMenuControllerDataSource.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// PageMenuControllerDataSource.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/9/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/PageMenuControllerDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// PageMenuControllerDelegate.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/9/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/TabMenu/RoundRectCursorView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// RoundRectCursorView.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/10/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/TabMenu/TabMenuItemCell.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// TabMenuItemCell.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/9/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/TabMenu/TabMenuItemCursor.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// TabMenuItemCursor.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/10/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/TabMenu/TabMenuView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// TabMenuView.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/9/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/TabMenu/UnderlineCursorView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// UnderlineCursorView.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/10/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
6 changes: 6 additions & 0 deletions Sources/Util/EMPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class EMPageViewController: UIViewController, UIScrollViewDelegate {
/// - important: Properties should be set with caution to prevent unexpected behavior.
open private(set) lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()

scrollView.isPagingEnabled = true
scrollView.scrollsToTop = false
scrollView.autoresizingMask = [.flexibleTopMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleLeftMargin]
Expand All @@ -152,6 +153,11 @@ class EMPageViewController: UIViewController, UIScrollViewDelegate {
scrollView.translatesAutoresizingMaskIntoConstraints = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false

let panBlockGestureRecognizer = PanBlockGestureRecognizer(in: self.view)
scrollView.addGestureRecognizer(panBlockGestureRecognizer)
scrollView.panGestureRecognizer.require(toFail: panBlockGestureRecognizer)

return scrollView
}()

Expand Down
63 changes: 63 additions & 0 deletions Sources/Util/PanBlockGestureRecognizer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// PanBlockGestureRecognizer.swift
// SwiftPageMenu
//
// Created by Taketo Yoshida on 11/1/29 H.
// Copyright © 29 Heisei Tamanyan. All rights reserved.
//

import UIKit

class PanBlockGestureRecognizerDelegate: NSObject, UIGestureRecognizerDelegate {

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

class PanBlockGestureRecognizer: UIPanGestureRecognizer {

fileprivate var panBlockGestureRecognizerDelegate = PanBlockGestureRecognizerDelegate()

var inView: UIView?

var isEnded: Bool = false

var beginPanLocation: CGPoint = .zero

var endPanLocation: CGPoint = .zero

init(in view: UIView) {
super.init(target: self, action: #selector(self.performAction(sender:)))

self.inView = view
self.delegate = self.panBlockGestureRecognizerDelegate
}

func performAction(sender: UIGestureRecognizer) {
guard let panGesture = sender as? UIPanGestureRecognizer else { return }
let state = panGesture.state
let panLocation = panGesture.location(in: self.inView)
let permissionVertical: CGFloat = 10
let swipeStroke: CGFloat = 10

if state == .began {
self.beginPanLocation = panLocation
self.isEnded = false
} else if state == .changed && self.isEnded == false {
self.endPanLocation = panLocation
let moveX = self.endPanLocation.x - self.beginPanLocation.x
let moveY = self.endPanLocation.y - self.beginPanLocation.y
let absX = abs(moveX)
let absY = abs(moveY)

if absY < permissionVertical && absX > swipeStroke {
panGesture.setValue(UIGestureRecognizerState.cancelled.rawValue, forKey: "state")
self.isEnded = true
} else if absY > permissionVertical {
self.isEnded = true
panGesture.setValue(UIGestureRecognizerState.ended.rawValue, forKey: "state")
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/Util/UIColor+Extension.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// UIColor+Extension.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/10/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Util/UIScrollView+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// UIScrollView+Extensions.swift
// SwiftPager
// SwiftPageMenu
//
// Created by Tamanyan on 3/10/17.
// Copyright © 2017 Tamanyan. All rights reserved.
Expand Down
4 changes: 4 additions & 0 deletions SwiftPageMenu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
23C1D2071E73059500A4A491 /* UIColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23C1D1FB1E73059500A4A491 /* UIColor+Extension.swift */; };
23C1D2081E73059500A4A491 /* UIScrollView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23C1D1FC1E73059500A4A491 /* UIScrollView+Extensions.swift */; };
23C1D26B1E73175100A4A491 /* PageMenuNavigationDirection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23C1D26A1E73175100A4A491 /* PageMenuNavigationDirection.swift */; };
AB39A18D1FA99B91005E8C2C /* PanBlockGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB39A18C1FA99B91005E8C2C /* PanBlockGestureRecognizer.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -40,6 +41,7 @@
23C1D1FB1E73059500A4A491 /* UIColor+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+Extension.swift"; sourceTree = "<group>"; };
23C1D1FC1E73059500A4A491 /* UIScrollView+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIScrollView+Extensions.swift"; sourceTree = "<group>"; };
23C1D26A1E73175100A4A491 /* PageMenuNavigationDirection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PageMenuNavigationDirection.swift; sourceTree = "<group>"; };
AB39A18C1FA99B91005E8C2C /* PanBlockGestureRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PanBlockGestureRecognizer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -119,6 +121,7 @@
23C1D1FA1E73059500A4A491 /* EMPageViewController.swift */,
23C1D1FB1E73059500A4A491 /* UIColor+Extension.swift */,
23C1D1FC1E73059500A4A491 /* UIScrollView+Extensions.swift */,
AB39A18C1FA99B91005E8C2C /* PanBlockGestureRecognizer.swift */,
);
path = Util;
sourceTree = "<group>";
Expand Down Expand Up @@ -212,6 +215,7 @@
23C1D2001E73059500A4A491 /* PageMenuControllerDelegate.swift in Sources */,
23C1D2021E73059500A4A491 /* TabMenuItemCell.swift in Sources */,
23C1D2041E73059500A4A491 /* TabMenuView.swift in Sources */,
AB39A18D1FA99B91005E8C2C /* PanBlockGestureRecognizer.swift in Sources */,
23C1D1FD1E73059500A4A491 /* PageMenuOptions.swift in Sources */,
23C1D1FF1E73059500A4A491 /* PageMenuControllerDataSource.swift in Sources */,
23C1D1FE1E73059500A4A491 /* PageMenuController.swift in Sources */,
Expand Down

0 comments on commit d0bd134

Please sign in to comment.