The Material Motion Runtime is a tool for describing motion declaratively.
This library does not do much on its own. What it does do, however, is enable the expression of motion as discrete units of data that can be introspected, composed, and sent over a wire.
This library encourages you to describe motion as data, or what we call plans. Plans are committed to a scheduler. A scheduler coordinates the creation of performers, objects responsible for translating plans into concrete execution.
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 MaterialMotionRuntime
to your Podfile
:
pod 'MaterialMotionRuntime'
Then run the following command:
pod install
Import the Material Motion Runtime framework:
@import MaterialMotionRuntime;
You will now have access to all of the APIs.
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-runtime-objc.git
cd material-motion-runtime-objc
pod install
open MaterialMotionRuntime.xcworkspace
- Architecture
- How to define a new plan and performer type
- How to commit a plan to a scheduler
- How to configure performers with plans
- How to use composition to fulfill plans
- How to indicate continuous performance
The Material Motion Runtime consists of two groups of APIs: a scheduler/transaction object and a constellation of protocols loosely consisting of plan and performing types.
The Scheduler object is a coordinating entity whose primary responsibility is to fulfill plans by creating performers. You can create many schedulers throughout the lifetime of your application. A good rule of thumb is to have one scheduler per interaction or transition.
Transactions are the mechanism by which plans are committed to a scheduler. Transactions allow the runtime to minimize the API surface area of the scheduler while providing a vessel for plans to be transported within.
The Plan and Performing protocol each define the minimal characteristics required for an object to be considered either a plan or a performer, respectively, by the Material Motion Runtime.
Plans and performers have a symbiotic relationship. A plan is executed by the performer it defines. Performer behavior is configured by the provided plan instances.
Learn more about the Material Motion Runtime by reading the Starmap.
The following steps provide copy-pastable snippets of code.
Questions to ask yourself when creating a new plan type:
- What do I want my plan/performer to accomplish?
- Will my performer need many plans to achieve the desired outcome?
- How can I name my plan such that it clearly communicates either a behavior or a change in state?
As general rules:
- Plans with an -able suffix alter the behavior of the target, often indefinitely. Examples: Draggable, Pinchable, Tossable.
- Plans that are verbs describe some change in state, often over a period of time. Examples: FadeIn, Tween, SpringTo.
Code snippets:
In Objective-C:
@interface <#Plan#> : NSObject
@end
@implementation <#Plan#>
@end
In Swift:
class <#Plan#>: NSObject {
}
Performers are responsible for fulfilling plans. Fulfillment is possible in a variety of ways:
- PlanPerforming: How to configure performers with plans
- DelegatedPerforming
- ComposablePerforming: How to use composition to fulfill plans
See the associated links for more details on each performing type.
Note: only one instance of a type of performer per target is ever created. This allows you to register multiple plans to the same target in order to configure a performer. See How to configure performers with plans for more details.
Code snippets:
In Objective-C:
@interface <#Performer#> : NSObject <MDMPerforming>
@end
@implementation <#Performer#> {
UIView *_target;
}
- (instancetype)initWithTarget:(id)target {
self = [super init];
if (self) {
assert([target isKindOfClass:[UIView class]]);
_target = target;
}
return self;
}
@end
In Swift:
class <#Performer#>: NSObject, Performing {
let target: UIView
required init(target: Any) {
self.target = target as! UIView
super.init()
}
}
Conforming to Plan requires:
- that you define the type of performer your plan requires, and
- that your plan be copyable.
Code snippets:
In Objective-C:
@interface <#Plan#> : NSObject <MDMPlan>
@end
@implementation <#Plan#>
- (Class)performerClass {
return [<#Plan#> class];
}
- (id)copyWithZone:(NSZone *)zone {
return [[[self class] allocWithZone:zone] init];
}
@end
In Swift:
class <#Plan#>: NSObject, Plan {
func performerClass() -> AnyClass {
return <#Performer#>.self
}
func copy(with zone: NSZone? = nil) -> Any {
return <#Plan#>()
}
}
Code snippets:
In Objective-C:
@interface MyClass ()
@property(nonatomic, strong) MDMScheduler* scheduler;
@end
- (instancetype)init... {
...
self.scheduler = [MDMScheduler new];
...
}
In Swift:
class MyClass {
let scheduler = Scheduler()
}
Code snippets:
In Objective-C:
MDMTransaction *transaction = [MDMTransaction new];
[transaction addPlan:<#Plan instance#> toTarget:<#View instance#>];
In Swift:
let transaction = Transaction()
transaction.add(plan: <#Plan instance#>, to: <#View instance#>)
Code snippets:
In Objective-C:
[self.scheduler commitTransaction:transaction];
In Swift:
scheduler.commit(transaction: transaction)
Configuring performers with plans starts by making your performer conform to PlanPerforming.
PlanPerforming requires that you implement the addPlan:
method. This method will be called on a
performer each time a plan is committed to the scheduler that expects to be fulfilled by the
performer.
Code snippets:
In Objective-C:
@interface <#Performer#> (PlanPerforming) <MDMPlanPerforming>
@end
@implementation <#Performer#> (PlanPerforming)
- (void)addPlan:(id<MDMPlan>)plan {
<#Plan#>* <#casted plan instance#> = plan;
// Do something with the plan.
}
@end
In Swift:
extension <#Performer#>: PlanPerforming {
func add(plan: Plan) {
let <#casted plan instance#> = plan as! <#Plan#>
// Do something with the plan.
}
}
Handling multiple plan types in Swift:
Make use of Swift's typed switch/casing to handle multiple plan types.
func add(plan: Plan) {
switch plan {
case let <#plan instance 1#> as <#Plan type 1#>:
()
case let <#plan instance 2#> as <#Plan type 2#>:
()
case is <#Plan type 3#>:
()
default:
assert(false)
}
}
A composition performer is able to emit new transactions using an emitter object. This feature enables the reuse of plans and the creation of higher-order abstractions.
Code snippets:
In Objective-C:
@interface <#Performer#> ()
@property(nonatomic, strong) id<MDMTransactionEmitting> transactionEmitter;
@end
@interface <#Performer#> (Composition) <MDMComposablePerforming>
@end
@implementation <#Performer#> (Composition)
- (void)setTransactionEmitter:(id<MDMTransactionEmitting>)transactionEmitter {
self.transactionEmitter = transactionEmitter;
}
@end
In Swift:
// Store the emitter in your class' definition.
class <#Performer#>: ... {
...
var emitter: TransactionEmitting!
...
}
extension <#Performer#>: ComposablePerforming {
func set(transactionEmitter: TransactionEmitting) {
emitter = transactionEmitter
}
}
As a general practice performers should only associate plans with their target. If you find that a performer needs to associate plans with more than one target, you may want to consider whether a director is a more applicable place to put this logic.
Code snippets:
In Objective-C:
MDMTransaction *transaction = [MDMTransaction new];
[transaction addPlan:<#(nonnull id<MDMPlan>)#> toTarget:self.target];
[self.transactionEmitter emitTransaction:transaction];
In Swift:
let transaction = Transaction()
transaction.add(plan: <#T##Plan#>, to: target)
emitter.emit(transaction: transaction)
Oftentimes performers will perform their actions over a period of time or while an interaction is active. These types of performers are called continuous performers.
A continuous performer is able to affect the active state of the scheduler by generating is-active tokens. The scheduler is considered active so long as an is-active token exists and has not been terminated. Continuous performers are expected to terminate a token when its corresponding work has completed.
For example, a performer that registers a platform animation might generate a token when the animation starts. When the animation completes the token would be terminated.
Code snippets:
In Objective-C:
@interface <#Performer#> ()
@property(nonatomic, strong) id<MDMIsActiveTokenGenerating> tokenGenerator;
@end
@interface <#Performer#> (Composition) <MDMComposablePerforming>
@end
@implementation <#Performer#> (Composition)
- (void)setIsActiveTokenGenerator:(id<MDMIsActiveTokenGenerating>)isActiveTokenGenerator {
self.tokenGenerator = isActiveTokenGenerator;
}
@end
In Swift:
// Store the emitter in your class' definition.
class <#Performer#>: ... {
...
var tokenGenerator: IsActiveTokenGenerating!
...
}
extension <#Performer#>: ContinuousPerforming {
func set(isActiveTokenGenerator: IsActiveTokenGenerating) {
tokenGenerator = isActiveTokenGenerator
}
}
You will likely need to store the token in order to be able to reference it at a later point.
Code snippets:
In Objective-C:
id<MDMIsActiveTokenable> token = [self.tokenGenerator generate];
tokenMap[animation] = token;
In Swift:
let token = tokenGenerator.generate()!
tokenMap[animation] = token
Code snippets:
In Objective-C:
[token terminate];
In Swift:
token.terminate()
We welcome contributions!
Check out our upcoming milestones.
Learn more about our team, our community, and our contributor essentials.
Licensed under the Apache 2.0 license. See LICENSE for details.