diff --git a/UPCarouselFlowLayout/UPCarouselFlowLayout.swift b/UPCarouselFlowLayout/UPCarouselFlowLayout.swift index a4a5da1..3e78bd6 100644 --- a/UPCarouselFlowLayout/UPCarouselFlowLayout.swift +++ b/UPCarouselFlowLayout/UPCarouselFlowLayout.swift @@ -14,6 +14,12 @@ public enum UPCarouselFlowLayoutSpacingMode { case overlap(visibleOffset: CGFloat) } +public enum UPCarouselFlowInitialSpacing { + case startFromZero + case equalSpace + case customSpace(initialX: CGFloat) +} + open class UPCarouselFlowLayout: UICollectionViewFlowLayout { @@ -29,6 +35,7 @@ open class UPCarouselFlowLayout: UICollectionViewFlowLayout { @IBInspectable open var sideItemAlpha: CGFloat = 0.6 @IBInspectable open var sideItemShift: CGFloat = 0.0 open var spacingMode = UPCarouselFlowLayoutSpacingMode.fixed(spacing: 40) + open var initialSpacing = UPCarouselFlowInitialSpacing.equalSpace fileprivate var state = LayoutState(size: CGSize.zero, direction: .horizontal) @@ -59,7 +66,20 @@ open class UPCarouselFlowLayout: UICollectionViewFlowLayout { let yInset = (collectionSize.height - self.itemSize.height) / 2 let xInset = (collectionSize.width - self.itemSize.width) / 2 + + + switch initialSpacing { + case .startFromZero: + self.sectionInset = UIEdgeInsets(top: yInset, left: 0, bottom: yInset, right: xInset) + case .equalSpace: + self.sectionInset = UIEdgeInsets(top: yInset, left: xInset, bottom: yInset, right: xInset) + case .customSpace(let initialX): + self.sectionInset = UIEdgeInsets(top: yInset, left: initialX, bottom: yInset, right: xInset) + } + self.sectionInset = UIEdgeInsets.init(top: yInset, left: xInset, bottom: yInset, right: xInset) + + let side = isHorizontal ? self.itemSize.width : self.itemSize.height let scaledItemOffset = (side - side*self.sideItemScale) / 2