ImageViewerViewController.swift 7.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
//  ImageViewerViewController.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 20.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class ImageViewerViewController: UIViewController {
    
    // MARK: - Variables
    
    // MARK: - View Variables
    
    public private(set) var dismissButton: UIButton!
    
domenicw's avatar
domenicw committed
20
21
    public private(set) var shareButton: UIButton!
    
22
23
24
25
26
27
28
29
    public private(set) var imageView: UIImageView!
    
    public private(set) var scrollView: UIScrollView!
    
    // MARK: - Initializers
    
    public init(image: UIImage) {
        super.init(nibName: nil, bundle: nil)
30
        self.view.backgroundColor = .black
31
32
33
34
35
36
37
38
39
40
41
        
        // View Creation
        self.scrollView = self.createScrollView()
        self.view.addSubview(self.scrollView)
        
        self.imageView = self.createImageView(image)
        self.scrollView.addSubview(imageView)
        
        self.dismissButton = self.createDismissButton()
        self.view.addSubview(self.dismissButton)
        
domenicw's avatar
domenicw committed
42
43
44
        self.shareButton = self.createShareButton()
        self.view.addSubview(self.shareButton)
        
45
46
47
48
        // View Constraints
        self.applyImageViewConstraints()
        self.applyScrollViewConstraints()
        self.applyDismissButtonConstraints()
domenicw's avatar
domenicw committed
49
        self.applyShareButtonConstraints()
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
        
        // Swipe down to dismiss viewer gesture
        let gesture = UISwipeGestureRecognizer(target: self, action: #selector(self.dismissView))
        gesture.direction = .down
        self.view.addGestureRecognizer(gesture)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Creation
    
    private func createDismissButton() -> UIButton {
        let button = BlurButton(style: .prominent)
        button.setTitle("Close", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.setTitleColor(.darkGray, for: .highlighted)
        button.addTarget(self, action: #selector(self.dismissView), for: .touchUpInside)
        button.layer.cornerRadius = 17
        button.layer.masksToBounds = true
        button.contentEdgeInsets = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        return button
    }
    
    private func applyDismissButtonConstraints() {
        NSLayoutConstraint(item: self.dismissButton, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .topMargin, multiplier: 1, constant: 10).isActive = true
domenicw's avatar
domenicw committed
80
        NSLayoutConstraint(item: self.dismissButton, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
81
82
83
        NSLayoutConstraint(item: self.dismissButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 34).isActive = true
    }
    
domenicw's avatar
domenicw committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    private func createShareButton() -> UIButton {
        let button = BlurButton(style: .prominent)
        button.setTitle("Share", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.setTitleColor(.darkGray, for: .highlighted)
        button.addTarget(self, action: #selector(self.shareImage), for: .touchUpInside)
        button.layer.cornerRadius = 17
        button.layer.masksToBounds = true
        button.contentEdgeInsets = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        return button
    }
    
    private func applyShareButtonConstraints() {
        NSLayoutConstraint(item: self.shareButton, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .topMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item:  self.shareButton, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.shareButton, attribute: .trailing, relatedBy: .lessThanOrEqual, toItem: self.dismissButton, attribute: .leading, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: self.shareButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 34).isActive = true
    }
    
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
    private func createImageView(_ image: UIImage) -> UIImageView {
        let view = UIImageView()
        view.image = image
        view.contentMode = .scaleAspectFit
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return view
    }
    
    private func applyImageViewConstraints() {
        NSLayoutConstraint(item: self.imageView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item:  self.imageView, attribute: .leading, relatedBy: .equal, toItem: self.scrollView, attribute: .leading, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.imageView, attribute: .trailing, relatedBy: .equal, toItem: self.scrollView, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.imageView, attribute: .bottom, relatedBy: .equal, toItem: self.scrollView, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.imageView, attribute: .centerX, relatedBy: .equal, toItem: self.scrollView, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.imageView, attribute: .centerY, relatedBy: .equal, toItem: self.scrollView, attribute: .centerY, multiplier: 1, constant: 0).isActive = true
        
    }
    
    private func createScrollView() -> UIScrollView {
        let view = UIScrollView()
        view.minimumZoomScale = 1.0
        view.maximumZoomScale = 5.0
        view.delegate = self
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return view
    }
    
    private func applyScrollViewConstraints() {
        NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item:  self.scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
    }
    
    // MARK: - View Setup
    
    // MARK: - View Interaction
    
    @objc private func dismissView() {
        debugPrint("dismissing ImageViewer")
        self.dismiss(animated: false, completion: nil)
    }
    
domenicw's avatar
domenicw committed
150
151
152
153
154
155
156
157
158
159
160
    @objc private func shareImage() {
        debugPrint("sharing image")
        
        guard let image = self.imageView.image else {
            return
        }
        
        let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil)
        self.present(activity, animated: true, completion: nil)
    }
    
161
162
163
164
165
166
167
168
169
}

extension ImageViewerViewController: UIScrollViewDelegate {
    
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.imageView
    }
    
}