Skip to content

Commit

Permalink
Merge pull request #270 from FelixII/swipe-to-dismiss
Browse files Browse the repository at this point in the history
Allow swipe to dismiss the image picker
  • Loading branch information
mikaoj authored Jun 29, 2020
2 parents db79bbf + ca61469 commit b6e85e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions BSImagePicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
94DA6870247BDE5900CD5251 /* UIColor+BSImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94DA686F247BDE5900CD5251 /* UIColor+BSImagePicker.swift */; };
C2DC13CA23F75BDB0035FD13 /* NumberView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2DC13C923F75BDA0035FD13 /* NumberView.swift */; };
C2DC13CC23F75BE40035FD13 /* SelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2DC13CB23F75BE40035FD13 /* SelectionView.swift */; };
DCB6D3A42486A68D0057A2D4 /* ImagePickerController+PresentationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCB6D3A32486A68D0057A2D4 /* ImagePickerController+PresentationDelegate.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -130,6 +131,7 @@
94DA686F247BDE5900CD5251 /* UIColor+BSImagePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+BSImagePicker.swift"; sourceTree = "<group>"; };
C2DC13C923F75BDA0035FD13 /* NumberView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NumberView.swift; sourceTree = "<group>"; };
C2DC13CB23F75BE40035FD13 /* SelectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectionView.swift; sourceTree = "<group>"; };
DCB6D3A32486A68D0057A2D4 /* ImagePickerController+PresentationDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ImagePickerController+PresentationDelegate.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -264,6 +266,7 @@
559DB81621E6AFD800CD58B4 /* ImagePickerController+Assets.swift */,
559DB81821E6AFF300CD58B4 /* ImagePickerController+Albums.swift */,
559DB81A21E6B43400CD58B4 /* ImagePickerController+ButtonActions.swift */,
DCB6D3A32486A68D0057A2D4 /* ImagePickerController+PresentationDelegate.swift */,
559DB80E21E655D000CD58B4 /* ImagePickerControllerDelegate.swift */,
);
path = Controller;
Expand Down Expand Up @@ -476,6 +479,7 @@
559DB80F21E655D000CD58B4 /* ImagePickerControllerDelegate.swift in Sources */,
559DB81721E6AFD800CD58B4 /* ImagePickerController+Assets.swift in Sources */,
55CDB45B223435420050D572 /* PlayerView.swift in Sources */,
DCB6D3A42486A68D0057A2D4 /* ImagePickerController+PresentationDelegate.swift in Sources */,
C2DC13CC23F75BE40035FD13 /* SelectionView.swift in Sources */,
559DB81921E6AFF300CD58B4 /* ImagePickerController+Albums.swift in Sources */,
55BCF8D721D52C1000386752 /* CheckmarkView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// The MIT License (MIT)
//
// Copyright (c) 2020 Felix Lisczyk
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import UIKit

extension ImagePickerController: UIAdaptivePresentationControllerDelegate {

@available(iOS 13, *)
public func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
return settings.dismiss.enabled && settings.dismiss.allowSwipe
}

// This method is only called if
// - the presented view controller is not dismissed programmatically and
// - its `isModalInPresentation` property is set to false.
@available(iOS 13, *)
public func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
imagePickerDelegate?.imagePicker(self, didCancelWithAssets: assetStore.assets)
}
}
8 changes: 3 additions & 5 deletions Sources/Controller/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ import Photos

public override func viewDidLoad() {
super.viewDidLoad()

if #available(iOS 13.0, *) {
// Disables iOS 13 swipe to dismiss - to force user to press cancel or done.
isModalInPresentation = true
}

// Sync settings
albumsViewController.settings = settings
Expand All @@ -103,7 +98,10 @@ import Photos

viewControllers = [assetsViewController]
view.backgroundColor = settings.theme.backgroundColor

// Setup delegates
delegate = zoomTransitionDelegate
presentationController?.delegate = self

// Turn off translucency so drop down can match its color
navigationBar.isTranslucent = false
Expand Down
3 changes: 3 additions & 0 deletions Sources/Model/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ import Photos
public class Dismiss : NSObject {
/// Should the image picker dismiss when done/cancelled
public lazy var enabled = true

/// Allow the user to dismiss the image picker by swiping down
public lazy var allowSwipe = false
}

/// Theme settings
Expand Down

0 comments on commit b6e85e1

Please sign in to comment.