This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathImageViewController.swift
351 lines (295 loc) · 13.8 KB
/
ImageViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// ImageViewController.swift
// UZImageCollectionView
//
// Created by sonson on 2015/06/05.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import UIKit
import FLAnimatedImage
let ImageViewControllerDidChangeCurrentImageName = Notification.Name(rawValue: "ImageViewControllerDidChangeCurrentImage")
class ImageViewController: UIViewController, Page, ImageDownloadable, ImageViewDestination {
var thumbnails: [Thumbnail] = []
let index: Int
let scrollView = UIScrollView(frame: CGRect.zero)
let mainImageView = FLAnimatedImageView(frame: CGRect.zero)
var indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
var maximumZoomScale: CGFloat = 0
var minimumZoomScale: CGFloat = 0
var imageURL = URL(string: "https://api.sonson.jp")!
var task: URLSessionDataTask?
var isOpenedBy3DTouch = false
private var _alphaWithoutMainContent: CGFloat = 1
var initialiContentOffsetY = CGFloat(0)
var alphaWithoutMainContent: CGFloat {
get {
return _alphaWithoutMainContent
}
set {
_alphaWithoutMainContent = newValue
scrollView.backgroundColor = scrollView.backgroundColor?.withAlphaComponent(_alphaWithoutMainContent)
view.backgroundColor = view.backgroundColor?.withAlphaComponent(_alphaWithoutMainContent)
}
}
func thumbnailImageView() -> UIImageView {
return mainImageView
}
deinit {
print("deinit ImageViewController")
NotificationCenter.default.removeObserver(self)
}
required init?(coder aDecoder: NSCoder) {
self.index = 0
// self.imageCollectionViewController = ImageCollectionViewController(collection: ImageCollection(newList: []))
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
setupSubviews()
view.backgroundColor = UIColor.white
scrollView.backgroundColor = UIColor.white
scrollView.alwaysBounceVertical = true
let rec = UITapGestureRecognizer(target: self, action: #selector(ImageViewController.didTapGesture(recognizer:)))
self.scrollView.addGestureRecognizer(rec)
}
@objc func didTapGesture(recognizer: UITapGestureRecognizer) {
if let imageViewPageController = self.parentImageViewPageController {
imageViewPageController.navigationBar.isHidden = !imageViewPageController.navigationBar.isHidden
}
}
override func willMove(toParentViewController parent: UIViewController?) {
super.willMove(toParentViewController: parent)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
print("viewDidAppear")
super.viewDidAppear(animated)
}
override func viewDidDisappear(_ animated: Bool) {
print("viewDidDisappear")
super.viewDidDisappear(animated)
}
func toggleDarkMode(isDark: Bool) {
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.scrollView.backgroundColor = isDark ? UIColor.black : UIColor.white
}, completion: { (_) -> Void in
})
}
init(index: Int, thumbnails: [Thumbnail], isOpenedBy3DTouch: Bool = false) {
self.index = index
scrollView.addSubview(mainImageView)
super.init(nibName: nil, bundle: nil)
self.thumbnails = thumbnails
self.imageURL = thumbnails[index].thumbnailURL
self.isOpenedBy3DTouch = isOpenedBy3DTouch
NotificationCenter.default.addObserver(self, selector: #selector(ImageViewController.didFinishDownloading(notification:)), name: ImageDownloadableDidFinishDownloadingName, object: nil)
download(imageURLs: [self.imageURL], sender: self)
}
class func controllerWithIndex(index: Int, isDark: Bool, thumbnails: [Thumbnail]) -> ImageViewController {
let con = ImageViewController(index: index, thumbnails: thumbnails)
return con
}
}
extension ImageViewController {
func setupScrollViewScale(imageSize: CGSize) {
scrollView.frame = self.view.bounds
let boundsSize = self.view.bounds
// calculate min/max zoomscale
let xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
let yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
// fill width if the image and phone are both portrait or both landscape; otherwise take smaller scale
let imagePortrait = imageSize.height > imageSize.width
let phonePortrait = boundsSize.height > imageSize.width
var minScale = imagePortrait == phonePortrait ? xScale : (xScale < yScale ? xScale : yScale)
// on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
// maximum zoom scale to 0.5.
let maxScale = 10.0 / UIScreen.main.scale
// don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
if minScale > maxScale {
minScale = maxScale
}
scrollView.maximumZoomScale = maxScale
scrollView.minimumZoomScale = minScale
scrollView.contentSize = mainImageView.image!.size
scrollView.zoomScale = scrollView.minimumZoomScale
}
func updateImageCenter() {
let boundsSize = self.view.bounds
var frameToCenter = mainImageView.frame
if frameToCenter.size.width < boundsSize.width {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2
} else {
frameToCenter.origin.x = 0
}
if frameToCenter.size.height < boundsSize.height {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2
} else {
frameToCenter.origin.y = 0
}
mainImageView.frame = frameToCenter
}
func setIndicator() {
self.view.addSubview(indicator)
indicator.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraint(NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
}
func setScrollView() {
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.bouncesZoom = true
scrollView.decelerationRate = UIScrollViewDecelerationRateFast
scrollView.delegate = self
scrollView.isMultipleTouchEnabled = true
self.view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[scrollView]-0-|", options: NSLayoutFormatOptions(), metrics: [:], views: ["scrollView": scrollView]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[scrollView]-1-|", options: NSLayoutFormatOptions(), metrics: [:], views: ["scrollView": scrollView]))
}
func setupSubviews() {
self.view.isMultipleTouchEnabled = true
self.navigationController?.view.isMultipleTouchEnabled = true
setScrollView()
setIndicator()
indicator.startAnimating()
indicator.isHidden = false
setImage(of: self.imageURL)
}
func close() {
if let imageViewPageController = parentImageViewPageController {
imageViewPageController.close(sender: self)
}
}
var parentImageViewPageController: ImageViewPageController? {
if let vc1 = self.parent as? ImageViewPageController {
return vc1
}
return nil
}
}
// MARK: UIScrollViewDelegate
extension ImageViewController: UIScrollViewDelegate {
func scrollViewDidZoom(_ scrollView: UIScrollView) {
print("scrollViewDidZoom")
let ratio = scrollView.zoomScale / scrollView.minimumZoomScale
let threshold = CGFloat(0.6)
var alpha = CGFloat(1)
print(ratio)
if ratio > 1 {
alpha = 1
} else if ratio > threshold {
// alpha = (ratio - threshold / 2) * (ratio - threshold / 2) + 1 - (1 - threshold / 2) * (1 - threshold / 2)
alpha = (ratio - 1) * (ratio - 1) * (-1) / ((threshold - 1) * (threshold - 1)) + 1
} else {
alpha = 0
}
print(alpha)
parentImageViewPageController?.alphaWithoutMainContent = alpha
self.updateImageCenter()
if ratio < threshold {
close()
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
print("scrollViewDidEndDragging")
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("scrollViewDidScroll")
if scrollView.contentOffset.y < initialiContentOffsetY {
let threshold = CGFloat(-150)
let s = CGFloat(-50)
let x = scrollView.contentOffset.y - initialiContentOffsetY
if x < s {
let param = (x - s) * (x - s) * (-1) / ((threshold - s) * (threshold - s)) + 1
parentImageViewPageController?.alphaWithoutMainContent = param
} else {
parentImageViewPageController?.alphaWithoutMainContent = 1
}
if x < threshold {
close()
}
}
}
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDecelerating")
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
print("scrollViewDidEndScrollingAnimation")
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print("scrollViewDidEndDecelerating")
}
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("scrollViewDidEndZooming")
self.updateImageCenter()
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging")
let userInfo: [String: Any] = ["index": self.index, "thumbnail": self.thumbnails[self.index]]
NotificationCenter.default.post(name: ImageViewPageControllerDidChangePageName, object: nil, userInfo: userInfo)
NotificationCenter.default.post(name: ImageViewPageControllerDidStartDraggingThumbnailName, object: nil, userInfo: ["thumbnail": thumbnails[index]])
}
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
print("scrollViewWillBeginZooming")
let userInfo: [String: Any] = ["index": self.index, "thumbnail": self.thumbnails[self.index]]
NotificationCenter.default.post(name: ImageViewPageControllerDidChangePageName, object: nil, userInfo: userInfo)
NotificationCenter.default.post(name: ImageViewPageControllerDidStartDraggingThumbnailName, object: nil, userInfo: ["thumbnail": thumbnails[index]])
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
print("viewForZooming")
return mainImageView
}
}
// MARK: ImageDownloadable
extension ImageViewController {
func setImage(of url: URL) {
do {
let data = try cachedDataInCache(of: url)
guard let animatedImage: FLAnimatedImage = FLAnimatedImage(animatedGIFData: data) else {
if let image = UIImage(data: data as Data) {
mainImageView.isHidden = false
mainImageView.image = image
mainImageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image.size)
setupScrollViewScale(imageSize: image.size)
updateImageCenter()
initialiContentOffsetY = scrollView.contentOffset.y
indicator.stopAnimating()
indicator.isHidden = true
} else {
indicator.startAnimating()
indicator.isHidden = false
}
return
}
mainImageView.animatedImage = animatedImage
mainImageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: animatedImage.size)
setupScrollViewScale(imageSize: animatedImage.size)
updateImageCenter()
initialiContentOffsetY = scrollView.contentOffset.y
indicator.stopAnimating()
indicator.isHidden = true
} catch {
print(error)
}
}
@objc func didFinishDownloading(notification: NSNotification) {
if let userInfo = notification.userInfo, let obj = userInfo[ImageDownloadableSenderKey] as? ImageViewController, let url = userInfo[ImageDownloadableUrlKey] as? URL {
if obj == self {
if let _ = userInfo[ImageDownloadableErrorKey] as? NSError {
mainImageView.isHidden = false
mainImageView.image = UIImage(named: "error")
mainImageView.frame = self.view.bounds
updateImageCenter()
mainImageView.contentMode = .center
mainImageView.isUserInteractionEnabled = false
indicator.stopAnimating()
indicator.isHidden = true
} else {
setImage(of: url)
}
}
}
}
}