Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge branch 'feature/add-preview'
Browse files Browse the repository at this point in the history
  • Loading branch information
medisean committed Aug 25, 2017
2 parents 98068b7 + a7471f8 commit 8a6f98b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 59 deletions.
Binary file modified PhotoBrowser/Images.xcassets/icon-share.imageset/icon_share.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions PhotoBrowser/Images.xcassets/moreIcon.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "moreIcon.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file not shown.
71 changes: 43 additions & 28 deletions PhotoBrowser/PBCustomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,20 @@ class WaitingView: UIView {

class PBNavigationBar: UIView {

var isFromPhotoPicker: Bool = false
var isPreviewMode: Bool = false {
didSet {
if isPreviewMode {
isFromPhotoPicker = true
rightButton.isHidden = true
}
}
}

var isFromPhotoPicker: Bool = false {
didSet {
self.updateMoreButtonStatus(isFromPhotoPicker)
}
}
var imageSelected: Bool = false {
didSet {
if isFromPhotoPicker {
Expand All @@ -90,13 +103,13 @@ class PBNavigationBar: UIView {
view.addSubview(self.leftButton)
view.addSubview(self.rightButton)
view.addSubview(self.titleLabel)
view.addSubview(self.indexLabel)

var titleLabelTrailingConstant: CGFloat = 60
if !self.isFromPhotoPicker {
view.addSubview(self.showSkitchButton)
view.addConstraint(NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: self.showSkitchButton, attribute: .centerY, multiplier: 1.0, constant: 0))
view.addConstraint(NSLayoutConstraint(item: self.showSkitchButton, attribute: .trailing, relatedBy: .equal, toItem: self.rightButton, attribute: .leading, multiplier: 1.0, constant: 0))
view.addSubview(self.moreButton)
view.addConstraint(NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: self.moreButton, attribute: .centerY, multiplier: 1.0, constant: 0))
self.moreTrailingConstraint = NSLayoutConstraint(item: self.moreButton, attribute: .trailing, relatedBy: .equal, toItem: self.rightButton, attribute: .leading, multiplier: 1.0, constant: 0)
view.addConstraint(self.moreTrailingConstraint!)
titleLabelTrailingConstant = 85
}

Expand All @@ -107,19 +120,19 @@ class PBNavigationBar: UIView {
view.addConstraint(NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: self.rightButton, attribute: .trailing, multiplier: 1.0, constant: 8))

view.addConstraint(NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self.titleLabel, attribute: .top, multiplier: 1.0, constant: 0))
view.addConstraint(NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: self.titleLabel, attribute: .bottom, multiplier: 1.0, constant: 0))
view.addConstraint(NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: self.titleLabel, attribute: .centerX, multiplier: 1.0, constant: 0))

view.addConstraint(NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: self.titleLabel, attribute: .leading, multiplier: 1, constant: -60))

self.titleTrailingConstraint = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: self.titleLabel, attribute: .trailing, multiplier: 1, constant: titleLabelTrailingConstant)
view.addConstraint(self.titleTrailingConstraint!)

view.addConstraint(NSLayoutConstraint(item: self.titleLabel, attribute: .bottom, relatedBy: .equal, toItem: self.indexLabel, attribute: .top, multiplier: 1.0, constant: -3))
view.addConstraint(NSLayoutConstraint(item: self.titleLabel, attribute: .centerX, relatedBy: .equal, toItem: self.indexLabel, attribute: .centerX, multiplier: 1.0, constant: 0))
return view
}()

var titleTrailingConstraint: NSLayoutConstraint?
var moreTrailingConstraint: NSLayoutConstraint?

lazy var titleLabel: UILabel = {
let label = UILabel()
Expand All @@ -130,15 +143,6 @@ class PBNavigationBar: UIView {
return label
}()

lazy var indexLabel: UILabel = {
let label = UILabel()
label.text = "Index"
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 14)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

lazy var leftButton: UIButton = {
let button = UIButton()
let image = UIImage(named: "icon-cross", in: Bundle(for: classForCoder()), compatibleWith: nil)
Expand All @@ -149,9 +153,9 @@ class PBNavigationBar: UIView {
return button
}()

lazy var showSkitchButton: UIButton = {
lazy var moreButton: UIButton = {
let button = UIButton()
var image = UIImage(named: "filePreviewVisibleIcon", in: Bundle(for: classForCoder()), compatibleWith: nil)
var image = UIImage(named: "moreIcon", in: Bundle(for: classForCoder()), compatibleWith: nil)
button.setImage(image, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addConstraint(NSLayoutConstraint(item: button, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40))
Expand Down Expand Up @@ -185,11 +189,22 @@ class PBNavigationBar: UIView {
super.init(coder: aDecoder)
}

func updateShowSkitchButtonStatus(_ isHidden: Bool, isHiddenSkitch: Bool) {
self.showSkitchButton.isHidden = isHidden
let skitchImage = isHiddenSkitch ? "filePreviewInvisibleIcon" : "filePreviewVisibleIcon"
let image = UIImage(named: skitchImage, in: Bundle(for: classForCoder), compatibleWith: nil)
showSkitchButton.setImage(image, for: .normal)
func updateShareStatus(_ isEnableShare: Bool) {
self.removeConstraint(self.moreTrailingConstraint!)
self.moreTrailingConstraint = NSLayoutConstraint(item: self.moreButton, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: -15)
self.addConstraint(self.moreTrailingConstraint!)

if !isEnableShare {
titleTrailingConstraint?.constant = 60
} else {
titleTrailingConstraint?.constant = 85
}
layoutIfNeeded()
}

func updateMoreButtonStatus(_ isHidden: Bool) {

self.moreButton.isHidden = isHidden
if isHidden {
titleTrailingConstraint?.constant = 60
} else {
Expand All @@ -198,11 +213,11 @@ class PBNavigationBar: UIView {
layoutIfNeeded()
}

func updateSkitchButton(_ isHiddenSkitch: Bool) {
let skitchImage = isHiddenSkitch ? "filePreviewInvisibleIcon" : "filePreviewVisibleIcon"
let image = UIImage(named: skitchImage, in: Bundle(for: classForCoder), compatibleWith: nil)
showSkitchButton.setImage(image, for: .normal)
}
// func updateSkitchButton(_ isHiddenSkitch: Bool) {
// let skitchImage = isHiddenSkitch ? "filePreviewInvisibleIcon" : "filePreviewVisibleIcon"
// let image = UIImage(named: skitchImage, in: Bundle(for: classForCoder), compatibleWith: nil)
// moreButton.setImage(image, for: .normal)
// }

func setup() {
addSubview(backgroundView)
Expand Down
38 changes: 25 additions & 13 deletions PhotoBrowser/PhotoBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ let PadToolbarItemSpace: CGFloat = 72

public protocol PhotoBrowserDelegate: class {
func dismissPhotoBrowser(_ photoBrowser: PhotoBrowser)
func longPressOnImage(_ gesture: UILongPressGestureRecognizer)
func photoBrowser(_ browser: PhotoBrowser, longPressOnPhoto photo: Photo, index: Int)
func photoBrowser(_ browser: PhotoBrowser, didShowPhotoAtIndex index: Int)
func photoBrowser(_ browser: PhotoBrowser, willSharePhoto photo: Photo)
func photoBrowser(_ browser: PhotoBrowser, canSelectPhotoAtIndex index: Int) -> Bool
func photoBrowser(_ browser: PhotoBrowser, didSelectPhotoAtIndex index: Int)
func photoBrowser(_ browser: PhotoBrowser, didTapSkitch skitch: Skitch, versionID: String)
func photoBrowser(_ browser: PhotoBrowser, didHideSkitchButton isHidden: Bool)
func didShowMoreFiles(_ browser: PhotoBrowser)
}

public extension PhotoBrowserDelegate {
func dismissPhotoBrowser(_ photoBrowser: PhotoBrowser) {
photoBrowser.dismiss(animated: false, completion: nil)
}
func longPressOnImage(_ gesture: UILongPressGestureRecognizer) {}
func photoBrowser(_ browser: PhotoBrowser, longPressOnPhoto photo: Photo, index: Int) {}
func photoBrowser(_ browser: PhotoBrowser, didShowPhotoAtIndex index: Int) {}
func photoBrowser(_ browser: PhotoBrowser, willSharePhoto photo: Photo) {
browser.defaultShareAction()
Expand All @@ -40,6 +41,7 @@ public extension PhotoBrowserDelegate {
func photoBrowser(_ browser: PhotoBrowser, didSelectPhotoAtIndex index: Int) {}
func photoBrowser(_ browser: PhotoBrowser, didTapSkitch skitch: Skitch, versionID: String) {}
func photoBrowser(_ browser: PhotoBrowser, didHideSkitchButton isHidden: Bool) {}
func didShowMoreFiles(_ browser: PhotoBrowser) {}
}

open class PhotoBrowser: UIPageViewController {
Expand Down Expand Up @@ -68,7 +70,7 @@ open class PhotoBrowser: UIPageViewController {
}
}

fileprivate var isSkitchButtonHidden: Bool = true
open var isSkitchButtonHidden: Bool = true
fileprivate var isSkitchesSetted: Bool = false

open var skitchesDictionary: [Int: [[String: Any]]] = [:]
Expand All @@ -91,6 +93,9 @@ open class PhotoBrowser: UIPageViewController {
open var enableShare = true {
didSet {
headerView?.rightButton.isHidden = !enableShare
if !enableShare {
headerView?.updateShareStatus(false)
}
}
}

Expand Down Expand Up @@ -118,6 +123,7 @@ open class PhotoBrowser: UIPageViewController {
}

open var isFromPhotoPicker: Bool = false
open var isPreviewMode: Bool = false
open var selectedIndex: [Int] = []

public override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]?) {
Expand All @@ -140,6 +146,7 @@ open class PhotoBrowser: UIPageViewController {
edgesForExtendedLayout = UIRectEdge.top
dataSource = self
delegate = self

self.updateToolbar()
}

Expand Down Expand Up @@ -230,6 +237,7 @@ extension PhotoBrowser {
headerView = PBNavigationBar()
if let headerView = headerView {
headerView.isFromPhotoPicker = isFromPhotoPicker
headerView.isPreviewMode = isPreviewMode
headerView.alpha = 0
view.addSubview(headerView)
headerView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -239,20 +247,20 @@ extension PhotoBrowser {

headerView.leftButton.addTarget(self, action: #selector(leftButtonTap(_:)), for: .touchUpInside)
headerView.rightButton.addTarget(self, action: #selector(rightButtonTap(_:)), for: .touchUpInside)
headerView.showSkitchButton.addTarget(self, action: #selector(showSkitchButtonTap(_:)), for: .touchUpInside)
headerView.moreButton.addTarget(self, action: #selector(showMoreFiles(_:)), for: .touchUpInside)
headerView.imageSelected = selectedIndex.contains(currentIndex)
}
}
if let headerView = headerView {
headerView.titleLabel.text = photos[currentIndex].title
headerView.indexLabel.text = "\(currentIndex + 1)/\(photos.count)"
// headerView.indexLabel.text = "\(currentIndex + 1)/\(photos.count)"
headerView.imageSelected = selectedIndex.contains(currentIndex)
}
if let skitches = skitchesDictionary[currentIndex], skitches.count > 0 {
headerView?.updateShowSkitchButtonStatus(false, isHiddenSkitch: isSkitchButtonHidden)
} else {
headerView?.updateShowSkitchButtonStatus(true, isHiddenSkitch: isSkitchButtonHidden)
}
// if let skitches = skitchesDictionary[currentIndex], skitches.count > 0 {
// headerView?.updateShowSkitchButtonStatus(false, isHiddenSkitch: isSkitchButtonHidden)
// } else {
// headerView?.updateShowSkitchButtonStatus(true, isHiddenSkitch: isSkitchButtonHidden)
// }
}

func updateToolbar() {
Expand Down Expand Up @@ -316,13 +324,17 @@ extension PhotoBrowser {
}
}

func showSkitchButtonTap(_ sender: Any) {
func showMoreFiles(_ sender: Any) {
photoBrowserDelegate?.didShowMoreFiles(self)
}

open func showSkitchButtonTapped() {
if isSkitchButtonHidden {
isSkitchButtonHidden = false
} else {
isSkitchButtonHidden = true
}
headerView?.updateSkitchButton(isSkitchButtonHidden)
// headerView?.updateSkitchButton(isSkitchButtonHidden)
if let previewController = viewControllers?[0] as? PhotoPreviewController {
previewController.updateSkitchButtonStatus(isSkitchButtonHidden)
}
Expand Down Expand Up @@ -442,7 +454,7 @@ extension PhotoBrowser: PhotoPreviewControllerDelegate {
}

func longPressOn(_ photo: Photo, gesture: UILongPressGestureRecognizer) {
photoBrowserDelegate?.longPressOnImage(gesture)
photoBrowserDelegate?.photoBrowser(self, longPressOnPhoto: photo, index: currentIndex)
}

func didTapOnBackground() {
Expand Down
30 changes: 12 additions & 18 deletions PhotoBrowserDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,29 @@ extension ViewController {
photoBrowser.photos = photos
}
let item2 = PBActionBarItem(title: "TWO", style: .plain) { (photoBrowser, item) in
photoBrowser.enableShare = !photoBrowser.enableShare
// photoBrowser.enableShare = !photoBrowser.enableShare
print("item2")
let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailViewController")
self.photoBrowser?.navigationController?.pushViewController(vc, animated: true)
}
let item3 = PBActionBarItem(title: "THREE", style: .plain) { (photoBrowser, item) in
print("item3")
}
let item4 = PBActionBarItem(title: "FOUR", style: .plain) { (photoBrowser, item) in
print("item3")
}


photoBrowser = PhotoBrowser()
if let browser = photoBrowser {
browser.isFromPhotoPicker = true
// browser.isFromPhotoPicker = true

browser.selectedIndex = [0, 1]
browser.photos = [photo, photo2, photo3]
browser.actionItems = [item1, item2, item3]
browser.actionItems = [item1, item2, item3, item4]
browser.photoBrowserDelegate = self
browser.currentIndex = 0
browser.enableShare = false
presentPhotoBrowser(browser, fromView: imageView)
}
}
Expand Down Expand Up @@ -113,29 +119,17 @@ extension ViewController: PhotoBrowserDelegate {
dismissPhotoBrowser(toView: imageView)
}

func longPressOnImage(_ gesture: UILongPressGestureRecognizer) {
guard let imageView = gesture.view as? UIImageView else {
return
}
func photoBrowser(_ browser: PhotoBrowser, longPressOnPhoto photo: Photo, index: Int) {
let alertController = UIAlertController.init(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
let cancelAction = UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
let saveAction = UIAlertAction.init(title: "Save", style: UIAlertActionStyle.default) {[unowned self] (action) -> Void in
if let image = imageView.image {
if let image = photo.imageToSave() {
self.saveToAlbum(image)
}
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
if UIDevice.current.userInterfaceIdiom == .phone {
self.photoBrowser?.present(alertController, animated: true, completion: nil)
} else {
let location = gesture.location(in: gesture.view)
let rect = CGRect(x: location.x - 5, y: location.y - 5, width: 10, height: 10)
alertController.modalPresentationStyle = .popover
alertController.popoverPresentationController?.sourceRect = rect
alertController.popoverPresentationController?.sourceView = gesture.view
self.photoBrowser?.present(alertController, animated: true, completion: nil)
}
self.photoBrowser?.present(alertController, animated: true, completion: nil)
}

func photoBrowser(_ browser: PhotoBrowser, willSharePhoto photo: Photo) {
Expand Down

0 comments on commit 8a6f98b

Please sign in to comment.