Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - zooming callbacks #393

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion ImageSlideshow/Classes/Core/ImageSlideshow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ open class ImageSlideshow: UIView {

/// Image Slideshow Items loaded to slideshow
open fileprivate(set) var slideshowItems = [ImageSlideshowItem]()

/// Called on zoom of item.
open var onZoom: ((CGFloat) -> Void)?

/// Called on end of zoom.
open var onDidEndZooming: (() -> Void)?

/// Called on scroll of the scroll view
open var onDidScroll: ((_ contentOffset: CGPoint, _ contentSize: CGSize) -> Void)?

// MARK: - Preferences

Expand Down Expand Up @@ -320,7 +329,7 @@ open class ImageSlideshow: UIView {

var i = 0
for image in scrollViewImages {
let item = ImageSlideshowItem(image: image, zoomEnabled: zoomEnabled, activityIndicator: activityIndicator?.create(), maximumScale: maximumScale)
let item = ImageSlideshowItem(image: image, zoomEnabled: zoomEnabled, activityIndicator: activityIndicator?.create(), maximumScale: maximumScale, onZoom: onZoom, onDidEndZooming: onDidEndZooming)
item.imageView.contentMode = contentScaleMode
slideshowItems.append(item)
scrollView.addSubview(item)
Expand Down Expand Up @@ -590,9 +599,28 @@ extension ImageSlideshow: UIScrollViewDelegate {
if scrollView.isDragging {
pageIndicator?.page = currentPageForScrollViewPage(primaryVisiblePage)
}

self.onDidScroll?(scrollView.contentOffset, scrollView.contentSizePlusInsets)
}

public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
isAnimating = false
}
}

extension UIScrollView
{
var contentSizePlusInsets: CGSize
{
CGSize(
width: contentSize.width + adjustedContentInset.left + adjustedContentInset.right,
height: contentSize.height + adjustedContentInset.bottom + contentInset.top) // NOTE: the adjusted top inset intentionally left out, as SwiftUI uses a negative contentOffset to display the nav bar (doesn't affect content size)
}

var maxContentOffset: CGPoint
{
CGPoint(
x: max(0, contentSizePlusInsets.width - bounds.width),
y: max(0, contentSizePlusInsets.height + safeAreaInsets.top - bounds.height))
}
}
19 changes: 16 additions & 3 deletions ImageSlideshow/Classes/Core/ImageSlideshowItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {

/// Wraps around ImageView so RTL transformation on it doesn't interfere with UIScrollView zooming
private let imageViewWrapper = UIView()

/// Called when item is zoomed.
private let onZoom: ((CGFloat) -> Void)?

/// Called when item is zoom ends.
private let onDidEndZooming: (() -> Void)?

// MARK: - Life cycle

Expand All @@ -53,11 +59,13 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {
- parameter image: Input Source to load the image
- parameter zoomEnabled: holds if it should be possible to zoom-in the image
*/
init(image: InputSource, zoomEnabled: Bool, activityIndicator: ActivityIndicatorView? = nil, maximumScale: CGFloat = 2.0) {
init(image: InputSource, zoomEnabled: Bool, activityIndicator: ActivityIndicatorView? = nil, maximumScale: CGFloat = 2.0, onZoom: ((CGFloat) -> Void)? = nil, onDidEndZooming: (() -> Void)? = nil) {
self.zoomEnabled = zoomEnabled
self.image = image
self.activityIndicator = activityIndicator
self.maximumScale = maximumScale
self.onZoom = onZoom
self.onDidEndZooming = onDidEndZooming

super.init(frame: CGRect.null)

Expand Down Expand Up @@ -175,8 +183,8 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {
return self.zoomScale != self.minimumZoomScale
}

func zoomOut() {
self.setZoomScale(minimumZoomScale, animated: false)
public func zoomOut(_ animated: Bool = false) {
self.setZoomScale(minimumZoomScale, animated: animated)
}

func tapZoom() {
Expand Down Expand Up @@ -230,8 +238,13 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {
// MARK: UIScrollViewDelegate

open func scrollViewDidZoom(_ scrollView: UIScrollView) {
self.onZoom?(scrollView.zoomScale)
setPictoCenter()
}

open func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
self.onDidEndZooming?()
}

open func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return zoomEnabled ? imageViewWrapper : nil
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "ImageSlideshow",
platforms: [
.iOS(.v10),
.iOS(.v11),
],
products: [
.library(
Expand Down