/
ViewController.swift
159 lines (121 loc) · 7.07 KB
/
ViewController.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
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scrollView = UIScrollView(frame: self.view.bounds)
scrollView.delegate = self
scrollView.backgroundColor = UIColor.black
self.view.addSubview(scrollView)
let elCapitanImage = UIImage(named: "ElCapitan")!
let backgroundImageView = UIImageView(image: elCapitanImage)
scrollView.addSubview(backgroundImageView)
scrollView.contentSize = elCapitanImage.size
let minHorizScale = scrollView.bounds.width / elCapitanImage.size.width
let minVertScale = scrollView.bounds.height / elCapitanImage.size.height
scrollView.minimumZoomScale = min(minHorizScale, minVertScale)
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = max(minHorizScale, minVertScale)
scrollView.contentOffset = CGPoint(
x: (scrollView.contentSize.width - scrollView.bounds.width) / 2,
y: (scrollView.contentSize.height - scrollView.bounds.height) / 2)
setContentInsetToCenterScrollView(scrollView)
let darkBlur = UIBlurEffect(style: .dark)
let darkBlurView = UIVisualEffectView(effect: darkBlur)
self.view.addSubview(darkBlurView)
let lightBlur = UIBlurEffect(style: .light)
let lightBlurView = UIVisualEffectView(effect: lightBlur)
self.view.addSubview(lightBlurView)
let extraLightBlur = UIBlurEffect(style: .extraLight)
let extraLightBlurView = UIVisualEffectView(effect: extraLightBlur)
self.view.addSubview(extraLightBlurView)
let blurAreaAmount = self.view.bounds.height / 4
var remainder: CGRect
(darkBlurView.frame, remainder) = self.view.bounds.divided(atDistance: blurAreaAmount, from: CGRectEdge.maxYEdge)
(lightBlurView.frame, remainder) = remainder.divided(atDistance: blurAreaAmount, from: CGRectEdge.maxYEdge)
(extraLightBlurView.frame, remainder) = remainder.divided(atDistance: blurAreaAmount, from:CGRectEdge.maxYEdge)
darkBlurView.frame = darkBlurView.frame.integral
lightBlurView.frame = lightBlurView.frame.integral
extraLightBlurView.frame = extraLightBlurView.frame.integral
let extraLightVibrancyView = vibrancyEffectView(forBlurEffectView: extraLightBlurView)
extraLightBlurView.contentView.addSubview(extraLightVibrancyView)
let lightVibrancyView = vibrancyEffectView(forBlurEffectView: lightBlurView)
lightBlurView.contentView.addSubview(lightVibrancyView)
let darkVibrancyView = vibrancyEffectView(forBlurEffectView: darkBlurView)
darkBlurView.contentView.addSubview(darkVibrancyView)
let cameraButton = tintedIconButton(iconNamed: "Camera")
cameraButton.center = extraLightVibrancyView.convert(extraLightVibrancyView.center, from: extraLightVibrancyView.superview)
extraLightVibrancyView.contentView.addSubview(cameraButton)
let geniusButton = tintedIconButton(iconNamed: "Genius")
geniusButton.center = lightVibrancyView.convert(lightVibrancyView.center, from: lightVibrancyView.superview)
lightVibrancyView.contentView.addSubview(geniusButton)
let bitcoinButton = tintedIconButton(iconNamed: "Bitcoin")
bitcoinButton.center = darkVibrancyView.convert(darkVibrancyView.center, from: darkVibrancyView.superview)
darkVibrancyView.contentView.addSubview(bitcoinButton)
let extraLightTitleLabel = titleLabel(text: "Extra Light Blur")
extraLightVibrancyView.contentView.addSubview(extraLightTitleLabel)
let lightTitleLabel = titleLabel(text: "Light Blur")
lightVibrancyView.contentView.addSubview(lightTitleLabel)
let darkTitleLabel = titleLabel(text: "Dark Blur")
darkVibrancyView.contentView.addSubview(darkTitleLabel)
addVibrantStatusBarBackground(extraLightBlur)
}
fileprivate func vibrancyEffectView(forBlurEffectView blurEffectView:UIVisualEffectView) -> UIVisualEffectView {
let vibrancy = UIVibrancyEffect(blurEffect: blurEffectView.effect as! UIBlurEffect)
let vibrancyView = UIVisualEffectView(effect: vibrancy)
vibrancyView.frame = blurEffectView.bounds
vibrancyView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return vibrancyView
}
fileprivate func tintedIconButton(iconNamed iconName: String) -> UIButton {
let iconImage = UIImage(named: iconName)!.withRenderingMode(.alwaysTemplate)
let borderImage = UIImage(named: "ButtonRoundRect")!.withRenderingMode(.alwaysTemplate)
let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: borderImage.size))
button.setBackgroundImage(borderImage, for: UIControlState())
button.setImage(iconImage, for: UIControlState())
return button
}
fileprivate func titleLabel(text: String) -> UILabel {
let label = UILabel()
label.text = text
label.sizeToFit()
label.frame.origin = CGPoint(x: 12, y: 12)
return label
}
fileprivate func addVibrantStatusBarBackground(_ effect: UIBlurEffect) {
let statusBarBlurView = UIVisualEffectView(effect: effect)
statusBarBlurView.frame = UIApplication.shared.statusBarFrame
statusBarBlurView.autoresizingMask = .flexibleWidth
let statusBarVibrancyView = vibrancyEffectView(forBlurEffectView: statusBarBlurView)
statusBarBlurView.contentView.addSubview(statusBarVibrancyView)
let statusBar = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar.superview!.insertSubview(statusBarBlurView, belowSubview: statusBar)
self.view.addSubview(statusBarBlurView)
let statusBarBackgroundImage = UIImage(named: "MaskPixel")!.withRenderingMode(.alwaysTemplate)
let statusBarBackgroundView = UIImageView(image: statusBarBackgroundImage)
statusBarBackgroundView.frame = statusBarVibrancyView.bounds
statusBarBackgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
statusBarVibrancyView.contentView.addSubview(statusBarBackgroundView)
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
}
extension ViewController: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return scrollView.subviews.isEmpty ? nil : (scrollView.subviews[0] as UIView)
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
setContentInsetToCenterScrollView(scrollView)
}
func setContentInsetToCenterScrollView(_ scrollView: UIScrollView) {
var leftInset: CGFloat = 0.0
if (scrollView.contentSize.width < scrollView.bounds.width) {
leftInset = (scrollView.bounds.width - scrollView.contentSize.width) / 2
}
var topInset: CGFloat = 0.0
if (scrollView.contentSize.height < scrollView.bounds.height) {
topInset = (scrollView.bounds.height - scrollView.contentSize.height) / 2
}
scrollView.contentInset = UIEdgeInsets(top: topInset, left: leftInset, bottom: 0, right: 0)
}
}