Skip to content

Commit

Permalink
Fix interaction issues when dynamically updating
Browse files Browse the repository at this point in the history
  • Loading branch information
msaps committed May 4, 2020
1 parent c32a8e5 commit 0847dae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
12 changes: 6 additions & 6 deletions Sources/Pageboy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/* Begin PBXBuildFile section */
460906B7222A785E005474BF /* WeakContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 460906B6222A785E005474BF /* WeakContainer.swift */; };
460906B9222A79F6005474BF /* IndexedObjectMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 460906B8222A79F6005474BF /* IndexedObjectMap.swift */; };
461D6DF1201795A100E0CDEE /* UIScrollView+ScrollActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461D6DF0201795A100E0CDEE /* UIScrollView+ScrollActivity.swift */; };
461D6DF2201795A100E0CDEE /* UIScrollView+ScrollActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461D6DF0201795A100E0CDEE /* UIScrollView+ScrollActivity.swift */; };
461D6DF1201795A100E0CDEE /* UIScrollView+Interaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461D6DF0201795A100E0CDEE /* UIScrollView+Interaction.swift */; };
461D6DF2201795A100E0CDEE /* UIScrollView+Interaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461D6DF0201795A100E0CDEE /* UIScrollView+Interaction.swift */; };
462A65DD2000FCAA0051C79C /* UIView+Localization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 462A65D92000FCAA0051C79C /* UIView+Localization.swift */; };
462A65DE2000FCAA0051C79C /* UIView+Localization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 462A65D92000FCAA0051C79C /* UIView+Localization.swift */; };
462A65DF2000FCAA0051C79C /* UIView+AutoLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 462A65DA2000FCAA0051C79C /* UIView+AutoLayout.swift */; };
Expand Down Expand Up @@ -85,7 +85,7 @@
/* Begin PBXFileReference section */
460906B6222A785E005474BF /* WeakContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakContainer.swift; sourceTree = "<group>"; };
460906B8222A79F6005474BF /* IndexedObjectMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IndexedObjectMap.swift; sourceTree = "<group>"; };
461D6DF0201795A100E0CDEE /* UIScrollView+ScrollActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIScrollView+ScrollActivity.swift"; sourceTree = "<group>"; };
461D6DF0201795A100E0CDEE /* UIScrollView+Interaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIScrollView+Interaction.swift"; sourceTree = "<group>"; };
462A65D92000FCAA0051C79C /* UIView+Localization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Localization.swift"; sourceTree = "<group>"; };
462A65DA2000FCAA0051C79C /* UIView+AutoLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+AutoLayout.swift"; sourceTree = "<group>"; };
462A65DB2000FCAA0051C79C /* UIApplication+SafeShared.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIApplication+SafeShared.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -160,7 +160,7 @@
462A65DA2000FCAA0051C79C /* UIView+AutoLayout.swift */,
462A65DB2000FCAA0051C79C /* UIApplication+SafeShared.swift */,
462A65DC2000FCAA0051C79C /* UIPageViewController+ScrollView.swift */,
461D6DF0201795A100E0CDEE /* UIScrollView+ScrollActivity.swift */,
461D6DF0201795A100E0CDEE /* UIScrollView+Interaction.swift */,
462D03DE2091D3AB0033C710 /* UIView+Animation.swift */,
BB5277382211308B003695BB /* DispatchQueue+main.swift */,
);
Expand Down Expand Up @@ -502,7 +502,7 @@
462A65E32000FCAA0051C79C /* UIPageViewController+ScrollView.swift in Sources */,
460906B9222A79F6005474BF /* IndexedObjectMap.swift in Sources */,
46ADAAC2208F7E8500974529 /* TransitionOperation.swift in Sources */,
461D6DF1201795A100E0CDEE /* UIScrollView+ScrollActivity.swift in Sources */,
461D6DF1201795A100E0CDEE /* UIScrollView+Interaction.swift in Sources */,
462A65DF2000FCAA0051C79C /* UIView+AutoLayout.swift in Sources */,
46ADAAC8208F7EB200974529 /* PageboyViewController+Updating.swift in Sources */,
BB5277392211308B003695BB /* DispatchQueue+main.swift in Sources */,
Expand Down Expand Up @@ -550,7 +550,7 @@
464ADF562097565D00929AFB /* Page.swift in Sources */,
46ADAAB9208F7E1500974529 /* PageboyViewController+AutoScrolling.swift in Sources */,
462A65E42000FCAA0051C79C /* UIPageViewController+ScrollView.swift in Sources */,
461D6DF2201795A100E0CDEE /* UIScrollView+ScrollActivity.swift in Sources */,
461D6DF2201795A100E0CDEE /* UIScrollView+Interaction.swift in Sources */,
46ADAAC3208F7E8500974529 /* TransitionOperation.swift in Sources */,
462A65E02000FCAA0051C79C /* UIView+AutoLayout.swift in Sources */,
46ADAAC9208F7EB200974529 /* PageboyViewController+Updating.swift in Sources */,
Expand Down
13 changes: 9 additions & 4 deletions Sources/Pageboy/PageboyViewController+Updating.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ internal extension PageboyViewController {
func performUpdates(for newIndex: PageIndex?,
viewController: UIViewController?,
update: (operation: UpdateOperation, behavior: PageUpdateBehavior),
indexOperation: (_ currentIndex: PageIndex, _ newIndex: PageIndex) -> Void) {
indexOperation: (_ currentIndex: PageIndex, _ newIndex: PageIndex) -> Void,
completion: ((Bool) -> Void)?) {
guard let newIndex = newIndex, let viewController = viewController else { // no view controller - reset
updateViewControllers(to: [UIViewController()],
animated: false,
async: false,
force: false,
completion: nil)
completion: completion)
self.currentIndex = nil
return
}
Expand All @@ -49,7 +50,7 @@ internal extension PageboyViewController {
animated: false,
async: false,
force: false,
completion: nil)
completion: completion)
self.currentIndex = newIndex
return
}
Expand All @@ -64,14 +65,15 @@ internal extension PageboyViewController {
animated: false,
async: true,
force: false,
completion: nil)
completion: completion)
})
} else { // update is happening on some other page.
indexOperation(currentIndex, newIndex)

// If we are deleting, check if the new index is greater than the current. If it is then we
// dont need to do anything...
if update.operation == .delete && newIndex > currentIndex {
completion?(true)
return
}

Expand All @@ -83,6 +85,7 @@ internal extension PageboyViewController {

case .insert:
guard let currentViewController = currentViewController else {
completion?(true)
return
}
newViewController = currentViewController
Expand All @@ -93,9 +96,11 @@ internal extension PageboyViewController {

updateViewControllers(to: [newViewController], animated: false, async: true, force: false, completion: { [weak self, newIndex, update] _ in
self?.performScrollUpdate(to: newIndex, behavior: update.behavior)
completion?(true)
})
} else { // Otherwise just perform scroll update
performScrollUpdate(to: newIndex, behavior: update.behavior)
completion?(true)
}
}
}
Expand Down
26 changes: 15 additions & 11 deletions Sources/Pageboy/PageboyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,17 @@ open class PageboyViewController: UIViewController {

viewControllerCount = newPageCount
viewControllerIndexMap.removeAll()

performUpdates(for: index,
viewController: newViewController,
update: (operation: .insert, behavior: updateBehavior),
indexOperation: { (index, newIndex) in

pageViewController?.scrollView?.cancelTouches()
view.isUserInteractionEnabled = false
performUpdates(for: index, viewController: newViewController,
update: (operation: .insert, behavior: updateBehavior),
indexOperation: { (index, newIndex) in
if index >= newIndex {
currentIndex = index + 1
}
}},
completion: { (_) in
self.view.isUserInteractionEnabled = true
})
})
}
Expand Down Expand Up @@ -329,14 +331,16 @@ open class PageboyViewController: UIViewController {
viewControllerCount = newPageCount
viewControllerIndexMap.removeAll()

performUpdates(for: newIndex,
viewController: newViewController,
pageViewController?.scrollView?.cancelTouches()
view.isUserInteractionEnabled = false
performUpdates(for: newIndex, viewController: newViewController,
update: (operation: .delete, behavior: updateBehavior),
indexOperation: { (index, newIndex) in

if index > newIndex {
currentIndex = index - 1
}
}},
completion: { (_) in
self.view.isUserInteractionEnabled = true
})
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// UIScrollView+ScrollActivity.swift
// UIScrollView+Interaction.swift
// Pageboy
//
// Created by Merrick Sapsford on 23/01/2018.
Expand All @@ -14,4 +14,9 @@ internal extension UIScrollView {
var isProbablyActiveInScroll: Bool {
return isTracking || isDecelerating
}

func cancelTouches() {
panGestureRecognizer.isEnabled = false
panGestureRecognizer.isEnabled = true
}
}

0 comments on commit 0847dae

Please sign in to comment.