-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create CardsCollectionViewVerticalLayout.swift #3
Open
Viscmad
wants to merge
6
commits into
filletofish:master
Choose a base branch
from
Viscmad:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2a19d7c
Create CardsCollectionViewVerticleLayout.swift
Viscmad 9019fc6
Update CardsCollectionViewVerticleLayout.swift
Viscmad 2e631f9
Remove redundant comments, change appearance
filletofish bd010a0
Renamed classes, ident horizontal layout with 4 spaces
filletofish 3f56b5c
Refactor code between two layouts
filletofish 25bec17
Update pod spec, fix typo in file name
filletofish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// | ||
// CardsCollectionViewHorizontalLayout.swift | ||
// CardsLayout | ||
// | ||
// Created by Filipp Fediakov on 03.01.18. | ||
// Copyright © 2018 filletofish. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
|
||
protocol CardsCollectionViewLayout { | ||
var itemSize: CGSize { get set } | ||
var spacing: CGPoint { get set } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
var maximumVisibleItems: Int { get set } | ||
} | ||
|
||
|
||
open class CardsCollectionViewHorizontalLayout: UICollectionViewLayout, CardsCollectionViewLayout { | ||
|
||
// MARK: Layout configuration | ||
|
||
public var itemSize: CGSize = CGSize(width: 200, height: 300) { | ||
didSet { | ||
invalidateLayout() | ||
} | ||
} | ||
|
||
public var spacing: CGPoint = CGPoint(x: 10.0, y: 10.0) { | ||
didSet { | ||
invalidateLayout() | ||
} | ||
} | ||
|
||
public var maximumVisibleItems: Int = 4 { | ||
didSet { | ||
invalidateLayout() | ||
} | ||
} | ||
|
||
// MARK: - UICollectionViewLayout | ||
|
||
override open var collectionView: UICollectionView { | ||
return super.collectionView! | ||
} | ||
|
||
override open var collectionViewContentSize: CGSize { | ||
let itemsCount = CGFloat(collectionView.numberOfItems(inSection: 0)) | ||
return CGSize(width: collectionView.bounds.width * itemsCount, | ||
height: collectionView.bounds.height) | ||
} | ||
|
||
override open func prepare() { | ||
super.prepare() | ||
assert(collectionView.numberOfSections == 1, "Multiple sections aren't supported!") | ||
} | ||
|
||
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { | ||
let totalItemsCount = collectionView.numberOfItems(inSection: 0) | ||
let minVisibleIndex = max(Int(collectionView.contentOffset.x) / Int(collectionView.bounds.width), 0) | ||
let maxVisibleIndex = min(minVisibleIndex + maximumVisibleItems, totalItemsCount) | ||
let visibleIndices = minVisibleIndex..<maxVisibleIndex | ||
let attributes: [UICollectionViewLayoutAttributes?] = visibleIndices.map { index in | ||
let indexPath = IndexPath(item: index, section: 0) | ||
return self.layoutAttributesForItem(at: indexPath) | ||
} | ||
let filteredAttributes = attributes.flatMap { $0 } | ||
return filteredAttributes | ||
} | ||
|
||
override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { | ||
let minVisibleIndex = Int(collectionView.contentOffset.x) / Int(collectionView.bounds.width) | ||
let deltaOffset = CGFloat(Int(collectionView.contentOffset.x) % Int(collectionView.bounds.width)) | ||
let percentageDeltaOffset = CGFloat(deltaOffset) / collectionView.bounds.width | ||
let visibleIndex = indexPath.row - minVisibleIndex | ||
|
||
let attributes = UICollectionViewLayoutAttributes(forCellWith:indexPath) | ||
attributes.size = itemSize | ||
let midY = self.collectionView.bounds.midY | ||
let midX = self.collectionView.bounds.midX | ||
attributes.center = CGPoint(x: midX + spacing.x * CGFloat(visibleIndex), | ||
y: midY + spacing.y * CGFloat(visibleIndex)) | ||
attributes.zIndex = maximumVisibleItems - visibleIndex | ||
attributes.transform = scaleTransform(forVisibleIndex: visibleIndex, | ||
percentageOffset: percentageDeltaOffset) | ||
|
||
|
||
switch visibleIndex { | ||
case 0: | ||
attributes.center.x -= deltaOffset | ||
attributes.isHidden = false | ||
break | ||
case 1..<maximumVisibleItems: | ||
attributes.center.x -= spacing.x * percentageDeltaOffset | ||
attributes.center.y -= spacing.y * percentageDeltaOffset | ||
if visibleIndex == maximumVisibleItems - 1 { | ||
attributes.alpha = percentageDeltaOffset | ||
} else { | ||
attributes.alpha = 1.0 | ||
} | ||
|
||
attributes.isHidden = false | ||
break | ||
default: | ||
attributes.alpha = 0 | ||
attributes.isHidden = true | ||
break | ||
} | ||
return attributes | ||
} | ||
|
||
override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { | ||
return true | ||
} | ||
} | ||
|
||
|
||
extension CardsCollectionViewLayout { | ||
private func scale(at index: Int) -> CGFloat { | ||
let translatedCoefficient = CGFloat(index) - CGFloat(self.maximumVisibleItems) / 2 | ||
return CGFloat(pow(0.95, translatedCoefficient)) | ||
} | ||
|
||
func scaleTransform(forVisibleIndex visibleIndex: Int, percentageOffset: CGFloat) -> CGAffineTransform { | ||
guard visibleIndex != 0 else { | ||
let scaleAtZeroIndex = scale(at: visibleIndex) | ||
return CGAffineTransform(scaleX: scaleAtZeroIndex, y: scaleAtZeroIndex) | ||
} | ||
|
||
guard visibleIndex < maximumVisibleItems else { | ||
return CGAffineTransform.identity | ||
} | ||
|
||
var rawScale = scale(at: visibleIndex) | ||
let previousScale = scale(at: visibleIndex - 1) | ||
let delta = (previousScale - rawScale) * percentageOffset | ||
rawScale += delta | ||
return CGAffineTransform(scaleX: rawScale, y: rawScale) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// | ||
// CardsCollectionViewVerticalLayout.swift | ||
// CardsLayout | ||
// | ||
// Created by Filipp Fediakov on 18.08.17. | ||
// Copyright © 2017 filletofish. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
open class CardsCollectionViewVerticalLayout: UICollectionViewLayout, CardsCollectionViewLayout { | ||
|
||
// MARK: Layout configuration | ||
|
||
public var itemSize: CGSize = CGSize(width: 200, height: 300) { | ||
didSet{ | ||
invalidateLayout() | ||
} | ||
} | ||
|
||
public var spacing: CGPoint = CGPoint(x: 10.0, y: 20.0) { | ||
didSet{ | ||
invalidateLayout() | ||
} | ||
} | ||
|
||
public var maximumVisibleItems: Int = 4 { | ||
didSet{ | ||
invalidateLayout() | ||
} | ||
} | ||
|
||
// MARK: - UICollectionViewLayout | ||
|
||
override open var collectionView: UICollectionView { | ||
return super.collectionView! | ||
} | ||
|
||
override open var collectionViewContentSize: CGSize { | ||
let itemsCount = CGFloat(collectionView.numberOfItems(inSection: 0)) | ||
return CGSize(width: collectionView.bounds.width, | ||
height: collectionView.bounds.height * itemsCount) | ||
} | ||
|
||
override open func prepare() { | ||
super.prepare() | ||
assert(collectionView.numberOfSections == 1, "Multiple sections aren't supported!") | ||
} | ||
|
||
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { | ||
let totalItemsCount = collectionView.numberOfItems(inSection: 0) | ||
let minVisibleIndex = max(Int(collectionView.contentOffset.y) / Int(collectionView.bounds.height), 0) | ||
let maxVisibleIndex = min(minVisibleIndex + maximumVisibleItems, totalItemsCount) | ||
let visibleIndices = minVisibleIndex..<maxVisibleIndex | ||
let attributes: [UICollectionViewLayoutAttributes?] = visibleIndices.map { index in | ||
let indexPath = IndexPath(item: index, section: 0) | ||
return self.layoutAttributesForItem(at: indexPath) | ||
} | ||
let filteredAttributes = attributes.flatMap { $0 } | ||
return filteredAttributes | ||
} | ||
|
||
override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { | ||
let minVisibleIndex = Int(collectionView.contentOffset.y) / Int(collectionView.bounds.height) | ||
let deltaOffset = CGFloat(Int(collectionView.contentOffset.y) % Int(collectionView.bounds.height)) | ||
let percentageDeltaOffset = CGFloat(deltaOffset) / collectionView.bounds.height | ||
let visibleIndex = indexPath.row - minVisibleIndex | ||
|
||
let attributes = UICollectionViewLayoutAttributes(forCellWith:indexPath) | ||
attributes.size = itemSize | ||
let midY = self.collectionView.bounds.midY | ||
let midX = self.collectionView.bounds.midX | ||
attributes.center = CGPoint(x: midX + spacing.x * CGFloat(visibleIndex), | ||
y: midY + spacing.y * CGFloat(visibleIndex)) | ||
attributes.zIndex = maximumVisibleItems - visibleIndex | ||
attributes.transform = scaleTransform(forVisibleIndex: visibleIndex, | ||
percentageOffset: percentageDeltaOffset) | ||
|
||
|
||
switch visibleIndex { | ||
case 0: | ||
attributes.center.y -= deltaOffset | ||
attributes.isHidden = false | ||
break | ||
case 1..<maximumVisibleItems: | ||
attributes.center.x -= spacing.x * percentageDeltaOffset | ||
attributes.center.y -= spacing.y * percentageDeltaOffset | ||
if visibleIndex == maximumVisibleItems - 1 { | ||
attributes.alpha = percentageDeltaOffset | ||
} else { | ||
attributes.alpha = 1.0 | ||
} | ||
|
||
attributes.isHidden = false | ||
break | ||
default: | ||
attributes.alpha = 0 | ||
attributes.isHidden = true | ||
break | ||
} | ||
return attributes | ||
} | ||
|
||
|
||
override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { | ||
return true | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the protocol to constraint different layouts for common interface.