Skip to content

Instantly share code, notes, and snippets.

@wendyliga
Created September 25, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wendyliga/cee3351c9cf9abb6220f6bc5eb837510 to your computer and use it in GitHub Desktop.
Save wendyliga/cee3351c9cf9abb6220f6bc5eb837510 to your computer and use it in GitHub Desktop.
TextureGram StoryNode
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