-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeedImageCell.swift
79 lines (65 loc) · 2.19 KB
/
FeedImageCell.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
//
// FeedImageCell.swift
// Prototype
//
// Created by Romeo Flauta on 5/23/21.
//
import UIKit
final class FeedImageCell: UITableViewCell {
@IBOutlet private(set) var locationContainer: UIView!
@IBOutlet private(set) var locationLabel: UILabel!
@IBOutlet private(set) var feedImageContainer: UIView!
@IBOutlet private(set) var feedImageView: UIImageView!
@IBOutlet private(set) var descriptionLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
feedImageView.alpha = 0
feedImageContainer.startShimmering()
}
override func prepareForReuse() {
super.prepareForReuse()
feedImageView.alpha = 0
feedImageContainer.startShimmering()
}
func fadeIn(_ image: UIImage?) {
feedImageView.image = image
UIView.animate(
withDuration: 0.25,
delay: 1.25,
options: [],
animations: {
self.feedImageView.alpha = 1
}, completion: { completed in
if completed {
self.feedImageContainer.stopShimmering()
}
})
}
}
private extension UIView {
private var shimmerAnimationKey: String {
return "shimmer"
}
func startShimmering() {
let white = UIColor.white.cgColor
let alpha = UIColor.white.withAlphaComponent(0.7).cgColor
let width = bounds.width
let height = bounds.height
let gradient = CAGradientLayer()
gradient.colors = [alpha, white, alpha]
gradient.startPoint = CGPoint(x: 0.0, y: 0.4)
gradient.endPoint = CGPoint(x: 1.0, y: 0.6)
gradient.locations = [0.4, 0.5, 0.6]
gradient.frame = CGRect(x: -width, y: 0, width: width*3, height: height)
layer.mask = gradient
let animation = CABasicAnimation(keyPath: #keyPath(CAGradientLayer.locations))
animation.fromValue = [0.0, 0.1, 0.2]
animation.toValue = [0.8, 0.9, 1.0]
animation.duration = 1
animation.repeatCount = .infinity
gradient.add(animation, forKey: shimmerAnimationKey)
}
func stopShimmering() {
layer.mask = nil
}
}