Merge pull request #34 from alexsanderskywork/master

@alexsanderskywork , Looks great. I'll update readme for this.
This commit is contained in:
keishi suzuki 2016-03-01 10:39:51 +09:00
commit e21623d13b
2 changed files with 75 additions and 74 deletions

View File

@ -14,13 +14,13 @@ 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"
// MARK: - SKPhotoBrowser
public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionSheetDelegate {
public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate {
final let pageIndexTagOffset: Int = 1000
// animation property
@ -40,10 +40,9 @@ 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!
private var activityViewController: UIActivityViewController!
// tool for controls
@ -61,8 +60,8 @@ 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)
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<SKZoomingScrollView> = Set()
@ -220,19 +219,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)
if !displayDelete {
deleteButton.hidden = true
}
setSettingDeleteButton()
// action button
toolActionButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "actionButtonPressed")
@ -293,6 +280,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: 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)
deleteButton.addTarget(self, action: "deleteButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
deleteButton.alpha = 0.0
view.addSubview(deleteButton)
}
}
// MARK: - notification
public func handleSKPhotoLoadingDidEndNotification(notification: NSNotification) {
@ -447,6 +451,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 {
@ -571,8 +595,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
@ -591,8 +617,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
@ -633,7 +661,6 @@ public class SKPhotoBrowser: UIViewController, UIScrollViewDelegate, UIActionShe
prepareForClosePhotoBrowser()
dismissViewControllerAnimated(true) {
self.delegate?.didDismissAtPageIndex?(self.currentPageIndex)
self.delegate?.didDeleted?(self.deleted)
}
}
@ -825,8 +852,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 self.displayDeleteButton == true {
self.deleteButton.alpha = alpha
self.deleteButton.frame = hidden ? self.deleteButtonHideFrame : self.deleteButtonShowFrame
}
for v in captionViews {
v.alpha = alpha
}
@ -854,32 +883,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)
@ -888,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]
@ -918,25 +928,16 @@ 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)
}
}
}
}
// 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 {

View File

@ -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
}