From 334400e6650082cd9e44adea13219ea6e031e9b2 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Tue, 15 May 2018 17:12:59 +0800 Subject: [PATCH 01/10] feature: photo browser add mini map --- PhotoBrowser.xcodeproj/project.pbxproj | 8 +- PhotoBrowser/MiniMap.swift | 138 +++++++++++++++++++++ PhotoBrowser/PhotoPreviewController.swift | 35 +++++- PhotoBrowserDemo.xcodeproj/project.pbxproj | 4 +- 4 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 PhotoBrowser/MiniMap.swift diff --git a/PhotoBrowser.xcodeproj/project.pbxproj b/PhotoBrowser.xcodeproj/project.pbxproj index d5de503..0ec80e2 100644 --- a/PhotoBrowser.xcodeproj/project.pbxproj +++ b/PhotoBrowser.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 1E11D49920AAC4CD00E48785 /* MiniMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E11D49820AAC4CD00E48785 /* MiniMap.swift */; }; 4A0739991C98FB3C0004FEA5 /* PBCustomView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A0739981C98FB3C0004FEA5 /* PBCustomView.swift */; }; 4A0B3FDF1C76DB300049338C /* Kingfisher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A0B3FDE1C76DB300049338C /* Kingfisher.framework */; }; 4A52D1E41C72CB2E001C257B /* PhotoBrowser.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A52D1E31C72CB2E001C257B /* PhotoBrowser.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -23,6 +24,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 1E11D49820AAC4CD00E48785 /* MiniMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MiniMap.swift; sourceTree = ""; }; 4A0739981C98FB3C0004FEA5 /* PBCustomView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PBCustomView.swift; sourceTree = ""; }; 4A0B3FDE1C76DB300049338C /* Kingfisher.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Kingfisher.framework; path = Carthage/Build/iOS/Kingfisher.framework; sourceTree = ""; }; 4A52D1E01C72CB2E001C257B /* PhotoBrowser.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PhotoBrowser.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -85,6 +87,7 @@ 4A0739981C98FB3C0004FEA5 /* PBCustomView.swift */, A18117DF1E83B66900F8CE8E /* CustomPhotoBroswerManager.swift */, C4C79EA02057808200C92C0C /* UIView+Frame.swift */, + 1E11D49820AAC4CD00E48785 /* MiniMap.swift */, 4A52D1E51C72CB2E001C257B /* Info.plist */, ); path = PhotoBrowser; @@ -178,6 +181,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 1E11D49920AAC4CD00E48785 /* MiniMap.swift in Sources */, 864F45E81E9F0A4900FF9215 /* Skitch.swift in Sources */, 4A52D2301C72F47C001C257B /* Photo.swift in Sources */, 4A0739991C98FB3C0004FEA5 /* PBCustomView.swift in Sources */, @@ -241,7 +245,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -293,7 +297,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; diff --git a/PhotoBrowser/MiniMap.swift b/PhotoBrowser/MiniMap.swift new file mode 100644 index 0000000..d929e09 --- /dev/null +++ b/PhotoBrowser/MiniMap.swift @@ -0,0 +1,138 @@ +// +// MiniMap.swift +// ImageMap +// +// Created by wzxjiang on 2018/5/10. +// Copyright © 2018 wzxjiang. All rights reserved. +// + +import UIKit + +struct Ratios { + let top: CGFloat + let left: CGFloat + let width: CGFloat + let height: CGFloat + + static let zero = Ratios(top: 0, left: 0, width: 0, height: 0) + + init(top: CGFloat, left: CGFloat, width: CGFloat, height: CGFloat) { + self.top = min(max(top, 0), 1) + self.left = min(max(left, 0), 1) + self.width = min(max(width, 0), 1) + self.height = min(max(height, 0), 1) + } +} + +public class MiniMap: UIView { + public var image = UIImage() { + didSet { + updateRealSize() + imageView.image = image + } + } + + private let imageView = UIImageView() + + private let backgroundLayer = CALayer() + + private var lineLayer = CAShapeLayer() + + private var maskLayer = CAShapeLayer() + + private let _size: CGSize + + var ratios: Ratios = .zero { + didSet { + updateLayer() + } + } + + private var realSize: CGSize + + required public init(size: CGSize) { + self._size = size + self.realSize = size + super.init(frame: .zero) + setup() + addLayer() + } + + required public init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func setup() { + backgroundColor = .black + addSubview(imageView) + imageView.translatesAutoresizingMaskIntoConstraints = false + imageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true + imageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true + updateImageViewSize() + } + + private func addLayer() { + backgroundLayer.frame = CGRect(origin: .zero, size: _size) + backgroundLayer.backgroundColor = UIColor.black.withAlphaComponent(0.5).cgColor + imageView.layer.addSublayer(backgroundLayer) + + lineLayer.strokeColor = UIColor.white.cgColor + lineLayer.fillColor = UIColor.clear.cgColor + imageView.layer.addSublayer(lineLayer) + } + + private func updateLayer() { + var top: CGFloat = realSize.height * ratios.top + var left: CGFloat = realSize.width * ratios.left + let width: CGFloat = realSize.width * ratios.width + let height: CGFloat = realSize.height * ratios.height + + if top + height > realSize.height { + top = realSize.height - height + } + + if left + width > realSize.width { + left = realSize.width - width + } + + let maskBezierPath = UIBezierPath() + maskBezierPath.move(to: CGPoint(x: 0, y: 0)) + maskBezierPath.addLine(to: CGPoint(x: 0, y: realSize.height)) + maskBezierPath.addLine(to: CGPoint(x: realSize.width, y: realSize.height)) + maskBezierPath.addLine(to: CGPoint(x: realSize.width, y: 0)) + maskBezierPath.move(to: CGPoint(x: left, y: top)) + maskBezierPath.addLine(to: CGPoint(x: left + width, y: top)) + maskBezierPath.addLine(to: CGPoint(x: left + width, y: top + height)) + maskBezierPath.addLine(to: CGPoint(x: left, y: top + height)) + maskBezierPath.close() + maskLayer.path = maskBezierPath.cgPath + backgroundLayer.mask = maskLayer + + + let lineBezierPath = UIBezierPath() + lineBezierPath.move(to: CGPoint(x: left, y: top)) + lineBezierPath.addLine(to: CGPoint(x: left + width, y: top)) + lineBezierPath.addLine(to: CGPoint(x: left + width, y: top + height)) + lineBezierPath.addLine(to: CGPoint(x: left, y: top + height)) + lineBezierPath.close() + lineBezierPath.stroke() + lineLayer.path = lineBezierPath.cgPath + } + + private func updateRealSize() { + let ratio = image.size.width / image.size.height + + if image.size.width > image.size.height { + realSize = CGSize(width: _size.width, height: _size.width / ratio) + } else { + realSize = CGSize(width: _size.height * ratio, height: _size.height) + } + + updateImageViewSize() + } + + private func updateImageViewSize() { + imageView.widthAnchor.constraint(equalToConstant: realSize.width).isActive = true + imageView.heightAnchor.constraint(equalToConstant: realSize.height).isActive = true + } +} diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index b1a9496..c5cf609 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -84,6 +84,8 @@ class PhotoPreviewController: UIViewController { fileprivate var isFullScreenMode: Bool = false fileprivate var panLastY: CGFloat = 0 + + fileprivate var miniMap: MiniMap? init(photo: Photo, index: NSInteger, skitches: [[String: Any]]? = nil, isSkitchButtonHidden: Bool = true) { super.init(nibName: nil, bundle: nil) @@ -183,6 +185,7 @@ class PhotoPreviewController: UIViewController { } fileprivate func setImageViewFrame(_ image: UIImage) { + miniMap?.image = image imageView.width = screenWidth imageView.height = image.size.height / image.size.width * screenWidth imageView.center = self.view.center @@ -218,6 +221,18 @@ class PhotoPreviewController: UIViewController { singleTap.require(toFail: doubleTap) + let size = CGSize(width: 100, height: 100) + let miniMap = MiniMap(size: size) + miniMap.isHidden = true + view.addSubview(miniMap) + miniMap.translatesAutoresizingMaskIntoConstraints = false + miniMap.widthAnchor.constraint(equalToConstant: size.width).isActive = true + miniMap.heightAnchor.constraint(equalToConstant: size.height).isActive = true + miniMap.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true + miniMap.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true + + self.miniMap = miniMap + if let image = photo.localOriginalPhoto() { setImageViewFrame(image) imageView.image = image @@ -349,6 +364,21 @@ extension PhotoPreviewController: UIScrollViewDelegate { doPan(scrollView.panGestureRecognizer) } scrollOldY = scrollNewY + + if moveImage != nil { + miniMap?.isHidden = true + } else { + miniMap?.isHidden = scrollView.contentSize.width <= view.frame.width + + } + + miniMap?.ratios = + Ratios( + top: 0, + left: scrollView.contentOffset.x / scrollView.contentSize.width, + width: view.frame.width / scrollView.contentSize.width, + height: 1 + ) } } @@ -421,12 +451,12 @@ extension PhotoPreviewController { // 判断是否向下拖拽 isDirectionDown = panCurrentY > panLastY panLastY = panCurrentY - + // 拖拽进度 let progress = (panCurrentY - panBeginY) / maxMoveOfY panningProgress = min(progress, 1.0) delegate?.doDraging(panningProgress) - + if panCurrentY > panBeginY { moveImage?.width = imageWidthBeforeDrag - (imageWidthBeforeDrag - imageWidthBeforeDrag * minZoom) * panningProgress moveImage?.height = imageHeightBeforeDrag - (imageHeightBeforeDrag - imageHeightBeforeDrag * minZoom) * panningProgress @@ -462,6 +492,7 @@ extension PhotoPreviewController { self.moveImage?.removeFromSuperview() self.moveImage = nil self.updateSkitchButtonStatus(false) + self.miniMap?.isHidden = self.scrollView.contentSize.width <= self.view.frame.width }) } else { guard let image = moveImage else { return } diff --git a/PhotoBrowserDemo.xcodeproj/project.pbxproj b/PhotoBrowserDemo.xcodeproj/project.pbxproj index 03bff99..0d9d7d3 100644 --- a/PhotoBrowserDemo.xcodeproj/project.pbxproj +++ b/PhotoBrowserDemo.xcodeproj/project.pbxproj @@ -267,7 +267,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -316,7 +316,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; From b5464903c197ca6979b0cfdb80c2476258e23efe Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Tue, 15 May 2018 17:20:55 +0800 Subject: [PATCH 02/10] update: mini map support to vertical direction --- PhotoBrowser/PhotoPreviewController.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index c5cf609..f1fde3d 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -374,11 +374,11 @@ extension PhotoPreviewController: UIScrollViewDelegate { miniMap?.ratios = Ratios( - top: 0, + top: scrollView.contentOffset.y / scrollView.contentSize.height, left: scrollView.contentOffset.x / scrollView.contentSize.width, width: view.frame.width / scrollView.contentSize.width, - height: 1 - ) + height: view.frame.height / scrollView.contentSize.height + ) } } From 164b024a1c642cd5508d3e888e6b1f0cb9b5ad32 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Tue, 15 May 2018 17:28:39 +0800 Subject: [PATCH 03/10] update: set minimap size to public --- PhotoBrowser/PhotoPreviewController.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index f1fde3d..cfb546a 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -86,7 +86,8 @@ class PhotoPreviewController: UIViewController { fileprivate var panLastY: CGFloat = 0 fileprivate var miniMap: MiniMap? - + public var miniMapSize: CGSize = CGSize(width: 100, height: 100) + init(photo: Photo, index: NSInteger, skitches: [[String: Any]]? = nil, isSkitchButtonHidden: Bool = true) { super.init(nibName: nil, bundle: nil) self.index = index @@ -221,13 +222,12 @@ class PhotoPreviewController: UIViewController { singleTap.require(toFail: doubleTap) - let size = CGSize(width: 100, height: 100) - let miniMap = MiniMap(size: size) + let miniMap = MiniMap(size: miniMapSize) miniMap.isHidden = true view.addSubview(miniMap) miniMap.translatesAutoresizingMaskIntoConstraints = false - miniMap.widthAnchor.constraint(equalToConstant: size.width).isActive = true - miniMap.heightAnchor.constraint(equalToConstant: size.height).isActive = true + miniMap.widthAnchor.constraint(equalToConstant: miniMapSize.width).isActive = true + miniMap.heightAnchor.constraint(equalToConstant: miniMapSize.height).isActive = true miniMap.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true miniMap.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true From 7e6ca1ee9c52478e0411d18000a5ff39162b8819 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Tue, 15 May 2018 17:46:25 +0800 Subject: [PATCH 04/10] update: swift lint --- PhotoBrowser/MiniMap.swift | 4 ++-- PhotoBrowser/PhotoPreviewController.swift | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/PhotoBrowser/MiniMap.swift b/PhotoBrowser/MiniMap.swift index d929e09..c7e3eac 100644 --- a/PhotoBrowser/MiniMap.swift +++ b/PhotoBrowser/MiniMap.swift @@ -51,8 +51,8 @@ public class MiniMap: UIView { private var realSize: CGSize required public init(size: CGSize) { - self._size = size - self.realSize = size + _size = size + realSize = size super.init(frame: .zero) setup() addLayer() diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index cfb546a..6d166b2 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -369,7 +369,6 @@ extension PhotoPreviewController: UIScrollViewDelegate { miniMap?.isHidden = true } else { miniMap?.isHidden = scrollView.contentSize.width <= view.frame.width - } miniMap?.ratios = From 3c7d6112ba7074aa27086e5c986755a3857819af Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Tue, 15 May 2018 18:06:49 +0800 Subject: [PATCH 05/10] update: bottom to superview --- PhotoBrowser/PhotoPreviewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index 6d166b2..a7e3b1e 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -229,7 +229,7 @@ class PhotoPreviewController: UIViewController { miniMap.widthAnchor.constraint(equalToConstant: miniMapSize.width).isActive = true miniMap.heightAnchor.constraint(equalToConstant: miniMapSize.height).isActive = true miniMap.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true - miniMap.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true + miniMap.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100).isActive = true self.miniMap = miniMap From a4685e750b2c2481cc0b26fc7a287396c0f3fe51 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Thu, 17 May 2018 13:45:58 +0800 Subject: [PATCH 06/10] update: minimap position --- PhotoBrowser/PhotoPreviewController.swift | 77 ++++++++++++++++++----- 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index a7e3b1e..8128c64 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -11,6 +11,20 @@ import UIKit import Kingfisher import Photos +private extension UIDevice { + static let isIPhoneX = (UIScreen.main.bounds.size == DeviceSize.Portrait.iPhoneX || UIScreen.main.bounds.size == DeviceSize.Landscape.iPhoneX) +} + +private struct DeviceSize { + struct Landscape { + static let iPhoneX = CGSize(width: 812, height: 375) + } + + struct Portrait { + static let iPhoneX = CGSize(width: 375, height: 812) + } +} + // MARK: - PhotoPreviewControllerDelegate protocol PhotoPreviewControllerDelegate: class { var isFullScreenMode: Bool {get set} @@ -56,7 +70,7 @@ class PhotoPreviewController: UIViewController { lazy var imageView: UIImageView = self.makeImageView() var waitingView: WaitingView? - weak var delegate:PhotoPreviewControllerDelegate? + weak var delegate: PhotoPreviewControllerDelegate? fileprivate let minPanY: CGFloat = -10 fileprivate let maxMoveOfY: CGFloat = 250 @@ -81,11 +95,35 @@ class PhotoPreviewController: UIViewController { fileprivate var scrollNewY: CGFloat = 0 fileprivate var scrollOldY: CGFloat = 0 - fileprivate var isFullScreenMode: Bool = false + fileprivate var isFullScreenMode: Bool = false { + didSet { + updateMiniMapLayout() + } + } fileprivate var panLastY: CGFloat = 0 fileprivate var miniMap: MiniMap? + + fileprivate var miniMapTopConstraint: NSLayoutConstraint? + + private func makeMiniMap() -> MiniMap { + let miniMap = MiniMap(size: miniMapSize) + miniMap.isHidden = true + view.addSubview(miniMap) + miniMap.translatesAutoresizingMaskIntoConstraints = false + miniMap.widthAnchor.constraint(equalToConstant: miniMapSize.width).isActive = true + miniMap.heightAnchor.constraint(equalToConstant: miniMapSize.height).isActive = true + miniMap.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -15).isActive = true + if #available(iOS 11.0, *), UIDevice.isIPhoneX { + miniMapTopConstraint = miniMap.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: isFullScreenMode ? 15 : 15 + 44) + } else { + miniMapTopConstraint = miniMap.topAnchor.constraint(equalTo: view.topAnchor, constant: isFullScreenMode ? 15 : 15 + 64) + } + miniMapTopConstraint?.isActive = true + return miniMap + } + public var miniMapSize: CGSize = CGSize(width: 100, height: 100) init(photo: Photo, index: NSInteger, skitches: [[String: Any]]? = nil, isSkitchButtonHidden: Bool = true) { @@ -222,16 +260,7 @@ class PhotoPreviewController: UIViewController { singleTap.require(toFail: doubleTap) - let miniMap = MiniMap(size: miniMapSize) - miniMap.isHidden = true - view.addSubview(miniMap) - miniMap.translatesAutoresizingMaskIntoConstraints = false - miniMap.widthAnchor.constraint(equalToConstant: miniMapSize.width).isActive = true - miniMap.heightAnchor.constraint(equalToConstant: miniMapSize.height).isActive = true - miniMap.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true - miniMap.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100).isActive = true - - self.miniMap = miniMap + miniMap = makeMiniMap() if let image = photo.localOriginalPhoto() { setImageViewFrame(image) @@ -291,6 +320,23 @@ class PhotoPreviewController: UIViewController { } return 2 * scrollView.minimumZoomScale } + + fileprivate func updateMiniMapLayout() { + guard miniMap != nil else { + return + } + + if UIDevice.isIPhoneX { + miniMapTopConstraint?.constant = isFullScreenMode ? 15 : 15 + 44 + } else { + miniMapTopConstraint?.constant = isFullScreenMode ? 15 : 15 + 64 + } + + UIView.animate(withDuration: 0.25) { [weak self] in + guard let strongSelf = self else { return } + strongSelf.view.layoutIfNeeded() + } + } } extension PhotoPreviewController: SkitchViewDelegate { @@ -319,6 +365,7 @@ extension PhotoPreviewController { guard let delegate = delegate else { return } + isFullScreenMode = !isFullScreenMode delegate.isFullScreenMode = !delegate.isFullScreenMode } @@ -365,11 +412,7 @@ extension PhotoPreviewController: UIScrollViewDelegate { } scrollOldY = scrollNewY - if moveImage != nil { - miniMap?.isHidden = true - } else { - miniMap?.isHidden = scrollView.contentSize.width <= view.frame.width - } + miniMap?.isHidden = scrollView.contentSize.width <= view.frame.width || moveImage != nil miniMap?.ratios = Ratios( From 13f97f375b3d99d3a77a09993aee1660c092bfe6 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Thu, 17 May 2018 14:53:43 +0800 Subject: [PATCH 07/10] remove: weak self --- PhotoBrowser/PhotoPreviewController.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index 8128c64..f7ac5e2 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -332,9 +332,8 @@ class PhotoPreviewController: UIViewController { miniMapTopConstraint?.constant = isFullScreenMode ? 15 : 15 + 64 } - UIView.animate(withDuration: 0.25) { [weak self] in - guard let strongSelf = self else { return } - strongSelf.view.layoutIfNeeded() + UIView.animate(withDuration: 0.25) { + self.view.layoutIfNeeded() } } } From e2eee9e7d68c272d3d860564a8dd93f5833d2210 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Thu, 17 May 2018 15:02:28 +0800 Subject: [PATCH 08/10] update: rename realsize --- PhotoBrowser/MiniMap.swift | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/PhotoBrowser/MiniMap.swift b/PhotoBrowser/MiniMap.swift index c7e3eac..64da47b 100644 --- a/PhotoBrowser/MiniMap.swift +++ b/PhotoBrowser/MiniMap.swift @@ -27,7 +27,7 @@ struct Ratios { public class MiniMap: UIView { public var image = UIImage() { didSet { - updateRealSize() + updateimageViewSize() imageView.image = image } } @@ -48,11 +48,11 @@ public class MiniMap: UIView { } } - private var realSize: CGSize + private var imageViewSize: CGSize required public init(size: CGSize) { _size = size - realSize = size + imageViewSize = size super.init(frame: .zero) setup() addLayer() @@ -82,24 +82,24 @@ public class MiniMap: UIView { } private func updateLayer() { - var top: CGFloat = realSize.height * ratios.top - var left: CGFloat = realSize.width * ratios.left - let width: CGFloat = realSize.width * ratios.width - let height: CGFloat = realSize.height * ratios.height + var top: CGFloat = imageViewSize.height * ratios.top + var left: CGFloat = imageViewSize.width * ratios.left + let width: CGFloat = imageViewSize.width * ratios.width + let height: CGFloat = imageViewSize.height * ratios.height - if top + height > realSize.height { - top = realSize.height - height + if top + height > imageViewSize.height { + top = imageViewSize.height - height } - if left + width > realSize.width { - left = realSize.width - width + if left + width > imageViewSize.width { + left = imageViewSize.width - width } let maskBezierPath = UIBezierPath() maskBezierPath.move(to: CGPoint(x: 0, y: 0)) - maskBezierPath.addLine(to: CGPoint(x: 0, y: realSize.height)) - maskBezierPath.addLine(to: CGPoint(x: realSize.width, y: realSize.height)) - maskBezierPath.addLine(to: CGPoint(x: realSize.width, y: 0)) + maskBezierPath.addLine(to: CGPoint(x: 0, y: imageViewSize.height)) + maskBezierPath.addLine(to: CGPoint(x: imageViewSize.width, y: imageViewSize.height)) + maskBezierPath.addLine(to: CGPoint(x: imageViewSize.width, y: 0)) maskBezierPath.move(to: CGPoint(x: left, y: top)) maskBezierPath.addLine(to: CGPoint(x: left + width, y: top)) maskBezierPath.addLine(to: CGPoint(x: left + width, y: top + height)) @@ -119,20 +119,20 @@ public class MiniMap: UIView { lineLayer.path = lineBezierPath.cgPath } - private func updateRealSize() { + private func updateimageViewSize() { let ratio = image.size.width / image.size.height if image.size.width > image.size.height { - realSize = CGSize(width: _size.width, height: _size.width / ratio) + imageViewSize = CGSize(width: _size.width, height: _size.width / ratio) } else { - realSize = CGSize(width: _size.height * ratio, height: _size.height) + imageViewSize = CGSize(width: _size.height * ratio, height: _size.height) } updateImageViewSize() } private func updateImageViewSize() { - imageView.widthAnchor.constraint(equalToConstant: realSize.width).isActive = true - imageView.heightAnchor.constraint(equalToConstant: realSize.height).isActive = true + imageView.widthAnchor.constraint(equalToConstant: imageViewSize.width).isActive = true + imageView.heightAnchor.constraint(equalToConstant: imageViewSize.height).isActive = true } } From 7f9ed06a386e17d1f69712a71297dad8a86db2f8 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Fri, 18 May 2018 14:25:29 +0800 Subject: [PATCH 09/10] update: mini-map auto hide --- PhotoBrowser/PhotoPreviewController.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index f7ac5e2..04502ea 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -309,6 +309,10 @@ class PhotoPreviewController: UIViewController { } } + @objc fileprivate func hideMiniMap() { + miniMap?.isHidden = true + } + func updateConstraint() { updateSkitchViewConstraint() view.layoutIfNeeded() @@ -405,6 +409,8 @@ extension PhotoPreviewController: UIScrollViewDelegate { } func scrollViewDidScroll(_ scrollView: UIScrollView) { + NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hideMiniMap), object: nil) + scrollNewY = scrollView.contentOffset.y if (scrollView.contentOffset.y < minPanY || isPanning) && !isZooming { doPan(scrollView.panGestureRecognizer) @@ -421,6 +427,10 @@ extension PhotoPreviewController: UIScrollViewDelegate { height: view.frame.height / scrollView.contentSize.height ) } + + func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { + perform(#selector(hideMiniMap), with: self, afterDelay: 3) + } } // MARK: - pan gesture @@ -459,6 +469,7 @@ extension PhotoPreviewController { isPanning = false panBeginX = 0 panBeginY = 0 + perform(#selector(hideMiniMap), with: self, afterDelay: 3) return } From ada0f39ea438163d7af23c21459613413594bc44 Mon Sep 17 00:00:00 2001 From: wzxjiang Date: Mon, 21 May 2018 12:28:08 +0800 Subject: [PATCH 10/10] fix: minimap auto hide --- PhotoBrowser/PhotoPreviewController.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index 04502ea..b68907f 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -429,7 +429,11 @@ extension PhotoPreviewController: UIScrollViewDelegate { } func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { - perform(#selector(hideMiniMap), with: self, afterDelay: 3) + if scrollView.panGestureRecognizer.state == .ended { + perform(#selector(hideMiniMap), with: self, afterDelay: 3) + } else { + NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hideMiniMap), object: nil) + } } } @@ -469,7 +473,6 @@ extension PhotoPreviewController { isPanning = false panBeginX = 0 panBeginY = 0 - perform(#selector(hideMiniMap), with: self, afterDelay: 3) return }