5 // Created by Tieme van Veen on 28/08/2017.
7 // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 /// Represent a placeholder type which could be set while loading as well as
35 /// loading finished without getting an image.
36 public protocol Placeholder {
38 /// How the placeholder should be added to a given image view.
39 func add(to imageView: ImageView)
41 /// How the placeholder should be removed from a given image view.
42 func remove(from imageView: ImageView)
45 /// Default implementation of an image placeholder. The image will be set or
46 /// reset directly for `image` property of the image view.
47 extension Placeholder where Self: Image {
49 /// How the placeholder should be added to a given image view.
50 public func add(to imageView: ImageView) { imageView.image = self }
52 /// How the placeholder should be removed from a given image view.
53 public func remove(from imageView: ImageView) { imageView.image = nil }
56 extension Image: Placeholder {}
58 /// Default implementation of an arbitrary view as placeholder. The view will be
59 /// added as a subview when adding and be removed from its super view when removing.
61 /// To use your customize View type as placeholder, simply let it conforming to
62 /// `Placeholder` by `extension MyView: Placeholder {}`.
63 extension Placeholder where Self: View {
65 /// How the placeholder should be added to a given image view.
66 public func add(to imageView: ImageView) {
67 imageView.addSubview(self)
69 self.translatesAutoresizingMaskIntoConstraints = false
70 NSLayoutConstraint.activate([
71 NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0),
72 NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0),
73 NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: imageView, attribute: .height, multiplier: 1, constant: 0),
74 NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: imageView, attribute: .width, multiplier: 1, constant: 0)
78 /// How the placeholder should be removed from a given image view.
79 public func remove(from imageView: ImageView) {
80 self.removeFromSuperview()