Skip to content

Commit

Permalink
Merge pull request #36 from SRozhina/master
Browse files Browse the repository at this point in the history
Small update for previous PR #34
  • Loading branch information
rnkyr committed Mar 13, 2018
2 parents ef94eac + 0fa58cc commit a02d5a1
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions Pod/Classes/Layouts/DisplaySwitchLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import UIKit
open class DisplaySwitchLayout: UICollectionViewLayout {

fileprivate let numberOfColumns: Int
fileprivate let cellHeightPadding: CGFloat
fileprivate let cellWidthPadding: CGFloat
fileprivate let cellPadding: CGPoint
fileprivate let staticCellHeight: CGFloat
fileprivate let nextLayoutStaticCellHeight: CGFloat
fileprivate var previousContentOffset: NSValue?
let layoutState: LayoutState
public let layoutState: LayoutState
fileprivate let listLayoutCountOfColumns = 1
fileprivate let gridLayoutCountOfColumns: Int

Expand All @@ -36,14 +35,14 @@ open class DisplaySwitchLayout: UICollectionViewLayout {

// MARK: - Lifecycle

public init(staticCellHeight: CGFloat, nextLayoutStaticCellHeight: CGFloat, layoutState: LayoutState, cellHeightPadding: CGFloat = 6, cellWidthPadding: CGFloat = 6, gridLayoutCountOfColumns: Int = 3) {
public init(staticCellHeight: CGFloat, nextLayoutStaticCellHeight: CGFloat, layoutState: LayoutState,
cellPadding: CGPoint = CGPoint(x: 6, y: 6), gridLayoutCountOfColumns: Int = 3) {
self.staticCellHeight = staticCellHeight
self.gridLayoutCountOfColumns = gridLayoutCountOfColumns
self.numberOfColumns = layoutState == .list ? listLayoutCountOfColumns : gridLayoutCountOfColumns
self.layoutState = layoutState
self.nextLayoutStaticCellHeight = nextLayoutStaticCellHeight
self.cellHeightPadding = cellHeightPadding
self.cellWidthPadding = cellWidthPadding
self.cellPadding = cellPadding

super.init()
}
Expand All @@ -70,9 +69,9 @@ open class DisplaySwitchLayout: UICollectionViewLayout {
var yOffset = [CGFloat](repeating: contentHeight, count: numberOfColumns)
for item in 0 ..< collectionView!.numberOfItems(inSection: 0) {
let indexPath = IndexPath(item: item, section: 0)
let height = cellHeightPadding + staticCellHeight
let height = cellPadding.y + staticCellHeight
let frame = CGRect(x: xOffsets[column], y: yOffset[column], width: columnWidth, height: height)
let insetFrame = frame.insetBy(dx: cellWidthPadding, dy: cellHeightPadding)
let insetFrame = frame.insetBy(dx: cellPadding.x, dy: cellPadding.y)
let attributes = DisplaySwitchLayoutAttributes(forCellWith: indexPath)
attributes.frame = insetFrame
baseLayoutAttributes.append(attributes)
Expand Down Expand Up @@ -109,11 +108,11 @@ open class DisplaySwitchLayout: UICollectionViewLayout {
return previousContentOffsetPoint
}
if layoutState == LayoutState.list {
let offsetY = ceil(previousContentOffsetPoint.y + (staticCellHeight * previousContentOffsetPoint.y / nextLayoutStaticCellHeight) + cellHeightPadding)
let offsetY = ceil(previousContentOffsetPoint.y + (staticCellHeight * previousContentOffsetPoint.y / nextLayoutStaticCellHeight) + cellPadding.y)
return CGPoint(x: superContentOffset.x, y: offsetY)
} else {
let realOffsetY = ceil((previousContentOffsetPoint.y / nextLayoutStaticCellHeight * staticCellHeight / CGFloat(numberOfColumns)) - cellHeightPadding)
let offsetY = floor(realOffsetY / staticCellHeight) * staticCellHeight + cellHeightPadding
let realOffsetY = ceil((previousContentOffsetPoint.y / nextLayoutStaticCellHeight * staticCellHeight / CGFloat(numberOfColumns)) - cellPadding.y)
let offsetY = floor(realOffsetY / staticCellHeight) * staticCellHeight + cellPadding.y
return CGPoint(x: superContentOffset.x, y: offsetY)
}
}
Expand Down

0 comments on commit a02d5a1

Please sign in to comment.