forked from tvidenov/appstore-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenshotsCell.swift
99 lines (73 loc) · 3.15 KB
/
ScreenshotsCell.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
//
// ScreenshotsCell.swift
// AppStore
//
// Created by Tihomir Videnov on 11/12/16.
// Copyright © 2016 Tihomir Videnov. All rights reserved.
//
import UIKit
class ScreenshotsCell: BaseCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var app: App? {
didSet {
collectionView.reloadData()
}
}
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
return cv
}()
let dividerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0.4, alpha: 0.4)
return view
}()
fileprivate let cellId = "cellId"
override func setupViews() {
super.setupViews()
collectionView.dataSource = self
collectionView.delegate = self
addSubview(collectionView)
addSubview(dividerView)
addConstraintsWithFormat(format: "H:|[v0]|", views: collectionView)
addConstraintsWithFormat(format: "H:|-14-[v0]|", views: dividerView)
addConstraintsWithFormat(format: "V:|[v0][v1(1)]|", views: collectionView,dividerView)
collectionView.register(ScreenshotImageCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = app?.screenshots?.count {
return count
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ScreenshotImageCell
if let imageName = app?.screenshots?[indexPath.item] {
cell.imageView.image = UIImage(named: imageName)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 240, height: frame.height - 28)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
}
private class ScreenshotImageCell: BaseCell {
let imageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFill
iv.backgroundColor = .green
return iv
}()
fileprivate override func setupViews() {
super.setupViews()
layer.masksToBounds = true
addSubview(imageView)
addConstraintsWithFormat(format: "H:|[v0]|", views: imageView)
addConstraintsWithFormat(format: "V:|[v0]|", views: imageView)
}
}
}