diff --git a/PhotoBrowser/Images.xcassets/icon-share.imageset/icon_share.pdf b/PhotoBrowser/Images.xcassets/icon-share.imageset/icon_share.pdf index 468df4e..0e45678 100644 Binary files a/PhotoBrowser/Images.xcassets/icon-share.imageset/icon_share.pdf and b/PhotoBrowser/Images.xcassets/icon-share.imageset/icon_share.pdf differ diff --git a/PhotoBrowser/Images.xcassets/moreIcon.imageset/Contents.json b/PhotoBrowser/Images.xcassets/moreIcon.imageset/Contents.json new file mode 100644 index 0000000..b1bb0d5 --- /dev/null +++ b/PhotoBrowser/Images.xcassets/moreIcon.imageset/Contents.json @@ -0,0 +1,12 @@ +{ + "images" : [ + { + "idiom" : "universal", + "filename" : "moreIcon.pdf" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/PhotoBrowser/Images.xcassets/moreIcon.imageset/moreIcon.pdf b/PhotoBrowser/Images.xcassets/moreIcon.imageset/moreIcon.pdf new file mode 100644 index 0000000..12564f4 Binary files /dev/null and b/PhotoBrowser/Images.xcassets/moreIcon.imageset/moreIcon.pdf differ diff --git a/PhotoBrowser/PBCustomView.swift b/PhotoBrowser/PBCustomView.swift index 9dd2641..4654802 100644 --- a/PhotoBrowser/PBCustomView.swift +++ b/PhotoBrowser/PBCustomView.swift @@ -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 { @@ -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 } @@ -107,6 +120,7 @@ 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)) @@ -114,12 +128,11 @@ class PBNavigationBar: UIView { 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() @@ -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) @@ -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)) @@ -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 { @@ -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) diff --git a/PhotoBrowser/PhotoBrowser.swift b/PhotoBrowser/PhotoBrowser.swift index 4cb0bb1..0bc793b 100644 --- a/PhotoBrowser/PhotoBrowser.swift +++ b/PhotoBrowser/PhotoBrowser.swift @@ -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() @@ -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 { @@ -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]]] = [:] @@ -91,6 +93,9 @@ open class PhotoBrowser: UIPageViewController { open var enableShare = true { didSet { headerView?.rightButton.isHidden = !enableShare + if !enableShare { + headerView?.updateShareStatus(false) + } } } @@ -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]?) { @@ -140,6 +146,7 @@ open class PhotoBrowser: UIPageViewController { edgesForExtendedLayout = UIRectEdge.top dataSource = self delegate = self + self.updateToolbar() } @@ -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 @@ -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() { @@ -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) } @@ -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() { diff --git a/PhotoBrowserDemo/ViewController.swift b/PhotoBrowserDemo/ViewController.swift index a8f0757..a78218c 100644 --- a/PhotoBrowserDemo/ViewController.swift +++ b/PhotoBrowserDemo/ViewController.swift @@ -56,7 +56,7 @@ 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) @@ -64,15 +64,21 @@ extension ViewController { 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) } } @@ -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) {