Giter VIP home page Giter VIP logo

collectionnode's Introduction

CollectionNode Carthage compatible

A collectionView made for Sprite Kit

Preview

installation

Carthage

Carthage is a dependency manager that provides binary frameworks for your projects.

you can install Carthage through Homebrew, with the following command:

$ brew update
$ brew install carthage

Then you need to tell carthage to integrate this framework in your Xcode project, by adding the following to your Cartfile:

github "bwide/CollectionNode"

Now:

  1. On your project folder, run carthage update
  2. On your application target, drag BWCollectionView.framework into your Xcode project Embedded Binaries

important

If you plan to upload your app you must follow additional instructions on Carthage's README on adding frameworks to your application if you're building for iOS, tvOS, or watchOS.

usage

  1. Import CollectionNode module on your CollectionNodeScene class:
import CollectionNode
  1. Add a CollectionNode to CollectionNodeScene and set it's dataSource and Delegate:
private var myCollectionNode: CollectionNode!

override func didMove(to view: SKView) {
    myCollectionNode = CollectionNode(at: view)

    myCollectionNode.dataSource = self
    myCollectionNode.delegate = self

    addChild(myCollectionNode)
}
  1. Conform this CollectionNodeScene to CollectionNodeDataSource and implement all it's methods:
extension GameScene: CollectionNodeDataSource {
    func numberOfItems() -> Int {
        return EmojiModel.default.emojis.count
    }

    func collectionNode(_ collection: CollectionNode, itemFor index: Index) -> CollectionNodeItem {
        //create and configure items
        let item = EmojiItem()
        item.emoji = EmojiModel.default.emojis[index]
        return item
    }
}
  1. Conform to CollectionNodeDelegateand override the methods that you need:
extension GameScene: CollectionNodeDelegate {

     func collectionNode(_ collectionNode: CollectionNode, didShowItemAt index: Index) {
        let growAction = SKAction.scale(to: 1.3, duration: 0.15)
        let shrinkAction = SKAction.scale(to: 1, duration: 0.15)

        collectionNode.item(at: index).run(growAction)
        collectionNode.children.filter{ emojiCollection.children.index(of: $0) != index }.forEach{ $0.run(shrinkAction) }
    }

    func collectionNode(_ collectionNode: CollectionNode, didSelectItem item: CollectionNodeItem, at index: Index) {
        print("selected \(item.name ?? "noNameItem") at index \(index)")
    }
}
  1. Now CollectionNodewill work with it's default implementation. Override it if you want custom behavior

Usage is pretty similar to the known UICollectionView, after you create your BWCollectionView object you'll need to give it some data to display with a data source object, which is any object that conforms to the BWCollectionViewDataSource protocol.

Properties

private(set) public var index: Int

the current index of the CollectionNode

public weak var dataSource: CollectionNodeDataSource?

the object that acts as data source for the collection view

public weak var delegate: CollectionNodeDelegate?

the object that acts as delegate for the collection view

public var spaceBetweenItems: CGFloat

the spacing between elements of the CollectionNode

public var items: [CollectionNodeItem]

returns all the children of this node that are CollectionNodeItems

Methods

public func update(_ currentTime: TimeInterval, dampingRatio: Double)

To be called on the scene's update. Allows this node to animate when touch is released dampingRatio: the ratio for the collectionNode deacceleration (0 to 1 meaning the percentage of speed to deaccelerate when touch is released, default is 1%)

public func snap(to index: Index, withDuration duration: Double)

snaps to an item at a given index duration: The duration of the snap animation in seconds (default is 0.3)

public func reloadData()

reloads all the items in the collection

CollectionNodeDelegate

func collectionNode(_ collectionNode: CollectionNode, didShowItemAt index: Index) -> Void

returns the number of items to be displayed on this collectionNode

func collectionNode(_ collectionNode: CollectionNode, didSelectItem item: CollectionNodeItem, at index: Index ) -> Void

called each time an item is selected

CollectionNodeDataSource

func numberOfItems() -> Int

here you should tell the number of items this collection will display

func collectionNode(_ collection: CollectionNode, itemFor index: Index) -> CollectionNodeItem

here you should return an item for each index in the collectionVIew

important

  1. you have to call the collection's update(_ currentTime:) method in your SKScene's Update method for it to properly work

Apps using CollectionNode

Preview

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.