Material Motion Exploring solutions that will empower creators with the tools needed to describe and implement rich, interactive motion on any platform. Edit this page · History
MotionObservable · interactions Applying constraints to interactions
Status Interface level Implementation level Library
Stable as of April 18, 2017 L2: Interaction creator L4: Runtime engineering material-motion
UpdatesCurrent StatusInitiation dateCompletion date
Introduced spec. Stable April 19, 2017
platformsrctests
Android View
iOS (Swift) View

Applying constraints to interactions

This is the engineering specification for applying constraints to Interaction instances.

Overview

Constraints are operators that are applied to interactions being added to a MotionRuntime.

runtime.add(Draggable(), to: view, constraints: { $0.xLocked(to: 100) })

Constraints can be added to one or more streams connected by the interaction - which streams are ultimately affected is defined on a per-interaction basis and should be well-documented.

MVP

Add a constraints generic type to Interaction

The constraints type is generic in order to allow interactions to define custom constraint types. Add an optional constraints argument to Interaction.add’s API.

protocol Interaction {
  associatedtype Target
  associatedtype Constraints

  func add(to target: Target, withRuntime runtime: MotionRuntime, constraints: Constraints?)
}

Add a constraints argument to runtime.add

The argument should be typed on the Interaction’s generic Constraints type. The argument is optional and defaults to nil, indicating no constraints should be applied to the interaction.

class MotionRuntime {
  func add<I: Interaction>(_ interaction: I, to target: I.Target, constraints: I.Constraints? = nil)
}

Pass the constraints to the interaction instance on runtime.add

class MotionRuntime {
  func add<I: Interaction>(_ interaction: I, to target: I.Target, constraints: I.Constraints? = nil) {
    ...
    interaction.add(to: target, withRuntime: self, constraints: constraints)
    ...
  }
}