Created
September 25, 2019 09:08
-
-
Save wendyliga/cee3351c9cf9abb6220f6bc5eb837510 to your computer and use it in GitHub Desktop.
TextureGram StoryNode
This file contains hidden or 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
import AsyncDisplayKit | |
class StoryNode: ASCollectionNode{ | |
let stories: [Story] | |
init(stories: [Story]) { | |
self.stories = stories | |
let layout = UICollectionViewFlowLayout() | |
layout.scrollDirection = .horizontal | |
layout.minimumLineSpacing = 6 | |
layout.minimumInteritemSpacing = 0 | |
super.init(frame: .zero, collectionViewLayout: layout, layoutFacilitator: nil) | |
self.style.width = ASDimension(unit: .fraction, value: 1) | |
self.style.height = ASDimension(unit: .points, value: 75) | |
self.delegate = self | |
self.dataSource = self | |
self.view.showsHorizontalScrollIndicator = false | |
} | |
} | |
extension StoryNode: ASCollectionDataSource, ASCollectionDelegate { | |
func numberOfSections(in collectionNode: ASCollectionNode) -> Int { | |
return 1 | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int { | |
return stories.count | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock { | |
guard stories.count > indexPath.row else { return { ASCellNode() } } | |
let story = stories[indexPath.row] | |
// this may be executed on a background thread - it is important to make sure it is thread safe | |
let cellNodeBlock = { () -> ASCellNode in | |
return StoryCell(story: story) | |
} | |
return cellNodeBlock | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment