Skip to content

Commit

Permalink
Merge pull request #157 from kozyty/master
Browse files Browse the repository at this point in the history
Add ItemView UIActivityViewController via longPress
  • Loading branch information
zfoltin authored Sep 29, 2017
2 parents 29cfd48 + 2532f74 commit 499e5d4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class ViewController: UIViewController {

GalleryConfigurationItem.swipeToDismissMode(.vertical),
GalleryConfigurationItem.toggleDecorationViewsBySingleTap(false),
GalleryConfigurationItem.activityViewByLongPress(false),

GalleryConfigurationItem.overlayColor(UIColor(white: 0.035, alpha: 1)),
GalleryConfigurationItem.overlayColorOpacity(1),
Expand Down
3 changes: 3 additions & 0 deletions ImageViewer/Source/GalleryConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public enum GalleryConfigurationItem {
///Allows to turn on/off decoration views hiding via single tap.
case toggleDecorationViewsBySingleTap(Bool)

///Allows to uiactivityviewcontroller with itemview via long press.
case activityViewByLongPress(Bool)

/// Allows you to select between different types of initial gallery presentation style
case presentationStyle(GalleryPresentationStyle)

Expand Down
18 changes: 18 additions & 0 deletions ImageViewer/Source/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import AVFoundation

open class GalleryViewController: UIPageViewController, ItemControllerDelegate {

Expand Down Expand Up @@ -652,6 +653,23 @@ open class GalleryViewController: UIPageViewController, ItemControllerDelegate {
self.decorationViewsHidden.flip()
animateDecorationViews(visible: !self.decorationViewsHidden)
}

open func itemControllerDidLongPress(_ controller: ItemController, in item: ItemView) {
switch (controller, item) {

case (_ as ImageViewController, let item as UIImageView):
guard let image = item.image else { return }
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.present(activityVC, animated: true)

case (_ as VideoViewController, let item as VideoView):
guard let videoUrl = ((item.player?.currentItem?.asset) as? AVURLAsset)?.url else { return }
let activityVC = UIActivityViewController(activityItems: [videoUrl], applicationActivities: nil)
self.present(activityVC, animated: true)

default: return
}
}

public func itemController(_ controller: ItemController, didSwipeToDismissWithDistanceToEdge distance: CGFloat) {

Expand Down
20 changes: 19 additions & 1 deletion ImageViewer/Source/ItemBaseController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ open class ItemBaseController<T: UIView>: UIViewController, ItemController, UIGe
fileprivate var displacementInsetMargin: CGFloat = 50
fileprivate var swipeToDismissMode = GallerySwipeToDismissMode.always
fileprivate var toggleDecorationViewBySingleTap = true
fileprivate var activityViewByLongPress = true

/// INTERACTIONS
fileprivate var singleTapRecognizer: UITapGestureRecognizer?
fileprivate var longPressRecognizer: UILongPressGestureRecognizer?
fileprivate let doubleTapRecognizer = UITapGestureRecognizer()
fileprivate let swipeToDismissRecognizer = UIPanGestureRecognizer()

Expand Down Expand Up @@ -84,6 +86,7 @@ open class ItemBaseController<T: UIView>: UIViewController, ItemController, UIGe
case .displacementInsetMargin(let margin): displacementInsetMargin = margin
case .swipeToDismissMode(let mode): swipeToDismissMode = mode
case .toggleDecorationViewsBySingleTap(let enabled): toggleDecorationViewBySingleTap = enabled
case .activityViewByLongPress(let enabled): activityViewByLongPress = enabled
case .spinnerColor(let color): activityIndicatorView.color = color
case .spinnerStyle(let style): activityIndicatorView.activityIndicatorViewStyle = style

Expand Down Expand Up @@ -154,6 +157,16 @@ open class ItemBaseController<T: UIView>: UIViewController, ItemController, UIGe
self.singleTapRecognizer = singleTapRecognizer
}

if activityViewByLongPress == true {

let longPressRecognizer = UILongPressGestureRecognizer()

longPressRecognizer.addTarget(self, action: #selector(scrollViewDidLongPress))
scrollView.addGestureRecognizer(longPressRecognizer)

self.longPressRecognizer = longPressRecognizer
}

if swipeToDismissMode != .never {

swipeToDismissRecognizer.addTarget(self, action: #selector(scrollViewDidSwipeToDismiss))
Expand Down Expand Up @@ -256,6 +269,11 @@ open class ItemBaseController<T: UIView>: UIViewController, ItemController, UIGe
self.delegate?.itemControllerDidSingleTap(self)
}

@objc func scrollViewDidLongPress() {

self.delegate?.itemControllerDidLongPress(self, in: itemView)
}

@objc func scrollViewDidDoubleTap(_ recognizer: UITapGestureRecognizer) {

let touchPoint = recognizer.location(ofTouch: 0, in: itemView)
Expand Down Expand Up @@ -527,7 +545,7 @@ open class ItemBaseController<T: UIView>: UIViewController, ItemController, UIGe
if UIApplication.isPortraitOnly == true {
self?.itemView.transform = deviceRotationTransform()
}

//position the image view to starting center
self?.itemView.bounds = displacedView.bounds
self?.itemView.center = displacedView.convert(displacedView.boundsCenter, to: self!.view)
Expand Down
1 change: 1 addition & 0 deletions ImageViewer/Source/ItemControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public protocol ItemControllerDelegate: class {
func itemControllerDidFinishSwipeToDismissSuccessfully()

func itemControllerDidSingleTap(_ controller: ItemController)
func itemControllerDidLongPress(_ controller: ItemController, in item: ItemView)

func itemControllerWillAppear(_ controller: ItemController)
func itemControllerWillDisappear(_ controller: ItemController)
Expand Down

0 comments on commit 499e5d4

Please sign in to comment.