From 08d9c508ac9b0efd772e5f6faccd7b62567dc4ae Mon Sep 17 00:00:00 2001 From: Alexsandersky Date: Mon, 29 Feb 2016 14:13:35 +0400 Subject: [PATCH 1/7] delete button via delegate --- SKPhotoBrowser/SKPhotoBrowser.swift | 49 +++++++++++++---------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/SKPhotoBrowser/SKPhotoBrowser.swift b/SKPhotoBrowser/SKPhotoBrowser.swift index f6a4cdd..10a68c4 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.swift +++ b/SKPhotoBrowser/SKPhotoBrowser.swift @@ -14,7 +14,7 @@ import UIKit optional func willShowActionSheet(photoIndex: Int) optional func didDismissAtPageIndex(index: Int) optional func didDismissActionSheetWithButtonIndex(buttonIndex: Int, photoIndex: Int) - optional func didDeleted(deletedIndex: [Int]) -> Void + optional func removePhoto(browser: SKPhotoBrowser, index: Int, reload: (() -> Void)) } public let SKPHOTO_LOADING_DID_END_NOTIFICATION = "photoLoadingDidEndNotification" @@ -447,6 +447,26 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe return CGSize(width: bounds.size.width * CGFloat(numberOfPhotos), height: bounds.size.height) } + // MARK: - delete function + @objc private func deleteButtonPressed(sender: UIButton) { + delegate?.removePhoto?(self, index: currentPageIndex, reload: { () -> Void in + self.deleteImage() + }) + } + + private func deleteImage() { + if photos.count > 1 { + photos.removeAtIndex(currentPageIndex) + if currentPageIndex != 0 { + gotoPreviousPage() + } + updateToolbar() + } else if photos.count == 1 { + dismissPhotoBrowser() + } + reloadData() + } + // MARK: - Toolbar public func updateToolbar() { if numberOfPhotos > 1 { @@ -633,7 +653,6 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe prepareForClosePhotoBrowser() dismissViewControllerAnimated(true) { self.delegate?.didDismissAtPageIndex?(self.currentPageIndex) - self.delegate?.didDeleted?(self.deleted) } } @@ -854,32 +873,6 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe } } - // MARK: - Button - public func deleteButtonPressed(sender: UIButton) { - let deleteAlert = UIAlertController(title: "Delete this photo?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet) - let deleteAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.Destructive, handler: deletePhoto) - deleteAlert.addAction(deleteAction) - let canelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) - deleteAlert.addAction(canelAction) - self.presentViewController(deleteAlert, animated: true, completion: nil) - } - - func deletePhoto(avc: UIAlertAction) -> Void { - guard let index = photos[currentPageIndex].index else { - return - } - deleted.append(index) - if photos.count == 1 { - dismissPhotoBrowser() - } else { - photos.removeAtIndex(currentPageIndex) - if currentPageIndex > 0 { - currentPageIndex = currentPageIndex - 1 - } - reloadData() - } - } - // MARK: Action Button public func actionButtonPressed() { let photo = photoAtIndex(currentPageIndex) From 5fa60321955ded23ae2064b030639b82602689a9 Mon Sep 17 00:00:00 2001 From: Alexsandersky Date: Mon, 29 Feb 2016 14:31:01 +0400 Subject: [PATCH 2/7] display delete button or no --- SKPhotoBrowser/SKPhotoBrowser.swift | 50 +++++++++++++++++++---------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/SKPhotoBrowser/SKPhotoBrowser.swift b/SKPhotoBrowser/SKPhotoBrowser.swift index 10a68c4..26a408e 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.swift +++ b/SKPhotoBrowser/SKPhotoBrowser.swift @@ -61,8 +61,9 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe private var doneButtonHideFrame: CGRect = CGRect(x: 5, y: -20, width: 44, height: 44) private var deleteButton: UIButton! - private var deleteButtonShowFrame: CGRect = CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: 5, width: 44, height: 44) - private var deleteButtonHideFrame: CGRect = CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: -20, width: 44, height: 44) + public var displayDeleteButton = false + private var deleteButtonShowFrame: CGRect! //= CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: 5, width: 44, height: 44) + private var deleteButtonHideFrame: CGRect! //= CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: -20, width: 44, height: 44) // photo's paging private var visiblePages: Set = Set() @@ -220,15 +221,7 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe view.addSubview(doneButton) // delete - let deleteImage = UIImage(named: "SKPhotoBrowser.bundle/images/btn_common_delete_wh", inBundle: bundle, compatibleWithTraitCollection: nil) ?? UIImage() - deleteButton = UIButton(type: UIButtonType.Custom) - deleteButton.setImage(deleteImage, forState: UIControlState.Normal) - deleteButton.frame = deleteButtonHideFrame - deleteButton.imageEdgeInsets = UIEdgeInsetsMake(15.25, 15.25, 15.25, 15.25) - deleteButton.backgroundColor = .clearColor() - deleteButton.addTarget(self, action: "deleteButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside) - deleteButton.alpha = 0.0 - view.addSubview(deleteButton) + setSettingDeleteButton() if !displayDelete { deleteButton.hidden = true @@ -293,6 +286,23 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe return areControlsHidden() } } + + // MARK: - setting of delete button + + private func setSettingDeleteButton() { + let bundle = NSBundle(forClass: SKPhotoBrowser.self) + if displayDeleteButton == true { + deleteButton = UIButton(type: .Custom) + deleteButtonShowFrame = CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: -5, width: 44, height: 44) + deleteButtonHideFrame = CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: -20, width: 44, height: 44) + let image = UIImage(named: "SKPhotoBrowser.bundle/images/btn_common_delete_wh", inBundle: bundle, compatibleWithTraitCollection: nil) ?? UIImage() + deleteButton.imageEdgeInsets = UIEdgeInsets(top: 15.25, left: 15.25, bottom: 15.25, right: 15.25) + deleteButton.setImage(image, forState: .Normal) + deleteButton.addTarget(self, action: "deleteButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside) + deleteButton.alpha = 0.0 + view.addSubview(deleteButton) + } + } // MARK: - notification public func handleSKPhotoLoadingDidEndNotification(notification: NSNotification) { @@ -591,8 +601,10 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe self.resizableImageView.frame = finalImageViewFrame self.doneButton.alpha = 1.0 self.doneButton.frame = self.doneButtonShowFrame - self.deleteButton.alpha = 1.0 - self.deleteButton.frame = self.deleteButtonShowFrame + if self.displayDeleteButton == true { + self.deleteButton.alpha = 1.0 + self.deleteButton.frame = self.deleteButtonShowFrame + } }, completion: { (Bool) -> Void in self.view.alpha = 1.0 @@ -611,8 +623,10 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe animations: { () -> Void in self.doneButton.alpha = 1.0 self.doneButton.frame = self.doneButtonShowFrame - self.deleteButton.alpha = 1.0 - self.deleteButton.frame = self.deleteButtonShowFrame + if self.displayDeleteButton == true { + self.deleteButton.alpha = 1.0 + self.deleteButton.frame = self.deleteButtonShowFrame + } }, completion: { (Bool) -> Void in self.view.alpha = 1.0 @@ -844,8 +858,10 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe self.toolBar.frame = hidden ? self.frameForToolbarHideAtOrientation() : self.frameForToolbarAtOrientation() self.doneButton.alpha = alpha self.doneButton.frame = hidden ? self.doneButtonHideFrame : self.doneButtonShowFrame - self.deleteButton.alpha = alpha - self.deleteButton.frame = hidden ? self.deleteButtonHideFrame : self.deleteButtonShowFrame + if displayDeleteButton == true { + self.deleteButton.alpha = alpha + self.deleteButton.frame = hidden ? self.deleteButtonHideFrame : self.deleteButtonShowFrame + } for v in captionViews { v.alpha = alpha } From 24a81bd07ed56de8dc9e618546c1698ea152d0fa Mon Sep 17 00:00:00 2001 From: Alexsandersky Date: Mon, 29 Feb 2016 14:36:14 +0400 Subject: [PATCH 3/7] change with displayDeleteButton --- SKPhotoBrowser/SKPhotoBrowser.swift | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/SKPhotoBrowser/SKPhotoBrowser.swift b/SKPhotoBrowser/SKPhotoBrowser.swift index 26a408e..5d5dbb4 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.swift +++ b/SKPhotoBrowser/SKPhotoBrowser.swift @@ -40,7 +40,7 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe public var displayBackAndForwardButton: Bool = true public var disableVerticalSwipe: Bool = false public var isForceStatusBarHidden: Bool = false - public var displayDelete: Bool = false + public var displayDeleteButton = false // actions private var actionSheet: UIActionSheet! @@ -61,7 +61,6 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe private var doneButtonHideFrame: CGRect = CGRect(x: 5, y: -20, width: 44, height: 44) private var deleteButton: UIButton! - public var displayDeleteButton = false private var deleteButtonShowFrame: CGRect! //= CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: 5, width: 44, height: 44) private var deleteButtonHideFrame: CGRect! //= CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: -20, width: 44, height: 44) @@ -222,10 +221,6 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe // delete setSettingDeleteButton() - - if !displayDelete { - deleteButton.hidden = true - } // action button toolActionButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "actionButtonPressed") @@ -858,7 +853,7 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe self.toolBar.frame = hidden ? self.frameForToolbarHideAtOrientation() : self.frameForToolbarAtOrientation() self.doneButton.alpha = alpha self.doneButton.frame = hidden ? self.doneButtonHideFrame : self.doneButtonShowFrame - if displayDeleteButton == true { + if self.displayDeleteButton == true { self.deleteButton.alpha = alpha self.deleteButton.frame = hidden ? self.deleteButtonHideFrame : self.deleteButtonShowFrame } From 9c6c154612b58c01c6a9a514bbff0b8677b1fdd5 Mon Sep 17 00:00:00 2001 From: Alexsandersky Date: Mon, 29 Feb 2016 14:53:10 +0400 Subject: [PATCH 4/7] change delete button frame because old frame hide the status bar --- SKPhotoBrowser/SKPhotoBrowser.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SKPhotoBrowser/SKPhotoBrowser.swift b/SKPhotoBrowser/SKPhotoBrowser.swift index 5d5dbb4..53ceafe 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.swift +++ b/SKPhotoBrowser/SKPhotoBrowser.swift @@ -288,8 +288,8 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe let bundle = NSBundle(forClass: SKPhotoBrowser.self) if displayDeleteButton == true { deleteButton = UIButton(type: .Custom) - deleteButtonShowFrame = CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: -5, width: 44, height: 44) - deleteButtonHideFrame = CGRect(x: UIScreen.mainScreen().bounds.size.width - 44 - 5, y: -20, width: 44, height: 44) + deleteButtonShowFrame = CGRect(x: view.frame.width - 39, y: 5, width: 44, height: 44) + deleteButtonHideFrame = CGRect(x: view.frame.width - 39, y: -20, width: 44, height: 44) let image = UIImage(named: "SKPhotoBrowser.bundle/images/btn_common_delete_wh", inBundle: bundle, compatibleWithTraitCollection: nil) ?? UIImage() deleteButton.imageEdgeInsets = UIEdgeInsets(top: 15.25, left: 15.25, bottom: 15.25, right: 15.25) deleteButton.setImage(image, forState: .Normal) From f2d11bcf2e609df89109efe46234614b0b21c87d Mon Sep 17 00:00:00 2001 From: Alexsandersky Date: Mon, 29 Feb 2016 15:04:52 +0400 Subject: [PATCH 5/7] different wrong zoom when double tap and pinch gesture. --- SKPhotoBrowser/SKZoomingScrollView.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SKPhotoBrowser/SKZoomingScrollView.swift b/SKPhotoBrowser/SKZoomingScrollView.swift index 412990c..ffe1bae 100644 --- a/SKPhotoBrowser/SKZoomingScrollView.swift +++ b/SKPhotoBrowser/SKZoomingScrollView.swift @@ -113,7 +113,7 @@ public class SKZoomingScrollView: UIScrollView, UIScrollViewDelegate, SKDetectin let xScale = boundsSize.width / imageSize.width let yScale = boundsSize.height / imageSize.height let minScale: CGFloat = min(xScale, yScale) - var maxScale: CGFloat = 4.0 + var maxScale: CGFloat = 0.6 maximumZoomScale = maxScale minimumZoomScale = minScale @@ -183,7 +183,7 @@ public class SKZoomingScrollView: UIScrollView, UIScrollViewDelegate, SKDetectin setZoomScale(minimumZoomScale, animated: true) } else { // zoom in - var newZoom: CGFloat = zoomScale * 2.0 + var newZoom: CGFloat = zoomScale * 4.0 if newZoom >= maximumZoomScale { newZoom = maximumZoomScale } From 9dd8af3399f7911d1ca688c7d0170287fb8fdcc0 Mon Sep 17 00:00:00 2001 From: Alexsandersky Date: Mon, 29 Feb 2016 15:17:41 +0400 Subject: [PATCH 6/7] changed UIPopoverController to UIPopoverPresentationController --- SKPhotoBrowser/SKPhotoBrowser.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/SKPhotoBrowser/SKPhotoBrowser.swift b/SKPhotoBrowser/SKPhotoBrowser.swift index 53ceafe..b82f2e4 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.swift +++ b/SKPhotoBrowser/SKPhotoBrowser.swift @@ -922,8 +922,10 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe if UI_USER_INTERFACE_IDIOM() == .Phone { presentViewController(activityViewController, animated: true, completion: nil) } else { - let popover = UIPopoverController(contentViewController: activityViewController) - popover.presentPopoverFromBarButtonItem(toolActionButton, permittedArrowDirections: .Any, animated: true) + activityViewController.modalPresentationStyle = .Popover + let popover: UIPopoverPresentationController! = activityViewController.popoverPresentationController + popover.barButtonItem = toolActionButton + presentViewController(activityViewController, animated: true, completion: nil) } } } From 09d45d2d5d672dc1b9b8b45455d31c038e153031 Mon Sep 17 00:00:00 2001 From: Alexsandersky Date: Mon, 29 Feb 2016 16:28:19 +0400 Subject: [PATCH 7/7] changed UIActionSheet to UIAlertController with ActionSheet style --- SKPhotoBrowser/SKPhotoBrowser.swift | 35 +++++++++++++---------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/SKPhotoBrowser/SKPhotoBrowser.swift b/SKPhotoBrowser/SKPhotoBrowser.swift index b82f2e4..42adbd4 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.swift +++ b/SKPhotoBrowser/SKPhotoBrowser.swift @@ -20,7 +20,7 @@ import UIKit public let SKPHOTO_LOADING_DID_END_NOTIFICATION = "photoLoadingDidEndNotification" // MARK: - SKPhotoBrowser -public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionSheetDelegate { +public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate { final let pageIndexTagOffset: Int = 1000 // animation property @@ -43,7 +43,6 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe public var displayDeleteButton = false // actions - private var actionSheet: UIActionSheet! private var activityViewController: UIActivityViewController! // tool for controls @@ -892,17 +891,24 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe if numberOfPhotos > 0 && photo.underlyingImage != nil { if let titles = actionButtonTitles { - actionSheet = UIActionSheet() - actionSheet.delegate = self + let actionSheetController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) + actionSheetController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in + })) for actionTitle in titles { - actionSheet.addButtonWithTitle(actionTitle) + actionSheetController.addAction(UIAlertAction(title: actionTitle, style: .Default, handler: { (action) -> Void in + + })) } - actionSheet.cancelButtonIndex = actionSheet.addButtonWithTitle("Cancel") - actionSheet.actionSheetStyle = .BlackTranslucent + if UI_USER_INTERFACE_IDIOM() == .Phone { - actionSheet.showInView(view) + presentViewController(actionSheetController, animated: true, completion: nil) } else { - actionSheet.showFromBarButtonItem(toolActionButton, animated: true) + actionSheetController.modalPresentationStyle = .Popover + let popoverController = actionSheetController.popoverPresentationController! + popoverController.barButtonItem = toolActionButton + presentViewController(actionSheetController, animated: true, completion: { () -> Void in + + }) } } else { var activityItems: [AnyObject] = [photo.underlyingImage] @@ -932,17 +938,6 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe } - // MARK: UIActionSheetDelegate - public func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) { - if actionSheet == self.actionSheet { - self.actionSheet = nil - - if buttonIndex != actionSheet.cancelButtonIndex { - self.delegate?.didDismissActionSheetWithButtonIndex?(buttonIndex, photoIndex: currentPageIndex) - } - } - } - // MARK: - UIScrollView Delegate public func scrollViewDidScroll(scrollView: UIScrollView) { guard isViewActive else {