Skip to content

Commit

Permalink
Merge pull request #33 from DeluxeAlonso/development
Browse files Browse the repository at this point in the history
Development to master for 3.0.0
  • Loading branch information
DeluxeAlonso authored Apr 28, 2023
2 parents 84b12b9 + 7f981e0 commit 0f50ede
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DLAutoSlidePageViewController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'DLAutoSlidePageViewController'
s.version = '2.1.0'
s.version = '3.0.0'
s.summary = 'An auto slide PageViewController.'

s.description = <<-DESC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
INFOPLIST_FILE = "$(SRCROOT)/DLAutoSlidePageViewController/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.alonso.DLAutoSlidePageViewControllera;
PRODUCT_BUNDLE_IDENTIFIER = com.alonso.DLAutoSlidePageViewControllers;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 1;
Expand All @@ -263,7 +263,7 @@
INFOPLIST_FILE = "$(SRCROOT)/DLAutoSlidePageViewController/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.alonso.DLAutoSlidePageViewControllera;
PRODUCT_BUNDLE_IDENTIFIER = com.alonso.DLAutoSlidePageViewControllers;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 1;
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pod "DLAutoSlidePageViewController"
To integrate using [Swift Package Manager](https://swift.org/package-manager/), add the following as a dependency to your Package.swift:

```Swift
.package(url: "https://github.com/DeluxeAlonso/DLAutoSlidePageViewController.git", .upToNextMajor(from: "1.2.0"))
.package(url: "https://github.com/DeluxeAlonso/DLAutoSlidePageViewController.git", .upToNextMajor(from: "3.0.0"))
```

## Usage
Expand Down Expand Up @@ -101,6 +101,8 @@ let pageViewController = DLAutoSlidePageViewController(pages: pages, configurati
| pageControlDirection | UIPageControl.Direction | Decribes the layout direction of a page control’s indicators. Available since iOS 16. |
| pageControlPreferredIndicatorImage | UIImage | The preferred image for indicators. Symbol images are recommended. Default is nil. Available since iOS 14. |
| pageControlPreferredCurrentPageIndicatorImage | UIImage | The preferred image for the current page indicator. Available since iOS 16. |
| shouldSlideOnTap | Bool | Indicates if the page controller should slide back/forward when the users taps on the left/right side. |
| tappableAreaPercentage | Float | Tappable area percentage used to detect taps on both sides: left and right. Defaults to 20%. Only used if shouldSlideOnTap is set to true. |

## Author

Expand Down
9 changes: 9 additions & 0 deletions Sources/AutoSlideConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public protocol AutoSlideConfiguration {
/// Indicates whether the automatic transition is to be animated.
var shouldAnimateTransition: Bool { get }

/// Indicates if the page controller should slide back/forward when the users taps on the left/right side.
var shouldSlideOnTap: Bool { get }

/// Tappable area percentage used to detect taps on both sides: left and right. Defaults to 20%. Only used if shouldSlideOnTap is set to true.
var tappableAreaPercentage: Float { get }

}

// MARK: - Default values
Expand All @@ -83,4 +89,7 @@ public extension AutoSlideConfiguration {

var shouldAnimateTransition: Bool { true }

var shouldSlideOnTap: Bool { true }
var tappableAreaPercentage: Float { 20 }

}
60 changes: 49 additions & 11 deletions Sources/DLAutoSlidePageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ open class DLAutoSlidePageViewController: UIPageViewController {
private var nextPageIndex: Int = 0
private var timer: Timer?

private var transitionInProgress: Bool = false
private var transitionInProgress: Bool = false {
didSet {
view.isUserInteractionEnabled = !transitionInProgress
}
}

// MARK: - Computed properties

Expand Down Expand Up @@ -85,6 +89,16 @@ open class DLAutoSlidePageViewController: UIPageViewController {
delegate = self
dataSource = self
setupObservers()

if configuration.shouldSlideOnTap {
setupTapGesture()
}
}

private func setupTapGesture() {
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapGestureHandler))
gestureRecognizer.delegate = self
view.addGestureRecognizer(gestureRecognizer)
}

// MARK: - Private
Expand Down Expand Up @@ -143,8 +157,31 @@ open class DLAutoSlidePageViewController: UIPageViewController {
setupPageTimer(with: configuration.timeInterval)
}

private func movePage(with navigationDirection: NavigationDirection, animated: Bool) {
currentPageIndex = AutoSlideHelper.pageIndex(for: currentPageIndex,
totalPageCount: pages.count,
direction: navigationDirection)
guard let viewController = viewControllerAtIndex(currentPageIndex) as UIViewController? else { return }
if !transitionInProgress {
transitionInProgress = true
setViewControllers([viewController], direction: navigationDirection, animated: animated, completion: { finished in
self.transitionInProgress = false
})
}
}

// MARK: - Selectors

@objc private func tapGestureHandler(_ sender: UITapGestureRecognizer) {
let point = sender.location(in: self.view)
let tappableArea = view.frame.width * (CGFloat(configuration.tappableAreaPercentage) / 100)
if point.x <= tappableArea { // Tap on left side
movePage(with: .reverse, animated: true)
} else if point.x >= view.frame.width - tappableArea { // Tap on right side
movePage(with: .forward, animated: true)
}
}

@objc private func movedToForeground() {
transitionInProgress = false
restartTimer()
Expand All @@ -153,16 +190,7 @@ open class DLAutoSlidePageViewController: UIPageViewController {
@objc private func changePage() {
let navigationDirection = configuration.navigationDirection
let shouldAnimateTransition = configuration.shouldAnimateTransition
currentPageIndex = AutoSlideHelper.pageIndex(for: currentPageIndex,
totalPageCount: pages.count,
direction: navigationDirection)
guard let viewController = viewControllerAtIndex(currentPageIndex) as UIViewController? else { return }
if !transitionInProgress {
transitionInProgress = true
setViewControllers([viewController], direction: navigationDirection, animated: shouldAnimateTransition, completion: { finished in
self.transitionInProgress = false
})
}
movePage(with: navigationDirection, animated: shouldAnimateTransition)
}

}
Expand Down Expand Up @@ -223,3 +251,13 @@ extension DLAutoSlidePageViewController: UIPageViewControllerDataSource {
}

}

// MARK: - UIGestureRecognizerDelegate

extension DLAutoSlidePageViewController: UIGestureRecognizerDelegate {

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

}
3 changes: 3 additions & 0 deletions Sources/DefaultAutoSlideConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ final public class DefaultAutoSlideConfiguration: AutoSlideConfiguration {
public var pageControlBackgroundColor: UIColor = UIColor.clear

public var shouldAnimateTransition: Bool = true

public var shouldSlideOnTap: Bool = false
public var tappableAreaPercentage: Float = 20

}

0 comments on commit 0f50ede

Please sign in to comment.