This repository has been archived by the owner on Jun 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
EFImageViewZoom.swift
165 lines (136 loc) · 4.39 KB
/
EFImageViewZoom.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
//
// EFImageViewZoom.swift
// EFImageViewZoom
//
// Created by Ezequiel França on 27/05/17.
// Copyright © 2017 Ezequiel França. All rights reserved.
//
import UIKit
public protocol EFImageViewZoomDelegate : class {
func viewForZooming(zoomView: EFImageViewZoom) -> UIView?
}
public extension EFImageViewZoomDelegate {
func viewForZooming(zoomView: EFImageViewZoom) -> UIView? {
return zoomView.imageView
}
}
private extension UIScrollView {
func hideIndicators() {
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
}
}
@IBDesignable
public class EFImageViewZoom: UIScrollView {
public weak var _delegate: EFImageViewZoomDelegate?
fileprivate (set) public var imageView: UIImageView!
fileprivate var cacheImage: UIImage!
@IBInspectable public var image: UIImage! {
didSet{
guard let _imageView = self.imageView else {
cacheImage = image
return
}
_imageView.image = image
}
}
@IBInspectable public var _minimumZoomScale: CGFloat = 1.0 {
didSet{
self.minimumZoomScale = _minimumZoomScale
}
}
@IBInspectable public var _maximumZoomScale: CGFloat = 6.0 {
didSet{
self.maximumZoomScale = _maximumZoomScale
}
}
public var contentModeImageView : UIView.ContentMode = .scaleAspectFit {
didSet{
self.contentMode = contentModeImageView
self.sizeToFit()
self.contentSize = self.imageView.intrinsicContentSize
self.imageView.contentMode = contentModeImageView
}
}
public var highlightedImage: UIImage? = nil {
didSet {
self.imageView.highlightedImage = highlightedImage
}
}
public var isHighlighted: Bool = false {
didSet {
self.imageView.isHighlighted = isHighlighted
}
}
public var animationImages: [UIImage]? = nil {
didSet {
self.imageView.animationImages = animationImages
}
}
public var highlightedAnimationImages: [UIImage]? = nil {
didSet{
self.imageView.highlightedAnimationImages = highlightedAnimationImages
}
}
public var animationDuration: TimeInterval = TimeInterval() {
didSet{
self.imageView.animationDuration = animationDuration
}
}
public var animationRepeatCount: Int = 0 {
didSet{
self.imageView.animationRepeatCount = animationRepeatCount
}
}
override public var tintColor: UIColor! {
didSet{
self.imageView.tintColor = tintColor
}
}
public func startAnimating() {
self.imageView.startAnimating()
}
public func stopAnimating() {
self.imageView.stopAnimating()
}
public var isAnimating: Bool {
get{
return self.imageView.isAnimating
}
}
public override func awakeFromNib() {
self.imageView = UIImageView(frame: self.bounds)
self.imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.imageView.contentMode = contentModeImageView
self.contentMode = contentModeImageView
self.imageView.clipsToBounds = true
self.addSubview(imageView)
if let _cache = cacheImage {
self.imageView.image = _cache
}
self.hideIndicators()
self.setupTap()
self.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(screenOrientationChanged), name: UIDevice.orientationDidChangeNotification, object: nil)
}
override public func draw(_ rect: CGRect) {
self.imageView.frame = rect
super.draw(rect)
}
@objc private func screenOrientationChanged() {
draw(self.frame)
}
private func setupTap() {
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
self.addGestureRecognizer(tap)
}
@objc func doubleTapped() {
self.setZoomScale(1.0, animated: true)
}
}
extension EFImageViewZoom: UIScrollViewDelegate {
public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self._delegate?.viewForZooming(zoomView: scrollView as! EFImageViewZoom)
}
}