Skip to content

Commit 58cde37

Browse files
authored
Merge pull request Yummypets#471 from sanderdekoning/hideGrid
Add overlay type
2 parents f568ae1 + 7433815 commit 58cde37

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

Source/Configuration/YPImagePickerConfiguration.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ public struct YPConfigLibrary {
214214

215215
/// Allow to preselected media items
216216
public var preselectedItems: [YPMediaItem]?
217+
218+
/// Set the overlay type shown on top of the selected library item
219+
public var itemOverlayType: YPItemOverlayType = .grid
217220
}
218221

219222
/// Encapsulates video specific settings.
@@ -268,6 +271,11 @@ public struct YPConfigSelectionsGallery {
268271
public var hidesRemoveButton = true
269272
}
270273

274+
public enum YPItemOverlayType {
275+
case none
276+
case grid
277+
}
278+
271279
public enum YPlibraryMediaType {
272280
case photo
273281
case video

Source/Pages/Gallery/YPAssetViewContainer.swift

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import AVFoundation
1414
/// The container for asset (video or image). It containts the YPGridView and YPAssetZoomableView.
1515
class YPAssetViewContainer: UIView {
1616
public var zoomableView: YPAssetZoomableView?
17-
public let grid = YPGridView()
17+
public var itemOverlay: UIView?
1818
public let curtain = UIView()
1919
public let spinnerView = UIView()
2020
public let squareCropButton = UIButton()
@@ -26,12 +26,25 @@ class YPAssetViewContainer: UIView {
2626
private var shouldCropToSquare = YPConfig.library.isSquareByDefault
2727
private var isMultipleSelection = false
2828

29+
public var itemOverlayType = YPConfig.library.itemOverlayType
30+
2931
override func awakeFromNib() {
3032
super.awakeFromNib()
3133

32-
addSubview(grid)
33-
grid.frame = frame
34-
clipsToBounds = true
34+
switch itemOverlayType {
35+
case .grid:
36+
itemOverlay = YPGridView()
37+
default:
38+
break
39+
}
40+
41+
if let itemOverlay = itemOverlay {
42+
addSubview(itemOverlay)
43+
itemOverlay.frame = frame
44+
clipsToBounds = true
45+
46+
itemOverlay.alpha = 0
47+
}
3548

3649
for sv in subviews {
3750
if let cv = sv as? YPAssetZoomableView {
@@ -40,8 +53,6 @@ class YPAssetViewContainer: UIView {
4053
}
4154
}
4255

43-
grid.alpha = 0
44-
4556
let touchDownGR = UILongPressGestureRecognizer(target: self,
4657
action: #selector(handleTouchDown))
4758
touchDownGR.minimumPressDuration = 0
@@ -126,9 +137,11 @@ extension YPAssetViewContainer: YPAssetZoomableViewDelegate {
126137
public func ypAssetZoomableViewDidLayoutSubviews(_ zoomableView: YPAssetZoomableView) {
127138
let newFrame = zoomableView.assetImageView.convert(zoomableView.assetImageView.bounds, to: self)
128139

129-
// update grid position
130-
grid.frame = frame.intersection(newFrame)
131-
grid.layoutIfNeeded()
140+
if let itemOverlay = itemOverlay {
141+
// update grid position
142+
itemOverlay.frame = frame.intersection(newFrame)
143+
itemOverlay.layoutIfNeeded()
144+
}
132145

133146
// Update play imageView position - bringing the playImageView from the videoView to assetViewContainer,
134147
// but the controll for appearing it still in videoView.
@@ -139,16 +152,22 @@ extension YPAssetViewContainer: YPAssetZoomableViewDelegate {
139152
}
140153

141154
public func ypAssetZoomableViewScrollViewDidZoom() {
155+
guard let itemOverlay = itemOverlay else {
156+
return
157+
}
142158
if isShown {
143159
UIView.animate(withDuration: 0.1) {
144-
self.grid.alpha = 1
160+
itemOverlay.alpha = 1
145161
}
146162
}
147163
}
148164

149165
public func ypAssetZoomableViewScrollViewDidEndZooming() {
166+
guard let itemOverlay = itemOverlay else {
167+
return
168+
}
150169
UIView.animate(withDuration: 0.3) {
151-
self.grid.alpha = 0
170+
itemOverlay.alpha = 0
152171
}
153172
}
154173
}
@@ -166,16 +185,19 @@ extension YPAssetViewContainer: UIGestureRecognizerDelegate {
166185

167186
@objc
168187
private func handleTouchDown(sender: UILongPressGestureRecognizer) {
188+
guard let itemOverlay = itemOverlay else {
189+
return
190+
}
169191
switch sender.state {
170192
case .began:
171193
if isShown {
172194
UIView.animate(withDuration: 0.1) {
173-
self.grid.alpha = 1
195+
itemOverlay.alpha = 1
174196
}
175197
}
176198
case .ended:
177199
UIView.animate(withDuration: 0.3) {
178-
self.grid.alpha = 0
200+
itemOverlay.alpha = 0
179201
}
180202
default: ()
181203
}

Source/Pages/Gallery/YPLibraryVC.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public class YPLibraryVC: UIViewController, YPPermissionCheckable {
313313
delegate?.libraryViewStartedLoadingImage()
314314

315315
let completion = { (isLowResIntermediaryImage: Bool) in
316-
self.v.hideGrid()
316+
self.v.hideOverlayView()
317317
self.v.assetViewContainer.refreshSquareCropButton()
318318
self.updateCropInfo()
319319
if !isLowResIntermediaryImage {

Source/Pages/Gallery/YPLibraryView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ extension YPLibraryView {
9797
return xibView
9898
}
9999

100-
// MARK: - Grid
100+
// MARK: - Overlay view
101101

102-
func hideGrid() {
103-
assetViewContainer.grid.alpha = 0
102+
func hideOverlayView() {
103+
assetViewContainer.itemOverlay?.alpha = 0
104104
}
105105

106106
// MARK: - Loader and progress

YPImagePickerExample/YPImagePickerExample/ExampleViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ExampleViewController: UIViewController {
8181

8282
/* Choose what media types are available in the library. Defaults to `.photo` */
8383
config.library.mediaType = .photoAndVideo
84-
84+
config.library.itemOverlayType = .grid
8585
/* Enables selecting the front camera by default, useful for avatars. Defaults to false */
8686
// config.usesFrontCamera = true
8787

0 commit comments

Comments
 (0)