Skip to content

Commit 118f9c3

Browse files
committed
Small changed
1 parent af34834 commit 118f9c3

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

Example/Example/Pattern1ViewController.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ final class Pattern1ViewController: UIViewController {
2828
pageControl.numberOfPages = itemsCount
2929
}
3030
}
31+
required init?(coder aDecoder: NSCoder) {
32+
super.init(coder: aDecoder)
33+
NotificationCenter.default.addObserver(self, selector: #selector(Pattern1ViewController.rotate(_:)), name: .UIDeviceOrientationDidChange, object: nil)
34+
}
35+
deinit {
36+
NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil)
37+
}
3138
static func createFromStoryboard() -> Pattern1ViewController {
3239
let storyboard = UIStoryboard(name: "Pattern1", bundle: nil)
3340
return storyboard.instantiateInitialViewController() as! Pattern1ViewController
3441
}
35-
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
36-
super.viewWillTransition(to: size, with: coordinator)
37-
layout.itemSize = size
42+
func rotate(_ notification: Notification) {
43+
layout.itemSize = UIScreen.main.bounds.size
3844
layout.invalidateLayout()
45+
collectionView.rotate(notification)
3946
collectionView.layoutIfNeeded()
4047
collectionView.setNeedsLayout()
4148
}

InfiniteCollectionView/InfiniteCollectionView.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ open class InfiniteCollectionView: UICollectionView {
3333
open weak var infiniteDataSource: InfiniteCollectionViewDataSource?
3434
open weak var infiniteDelegate: InfiniteCollectionViewDelegate?
3535
fileprivate var indexOffset: Int = 0
36-
fileprivate var contentWidth: CGFloat = UIScreen.main.bounds.width
3736
fileprivate var pageIndex = 0
3837
@available(*, deprecated, message: "It becomes unnecessary because it uses UICollectionViewFlowLayout.")
3938
open var cellWidth: CGFloat?
@@ -48,13 +47,8 @@ open class InfiniteCollectionView: UICollectionView {
4847
deinit {
4948
NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil)
5049
}
51-
open override func reloadData() {
52-
super.reloadData()
53-
contentWidth = totalContentWidth
54-
}
5550
open func rotate(_ notification: Notification) {
56-
contentWidth = totalContentWidth
57-
contentOffset = CGPoint(x: CGFloat(pageIndex + indexOffset) * itemWidth, y: contentOffset.y)
51+
setContentOffset(CGPoint(x: CGFloat(pageIndex + indexOffset) * itemWidth, y: contentOffset.y), animated: false)
5852
}
5953
}
6054

@@ -78,7 +72,7 @@ private extension InfiniteCollectionView {
7872
let currentOffset = contentOffset
7973
let centerX = (scrollView.contentSize.width - bounds.width) / 2
8074
let distFromCenter = centerX - currentOffset.x
81-
if fabs(distFromCenter) > (contentWidth / 4) {
75+
if fabs(distFromCenter) > (totalContentWidth / 4) {
8276
let cellcount = distFromCenter / itemWidth
8377
let shiftCells = Int((cellcount > 0) ? floor(cellcount) : ceil(cellcount))
8478
let offsetCorrection = (abs(cellcount).truncatingRemainder(dividingBy: 1)) * itemWidth

0 commit comments

Comments
 (0)