SpriteKit API reproducing UIView's spring animations with SKAction

Overview

SpriteKit-Spring

SpriteKit-Spring is a set of extensions to perform spring animations with SpriteKit.

Installation

iOS 7

If you need to support iOS 7, you can just drop SpriteKit-Spring.swift in your project.

iOS 8

If you support iOS 8 only, you can install SpriteKit-Spring as a framework with CocoaPods (version 0.36 or above) by adding the following lines to your Podfile:

use_frameworks!

pod 'SpriteKit-Spring'

You then need to import SpriteKit-Spring wherever you want to use it:

Swift:

import SpriteKit_Spring

Objective-C:

#import <SpriteKit_Spring/SpriteKit_Spring-Swift.h>

Usage

In iOS 7, Apple introduced spring animations in UIKit by adding a new animation method on UIView (see WWDC video and UIView documentation).

This library replicates this feature by adding a spring variant to most factory methods of SKAction. The parameters usingSpringWithDamping: and initialSpringVelocity: have the same meaning as their UIKit counterpart. See below for examples.

Examples

Move

let move = SKAction.moveByX(0, y: 200, 
  duration: 5, delay: 5,
  usingSpringWithDamping: 0.2, initialSpringVelocity: 0)
  
node.runAction(move)

Move

Scale

let scale = SKAction.scaleXTo(2, y: 0.5,
  duration: 5, delay: 5,
  usingSpringWithDamping: 0.2, initialSpringVelocity: 0)
  
node.runAction(scale)

Scale

Rotate

let rotate = SKAction.rotateByAngle(CGFloat(M_PI/2),
  duration: 5, delay: 5,
  usingSpringWithDamping: 0.2, initialSpringVelocity: 0)
  
node.runAction(rotate)

Rotate

Notes

  • Because they use customActionWithDuration:actionBlock:, none of these actions is reversible (the reverse action performs the same animation).
  • All these factory methods are based on the lower level animateKeyPath:byValue:duration:delay:usingSpringWithDamping:initialSpringVelocity: and animateKeyPath:toValue:duration:delay:usingSpringWithDamping:initialSpringVelocity: methods, which can animate any CGFloat key path. If you have a SKNode subclass with a fancy CGFloat property that you'd like to animate, feel free to use these methods!

Creator

License

SpriteKit-Spring is released under the Apache 2 license. See LICENSE for details.

Comments
  • var parameters are depreciated and will be removed in swift 3.

    var parameters are depreciated and will be removed in swift 3.

    private class func animateKeyPath(keyPath: String, **var** byValue initialDistance: CGFloat!, **var** toValue finalValue: CGFloat!, duration: NSTimeInterval, delay: NSTimeInterval, usingSpringWithDamping dampingRatio: CGFloat, initialSpringVelocity velocity: CGFloat) -> SKAction {

    var initialDistance = byValue var finalValue = toValue would it be ok to create local variables from the immutable ones that would be in the parameters, perhaps?

    opened by dkliman 4
  • Objective-C Version?

    Objective-C Version?

    Any way you could translate this to Objective-C? I really want to use Spring Animations, but my Swift knowledge is sparse, and would take a lot more time than I would want to figure out how to write it myself. If not, that's okay, but I thought I would ask as it would probably be helpful for others as well who still use Obj-C. Thanks!

    opened by KjellConnelly 2
  • Error upon building: CGFloat cast issue

    Error upon building: CGFloat cast issue

    Thanks for the 'package'. I am getting an error when I tried building my project after adding this pod

    SpriteKit-Spring/SpriteKit-Spring.swift:240:62: 'AnyObject?' is not convertible to 'CGFloat'; did you mean to use 'as!' to force downcast?

    opened by wbashir 2
  • Add explicit @objc markers for Objective-C support in Swift 4

    Add explicit @objc markers for Objective-C support in Swift 4

    Swift 4 by default requires explicit @objc markers to work with Objective-C.

    Because of the Generics it wasn't clear how to make the Damping Logic functions @objc compatible but if they're only used internally they shouldn't need to be exposed?

    opened by warpling 0
  • Make types more explicit to reduce complie times

    Make types more explicit to reduce complie times

    SpriteKit-Spring.swift was taking over 50 seconds to compile in Xcode 7.3.1 / Swift 2.2. This appears to be due to the type inference engine having to work very hard.

    I have made a few changes to make the types more explicit and this has reduced compile times to a few hundred ms.

    Fair warning! I haven't tested this change for anything other than the functions I am using in my project.

    opened by elgordino 0
  • Calculate distance correctly for animations using byValue

    Calculate distance correctly for animations using byValue

    Hey! Awesome library, thanks for building it. I noticed that there's a bug with animations that use byValue, e.g.:

    SKAction.scale(by:duration:delay:usingSpringWithDamping:initialSpringVelocity:)
    

    The distance isn't calculated correctly, so the behavior uses byValue as the distance.

    For example, say there's a node with a current scale of 1.0 - if we tried to scale it by 1.2, SpriteKit-Spring will currently scale it to 1.0 + 1.2 instead of 1.0 * 1.2, which in this case would more than double the scale instead of scaling appropariately. I put up an example app at https://github.com/noahsark769/NGSpriteKitSpringTest to demonstrate this.

    The fix is to calculate the initialDistance based on multiplying it by initialValue.

    Let me know if I need to do anything else to fix this bug! Thanks!

    opened by noahsark769 1
  • Cocoa Pods support for tvOS

    Cocoa Pods support for tvOS

    Is it possible to provide cocoa pods support for tvOS?. The helper works flawlessly on tvOS but doesn't have cocoa pods support which makes it harder to manage when having a universal iOS and tvOS game.

    Thanks for your great helper.

    opened by crashoverride777 2
Owner
Alexis Taugeron
Alexis Taugeron
Extensions giving Swift's Codable API type inference super powers 🦸‍♂️🦹‍♀️

Welcome to Codextended — a suite of extensions that aims to make Swift’s Codable API easier to use by giving it type inference-powered capabilities an

John Sundell 1.4k Jan 2, 2023
Swift-friendly API for a set of powerful Objective C runtime functions.

ObjectiveKit ObjectiveKit provides a Swift friendly API for a set of powerful Objective C runtime functions. Usage To use ObjectiveKit: Import Objecti

Roy Marmelstein 850 Oct 25, 2022
noppefoxwolf/notion is a notion.so API library written in swift.

notion noppefoxwolf/notion is a notion.so API library written in swift. Installation Xcode Project > Swift Packages [email protected]:noppefoxwolf/notion

noppefoxwolf 44 Oct 7, 2022
An SSH config parser library with a fancy API

The SshConfig makes it quick and easy to load, parse, and decode/encode the SSH configs. It also helps to resolve the properties by hostname and use them safely in your apps (thanks for Optional and static types in Swift).

Artem Labazin 8 Nov 25, 2022
This is a app developed in Swift, using Object Oriented Programing, UIKit user interface programmatically, API Request and Kingfisher to load remote images

iOS NOW ⭐ This is a app developed in Swift, using Object Oriented Programing, UIKit user interface programmatically, API Request and Kingfisher to loa

William TristĂŁo de Paula 1 Dec 7, 2021
Questrade API written in Swift

QuestradeAPI Getting Started The QuestAPI is made up of two main concepts: ResponseProviders API ResponseProviders The job of the provider is to retur

Eli Slade 2 Mar 15, 2022
Project shows how to unit test asynchronous API calls in Swift using Mocking without using any 3rd party software

UnitTestingNetworkCalls-Swift Project shows how to unit test asynchronous API ca

Gary M 0 May 6, 2022
Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway

Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway deployed on Graviton arm64 build swift:5.6.2-amazonlinux2-docker image

Furqan 2 Aug 16, 2022
PDF generator using UIViews or UIViews with an associated XIB

Description Create UIView objects using any method you like, including interface builder with Auto-layout and size classes enabled. Then generate a PD

null 34 Dec 17, 2022
Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers

Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers. It's magic. Features Animate a

Robert-Hein Hooijmans 1.3k Nov 17, 2022
A radial/circular menu featuring spring animations.

ALRadialMenu A radial/circular menu featuring spring animations. Written in swift. Experimenting with fluent interfaces (https://github.com/vandadnp/s

Alex Littlejohn 50 Jul 1, 2022
Swiftui Spring Animations

Swiftui Spring Animations This repository serves as your reference for SwiftUI Spring Animations. It demonstrates use cases for the various types of s

Stream 348 Dec 30, 2022
Wave is a spring-based animation engine for iOS that makes it easy to create fluid, interruptible animations that feel great.

Wave is a spring-based animation engine for iOS and iPadOS. It makes it easy to create fluid, interactive, and interruptible animations that feel great.

Janum Trivedi 1.2k Jan 8, 2023
A Swift library to take the power of UIView.animateWithDuration(_:, animations:...) to a whole new level - layers, springs, chain-able animations and mixing view and layer animations together!

ver 2.0 NB! Breaking changes in 2.0 - due to a lot of requests EasyAnimation does NOT automatically install itself when imported. You need to enable i

Marin Todorov 3k Dec 27, 2022
RetroVisor helps you with inspecting UIViews in your unit tests.

RetroVisor RetroVisor helps you with inspecting UIViews in your unit tests. You do test views in unit tests, right? Right? Usually, I mark subview pro

Vid Tadel 2 Mar 29, 2022
A Swift mixin for reusing views easily and in a type-safe way (UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers, Storyboards…)

Reusable A Swift mixin to use UITableViewCells, UICollectionViewCells and UIViewControllers in a type-safe way, without the need to manipulate their S

Olivier Halligon 2.9k Jan 3, 2023
Enables you to hide ur UIViews and make them screen/screen shot proof. objective c/c++ only

SecureView Enables you to hide ur UIViews and make them screen/screen shot proof. objective c/c++ only Usage UIWindow* mainWindow; - (void) setup {

Red16 6 Oct 13, 2022
UIViews that update themselves when your data changes, like React.

StateView is a UIView substitute that automatically updates itself when data changes. Contents: Overview What's it like? Sample apps How does it work?

Sahand Nayebaziz 491 Sep 9, 2022