Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LNZCollectionLayouts/Layouts/LNZCarouselCollectionViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ open class LNZCarouselCollectionViewLayout: LNZInfiniteCollectionViewLayout {
///to the distance of its center to the collection's center.
@IBInspectable public var minimumScaleFactor: CGFloat = 0.85

///The minimum value of alpha for items not in focus.
@IBInspectable public var sideItemAlpha: CGFloat = 1.0

//MARK: - Utility properties

override var canInfiniteScroll: Bool { return super.canInfiniteScroll && isInfiniteScrollEnabled }
Expand Down Expand Up @@ -63,6 +66,10 @@ open class LNZCarouselCollectionViewLayout: LNZInfiniteCollectionViewLayout {
//property.
attributes.zIndex = Int(scale * 100000)
attributes.transform3D = CATransform3DScale(CATransform3DIdentity, scale, scale, 1)

if sideItemAlpha != 1.0 {
shouldChangeAlpha(collection, attributes: attributes)
}
}

open override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
Expand All @@ -84,4 +91,18 @@ open class LNZCarouselCollectionViewLayout: LNZInfiniteCollectionViewLayout {
configureAttributes(for: attribute)
return attribute
}

private func shouldChangeAlpha(_ collectionView: UICollectionView, attributes: UICollectionViewLayoutAttributes) {
let collectionCenter = collectionView.frame.size.width / 2
let offset = collectionView.contentOffset.x
let normalizedCenter = attributes.center.x - offset

let maxDistance = self.itemSize.width
let distance = min(abs(collectionCenter - normalizedCenter), maxDistance)
let ratio = (maxDistance - distance) / maxDistance

let alpha = ratio * (1 - self.sideItemAlpha) + self.sideItemAlpha

focusChangeDelegate?.didChangeAlpha(alpha, for: attributes.indexPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

///This methods will be called on the delegate whenever a centered element is changing. In fact as element in focus is to be intended
///as element currently centered
@objc public protocol FocusChangeDelegate: class {
@objc public protocol FocusChangeDelegate: AnyObject {
///This method will signal to the delegate that the element in focus will change.
///- parameter container: The object that is tracking the focused element
///- parameter inFocus: The element currently in focus (before the change)
Expand All @@ -21,10 +21,15 @@ import UIKit
///- parameter container: The object that is tracking the focused element
///- parameter inFocus: The element currently in focus
func focusContainer(_ container: FocusedContaining, didChangeElement inFocus: Int)

///This method will signal to the delegate that the elements can change alpha.
///- parameter alpha: The value of alpha
///- parameter indexPath: The indexPath of element
func didChangeAlpha(_ alpha: CGFloat, for indexPath: IndexPath)
}

///An object conforming FocusContaining will track elements in focus in a collection and will alert the delegate for changes-
@objc public protocol FocusedContaining: class {
@objc public protocol FocusedContaining: AnyObject {
///The element currently in focus
var currentInFocus: Int { get }

Expand Down