Code Monkey home page Code Monkey logo

material-motion-swift's Introduction

Material Motion

Reactive motion driven by Core Animation.

Swift 3.1.x iOS 9.0+ Build Status codecov CocoaPods Compatible Platform Docs Chat

This library includes a variety of ready-to-use interactions. Interactions are registered to an instance of MotionRuntime:

// Store me for as long as the interactions should take effect.
let runtime = MotionRuntime(containerView: <#view#>)
InteractionSnippet
ArcMove
let arcMove = ArcMove()
arcMove.from.value = <#from#>
arcMove.to.value = <#to#>
runtime.add(arcMove, to: <#view#>)
ChangeDirection
runtime.add(ChangeDirection(withVelocityOf: gesture),
            to: <#direction#>)
DirectlyManipulable
runtime.add(DirectlyManipulable(), to: <#view#>)
Draggable
runtime.add(Draggable(), to: <#view#>)
Rotatable
runtime.add(Rotatable(), to: <#view#>)
Scalable
runtime.add(Scalable(), to: <#view#>)
SetPositionOnTap
runtime.add(SetPositionOnTap(),
            to: runtime.get(<#view#>.layer).position)
Spring
let spring = Spring()
spring.destination.value = <#initial destination#>
runtime.add(spring, to: <#view#>)
Tossable
let tossable = Tossable()
tossable.spring.destination.value = <#initial destination#>
runtime.add(tossable, to: <#view#>)
Tween
runtime.add(Tween(duration: 0.5, values: [1, 0]),
            to: runtime.get(<#view#>.layer).opacity)

Installation

Installation with CocoaPods

CocoaPods is a dependency manager for Objective-C and Swift libraries. CocoaPods automates the process of using third-party libraries in your projects. See the Getting Started guide for more information. You can install it with the following command:

gem install cocoapods

Add MaterialMotion to your Podfile:

pod 'MaterialMotion'

You will need to add use_frameworks! to your Podfile in order use Material Motion in your swift app.

A simple Podfile might look like so:

project 'MyApp/MyApp.xcodeproj'

use_frameworks!

target 'MyApp' do
  pod 'MaterialMotion'
end

Then run the following command:

pod install

Usage

Import the framework:

import MaterialMotion

You will now have access to all of the APIs.

Example apps/unit tests

Check out a local copy of the repo to access the Catalog application by running the following commands:

git clone https://github.com/material-motion/material-motion-swift.git
cd material-motion-swift
pod install
open MaterialMotion.xcworkspace

Case studies

Carousel

A carousel with pages that scale in and fade out in reaction to their scroll position.

View the source.

Contextual transition

A contextual view can be used to create continuity during transitions between view controllers. In this case study the selected photo is the contextual view. It's possible to flick the view to dismiss it using the tossable interaction.

Makes use of: Draggable, Tossable, Transition, TransitionSpring, Tween.

View the source.

Floating action button transition

A floating action button transition is a type of contextual transition that animates a mask outward from a floating button.

Makes use of: Transition and Tween.

View the source.

Material expansion

A Material Design transition using asymetric transformations.

Makes use of: Tween.

View the source.

Modal dialog

A modal dialog that's presented over the existing context and is dismissable using gestures.

Makes use of: Tossable and TransitionSpring.

View the source.

Pull down to dismiss

A modal scroll view controller that can be dismissed with a drag gesture.

Makes use of: Tossable and TransitionSpring.

View the source.

Sticker picker

Each sticker is individually directly manipulable, meaning they can be dragged, rotated, and scaled using multitouch gestures.

Makes use of: DirectlyManipulable.

View the source.

Contributing

We welcome contributions!

Check out our upcoming milestones.

Learn more about our team, our community, and our contributor essentials.

License

Licensed under the Apache 2.0 license. See LICENSE for details.

material-motion-swift's People

Contributors

appsforartists avatar jverkoey avatar randcode-generator avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

material-motion-swift's Issues

Javascript prototype for iOS

Goal: build a javascript bridge that can commit streams to a native MotionRuntime.

The bridge would ideally only be executed one time for a given director. E.g. a given transition might have a javascript bridge that can be used to drive the transition.

The javascript director will have a single mechanism for adding streams to a runtime, e.g. transition.runtime.write(to:)

Make it easier to use Int types throughout

We may want to treat Int values as CGFloat types throughout material motion in order to ease use of simple constants, e.g. 0 or 1 without requiring need for explicit typing, e.g. CGFloat(0) or CGFloat(1).

Fix this build failure

let spring = Spring(to: 1, threshold: 0.1, system: coreAnimation)

Ambiguous reference to member 'coreAnimation(_:initialValue:)'

Solution is to cast:

let spring = Spring(to: CGFloat(1), threshold: 0.1, system: coreAnimation)

But we should support using literal numbers as well.

Add support for tracing data moving down a stream

Unclear what this API would look like, but ideally there'd be some way to turn:

let someStream = gesture.translation().xLocked(to: 20)

into

gesture => { ... } => translation() => {50, 20} => xLocked(to: 50) => {20, 20}

or something similar. This would be emitted in real time to the console.

Some possible approaches:

  • Create an operator that traces anything upstream of it. E.g. gesture.translation().xLocked(to: 20).trace()

Formalize slop as an interaction

It's currently an operator and requires use of runtime.connect. Ideally it would be an interaction with the relevant configurable properties, e.g. slop size.

Challenge here is building an interaction that supports n-dimensional slopping, e.g. vertical/horizontal slop or slop outside of a 2d region.

Writes to position won't propagate to positionX or positionY

This is because each of these is a distinct property instance. This can be confusing to the end user. The "hack" fix is to always use the same property and apply constraints on it instead, e.g. position.x() and position.y().

Ideally the positionX and positionY properties would react to changes to position.

Note that changes to positionX and positionY do propagate to position because they use externalWrite to adjust the position property.

Resolving this would probably be a matter of having property do an external write to positionX and positionY, so long as this didn't cause a retain cycle.

Directly manipulable can't easily be added to a parent view and just affect the target view

Context: sticker picker case study.

The current case study registers directly manipulable to each sticker view directly. This ensures that each sticker view is individually manipulable, but doesn't allow you to pinch outside of a sticker in order to affect it.

Ideally it would be possible to register a single gesture recognizer set to the parent view and to only have them modify the top-most view at the centroid of the gestural interaction. This logic could be built into DirectlyManipulable as a feature with the following characteristics:

  1. Only one set of gesture recognizers is added to the parent view.
  2. The gesture recognizer delegate allows simultaneous recognition, as before.
  3. The gesture recognizer only begins if one of the target views is within the centroid of the gesture.
  4. Only the top-most target view is affected by the gesture.

runtime.get may lead to unexpected behavior when working with subclasses

runtime.get is currently making use of function overloading to create and cache reactive versions of non-reactive objects. This will create problems when working with sub-types, such as CAShapeLayer, because now the sub-type has its own cache. Consider the following example:

let shapeLayer = CAShapeLayer()
let layer: CALayer = shapeLayer
let reactiveShapeLayer = runtime.get(shapeLayer)
let reactiveLayer = runtime.get(layer)
// reactiveShapeLayer !== reactiveLayer

Note that even though we're using the same shape layer instance, we get a different reacitve object instance depending on the type of the variable we pass to runtime.get. This will lead to unexpected behavior in practice and we should consider alternative solutions to this.

Maintain consistency between ReactiveUIView's properties and its backing ReactiveCALayer's.

For example, if I write to a ReactiveUIView's backing ReactiveCALayer's position property the ReactiveUIView's center property should be updated as well (and vice versa).

A solution might be to use the layer's property directly if we can, and write a wrapper around it when it's not a 1:1 map:

public lazy var alpha: ReactiveProperty<CGFloat> = {
  return self.reactiveLayer.opacity
}()
public lazy var center: ReactiveProperty<CGPoint> = {
    let view = self.view
    let property = ReactiveProperty(initialValue: view.center,
                                    write: {
                                      // TODO: Adjust depending on anchor point
                                      self.reactiveLayer.position.value = $0
                                    }, coreAnimation: {event in
                                      self.reactiveLayer.position.coreAnimation(event) })
    let subscription = self.reactiveLayer.position.asStream().subscribe(next: { position in
      // TODO: Adjust depending on anchor point
      if position != property.value {
        property.value = position
      }
    }, state: { _ in }, coreAnimation: { _ in })
    self.subscriptions.append(subscription)
    return property
}()

Not easy to apply an impulse to make an object "bounce" with core animation

Consider the following interaction:

let spring = Spring<CGFloat>(threshold: 0.01, system: coreAnimation)
spring.initialVelocity.value = 10
spring.destination.value = 1
runtime.add(spring, to: runtime.get(square.layer).scale)

When using POP, this would behave as expected; the view would scale outward somewhat and eventually settle back at 1. This is because POP looks at the overall energy in the system to determine completeness.

When using Core Animation, however, this animation does nothing. This is because Core Animation thinks of velocity only relative to the distance to be traveled (in this case 0), so the animation ends up nooping.

I'm not sure whether there's a clear fix here, but I'll explore some hackfixes.

_map behaves oddly for additive Core Animation animation

It attempts to map the additive values, resulting in values that aren't compatible with the additive animation.

To resolve this we may need to store the pre-additive information in the animation channel and allow map to affect this. We can then calculate the additive information when the animation is committed to a layer.

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.