项目作者: cats-oss

项目描述 :
:deer: Simple Interface Core Animation. Run type-safe animation sequencially or parallelly
高级语言: Swift
项目地址: git://github.com/cats-oss/Sica.git
创建时间: 2018-06-25T07:55:06Z
项目社区:https://github.com/cats-oss/Sica

开源协议:MIT License

下载


Simple Interface Core Animation


Sica


Platform

Language


Carthage




Swift Package Manager


Version


License

Sica can execute various animations sequentially or parallelly.

Features

  • Animation with duration and delay
  • parallel / sequence animation
  • Easings
  • Springs
  • Transition

Requirements

  • Xcode 9.3 or greater
  • iOS 9 or greater
  • tvOS 10.0 or greater
  • macOS 10.11 or greater
  • Swift 4.2 (since 0.3.4)

Installation

Carthage

If you’re using Carthage, simply add
Sica to your Cartfile:

  1. github "cats-oss/Sica"

CocoaPods

Sica is available through CocoaPods. To instal
it, simply add the following line to your Podfile:

  1. pod 'Sica'

Swift Package Manager

Sica is available through SwiftPM, create Package.swift and add dependencies value

  1. dependencies: [
  2. .package(url: "https://github.com/cats-oss/Sica.git", from: "0.4.1")
  3. ]

See also: GitHub - j-channings/swift-package-manager-ios: Example of how to use SPM v4 to manage iOS dependencies

Usage

Sample Animation

Sequence Animation

If you set .sequence, sequence animation is shown.

  1. let animator = Animator(view: sampleView)
  2. animator
  3. .addBasicAnimation(keyPath: .positionX, from: 50, to: 150, duration: 2, timingFunction: .easeOutExpo)
  4. .addSpringAnimation(keyPath: .boundsSize, from: sampleView.frame.size, to: CGSize(width: 240, height: 240), damping: 12, mass: 1, stiffness: 240, initialVelocity: 0, duration: 1)
  5. .run(type: .sequence)

SequenceAnimation

Parallel Animation

If you set .parallel, parallel animation is shown.

  1. let animator = Animator(view: sampleView)
  2. animator
  3. .addBasicAnimation(keyPath: .positionX, from: 50, to: 150, duration: 5, timingFunction: .easeOutExpo)
  4. .addBasicAnimation(keyPath: .transformRotationZ, from: 0, to: CGFloat.pi, duration: 3, timingFunction: .easeOutExpo)
  5. .run(type: .parallel)

ParallelAnimation

Forever Animation

If you set forever before calling run, forever animation is shown.

  1. let animator = Animator(view: sampleView)
  2. animator
  3. .addBasicAnimation(keyPath: .positionX, from: 50, to: 150, duration: 2, timingFunction: .easeOutExpo)
  4. .addBasicAnimation(keyPath: .positionX, from: 150, to: 50, duration: 2, timingFunction: .easeOutExpo)
  5. .forever(autoreverses: false)
  6. .run(type: .sequence)

Forever

Cancel

If you want to cancel animation, you should call cancel.

  1. let animator = Animator(view: sampleView)
  2. /*
  3. Add animation and run
  4. */
  5. animator.cancel() // Animation cancel

Remove Added Animations

If you call run and then you call add animation method, no animation will be added.
If you use animator again, you call removeAll before addBasicAnimation or addSpringAnimation or addTransitionAnimation.

  1. let animator = Animator(view: sampleView)
  2. /*
  3. Add animation and run
  4. */
  5. // Bad
  6. animator.addBasicAnimation() // 🙅 you can't add animation
  7. // Good
  8. animator.removeAll()
  9. .addBasicAnimation() // 🙆 You can add animation.

Functions

Add Animation

  1. public func addBasicAnimation<T>(keyPath: Sica.AnimationKeyPath<T>, from: T, to: T, duration: Double, delay: Double = default, timingFunction: Sica.TimingFunction = default) -> Self where T : AnimationValueType
  2. public func addSpringAnimation<T>(keyPath: Sica.AnimationKeyPath<T>, from: T, to: T, damping: CGFloat, mass: CGFloat, stiffness: CGFloat, initialVelocity: CGFloat, duration: Double, delay: Double = default, timingFunction: Sica.TimingFunction = default) -> Self where T : AnimationValueType
  3. public func addTransitionAnimation(startProgress: Float, endProgress: Float, type: Sica.Transition, subtype: Sica.TransitionSub, duration: Double, delay: Double = default, timingFunction: Sica.TimingFunction = default) -> Self

Add Animation Option

  1. public func delay(_ delay: Double) -> Self
  2. public func forever(autoreverses: Bool = default) -> Self

Animation Operation

  1. public func run(type: Sica.Animator.AnimationPlayType, isRemovedOnCompletion: Bool = default, completion: (() -> Swift.Void)? = default)
  2. public func cancel()
  3. public func removeAll() -> Self

Extensions

You can access sica property in UIView and CALayer.

  1. let view = UIView(frame: ...)
  2. view.sica
  3. .addBasicAnimation(keyPath: .positionX, from: 50, to: 150, duration: 2, timingFunction: .easeOutExpo)
  4. .run(type: .sequence)
  1. let layer = CALayer()
  2. layer.sica
  3. .addBasicAnimation(keyPath: .positionX, from: 50, to: 150, duration: 2, timingFunction: .easeOutExpo)
  4. .run(type: .sequence)

Support

Animation

  • CABasicAnimation
  • CATransition
  • CASpringAnimation

AnimationPlayType

you can choose animation play type

  • run animation sequentially
  • run animation parallelly

EasingFunctions

you can choose various timing functions

EasingFunctions

KeyPaths Table

Sica KeyPath
.anchorPoint anchorPoint
.backgroundColor backgroundColor
.borderColor borderColor
.borderWidth borderWidth
.bounds bounds
.contents contents
.contentsRect contentsRect
.cornerRadius cornerRadius
.filters filters
.frame frame
.hidden hidden
.mask mask
.masksToBounds masksToBounds
.opacity opacity
.path path
.position position
.shadowColor shadowColor
.shadowOffset shadowOffset
.shadowOpacity shadowOpacity
.shadowPath shadowPath
.shadowRadius shadowRadius
.sublayers sublayers
.sublayerTransform sublayerTransform
.transform transform
.zPosition zPosition

Anchor Point

Sica KeyPath
.anchorPointX anchorPoint.x
.anchorPointy anchorPoint.y

Bounds

Sica KeyPath
.boundsOrigin bounds.origin
.boundsOriginX bounds.origin.x
.boundsOriginY bounds.origin.y
.boundsSize bounds.size
.boundsSizeWidth bounds.size.width
.boundsSizeHeight bounds.size.height

Contents

Sica KeyPath
.contentsRectOrigin contentsRect.origin
.contentsRectOriginX contentsRect.origin.x
.contentsRectOriginY contentsRect.origin.y
.contentsRectSize contentsRect.size
.contentsRectSizeWidth contentsRect.size.width
.contentsRectSizeHeight contentsRect.size.height

Frame

Sica KeyPath
.frameOrigin frame.origin
.frameOriginX frame.origin.x
.frameOriginY frame.origin.y
.frameSize frame.size
.frameSizeWidth frame.size.width
.frameSizeHeight frame.size.height

Position

Sica KeyPath
.positionX position.x
.positionY position.y

Shadow Offset

Sica KeyPath
.shadowOffsetWidth shadowOffset.width
.shadowOffsetHeight shadowOffset.height

Sublayer Transform

Sica KeyPath
.sublayerTransformRotationX sublayerTransform.rotation.x
.sublayerTransformRotationY sublayerTransform.rotation.y
.sublayerTransformRotationZ sublayerTransform.rotation.z
.sublayerTransformScaleX sublayerTransform.scale.x
.sublayerTransformScaleY sublayerTransform.scale.y
.sublayerTransformScaleZ sublayerTransform.scale.z
.sublayerTransformTranslationX sublayerTransform.translation.x
.sublayerTransformTranslationY sublayerTransform.translation.y
.sublayerTransformTranslationZ sublayerTransform.translation.z

Transform

Sica KeyPath
.transformRotationX transform.rotation.x
.transformRotationY transform.rotation.y
.transformRotationZ transform.rotation.z
.transformScaleX transform.scale.x
.transformScaleY transform.scale.y
.transformScaleZ transform.scale.z
.transformTranslationX transform.translation.x
.transformTranslationY transform.translation.y
.transformTranslationZ transform.translation.z

License

Sica is available under the MIT license. See the LICENSE file for more info.