-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePreviewFlowLayout.swift
More file actions
executable file
·153 lines (115 loc) · 6.02 KB
/
ImagePreviewFlowLayout.swift
File metadata and controls
executable file
·153 lines (115 loc) · 6.02 KB
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
//
// ImagePreviewFlowLayout.swift
// ImagePickerSheet
//
// Created by Laurin Brandner on 06/09/14.
// Copyright (c) 2014 Laurin Brandner. All rights reserved.
//
import UIKit
class ImagePreviewFlowLayout: UICollectionViewFlowLayout {
var invalidationCenteredIndexPath: NSIndexPath?
var showsSupplementaryViews: Bool = true {
didSet {
invalidateLayout()
}
}
private var layoutAttributes = [UICollectionViewLayoutAttributes]()
private var contentSize = CGSizeZero
// MARK: - Initialization
override init() {
super.init()
initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
private func initialize() {
scrollDirection = .Horizontal
}
// MARK: - Layout
override func prepareLayout() {
super.prepareLayout()
layoutAttributes.removeAll(keepCapacity: false)
contentSize = CGSizeZero
if let collectionView = collectionView,
dataSource = collectionView.dataSource,
delegate = collectionView.delegate as? UICollectionViewDelegateFlowLayout {
var origin = CGPoint(x: sectionInset.left, y: sectionInset.top)
let numberOfSections = dataSource.numberOfSectionsInCollectionView?(collectionView) ?? 0
for s in 0 ..< numberOfSections {
let indexPath = NSIndexPath(forRow: 0, inSection: s)
let size = delegate.collectionView?(collectionView, layout: self, sizeForItemAtIndexPath: indexPath) ?? CGSizeZero
let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = CGRect(origin: origin, size: size)
attributes.zIndex = 0
layoutAttributes.append(attributes)
origin.x = attributes.frame.maxX + sectionInset.right
}
contentSize = CGSize(width: origin.x, height: collectionView.frame.height)
}
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
override func collectionViewContentSize() -> CGSize {
return contentSize
}
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint) -> CGPoint {
var contentOffset = proposedContentOffset
if let indexPath = invalidationCenteredIndexPath {
if let collectionView = collectionView {
let frame = layoutAttributes[indexPath.section].frame
contentOffset.x = frame.midX - collectionView.frame.width / 2.0
contentOffset.x = max(contentOffset.x, -collectionView.contentInset.left)
contentOffset.x = min(contentOffset.x, collectionViewContentSize().width - collectionView.frame.width + collectionView.contentInset.right)
}
invalidationCenteredIndexPath = nil
}
return super.targetContentOffsetForProposedContentOffset(contentOffset)
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return layoutAttributes.filter { CGRectIntersectsRect(rect, $0.frame) }.reduce([UICollectionViewLayoutAttributes]()) { memo, attributes in
let supplementaryAttributes = layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: attributes.indexPath)
return memo + [attributes, supplementaryAttributes]
}
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
return layoutAttributes[indexPath.section]
}
override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
if let collectionView = collectionView,
dataSource = collectionView.dataSource,
delegate = collectionView.delegate as? UICollectionViewDelegateFlowLayout {
let itemAttributes = layoutAttributesForItemAtIndexPath(indexPath)
let inset = collectionView.contentInset
let bounds = collectionView.bounds
let contentOffset: CGPoint = {
var contentOffset = collectionView.contentOffset
contentOffset.x += inset.left
contentOffset.y += inset.top
return contentOffset
}()
let visibleSize: CGSize = {
var size = bounds.size
size.width -= (inset.left+inset.right)
return size
}()
let visibleFrame = CGRect(origin: contentOffset, size: visibleSize)
let size = delegate.collectionView?(collectionView, layout: self, referenceSizeForHeaderInSection: indexPath.section) ?? CGSizeZero
let originX = max(itemAttributes.frame.minX, min(itemAttributes.frame.maxX - size.width, visibleFrame.maxX - size.width))
let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, withIndexPath: indexPath)
attributes.zIndex = 1
attributes.hidden = !showsSupplementaryViews
attributes.frame = CGRect(origin: CGPoint(x: originX, y: itemAttributes.frame.minY), size: size)
return attributes
}
return nil
}
override func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
return layoutAttributesForItemAtIndexPath(itemIndexPath)
}
override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
return layoutAttributesForItemAtIndexPath(itemIndexPath)
}
}