Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Implement v2 APIs (#22)
Browse files Browse the repository at this point in the history
In short: this PR drops the redundant "motion" prefix on various types and aligns naming with system terminology where possible and re-implements the APIs as Objective-C APIs, closing #5.

| Old API | New API | Rationale |
|:------- |:-------- |:-----------|
| MotionTiming | AnimationTraits | This structure is intended to describe animations only, not motion in general. |
| MotionCurve | TimingCurve | This brings the API name closer to the similarly-purposed `CAMediaTimingFunction`. MotionCurve could also be easily confused with motion through x/y space rather than through time (e.g. ArcMove), which will be problematic as we start defining paths of motion through space. |
| MotionRepetition | RepetitionTraits | This aligns the naming with AnimationTraits. |

All prior APIs have been deleted, including the macro-based APIs and any active deprecations. This is a major change and will bump the release to v2.

A migration script has been included to ease migration of existing code.
  • Loading branch information
jverkoey authored Dec 4, 2017
1 parent 866ec18 commit 3a28e22
Show file tree
Hide file tree
Showing 27 changed files with 1,013 additions and 526 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
objects = {

/* Begin PBXBuildFile section */
660248AE1FD1EE78004C0147 /* MDMSpringTimingCurve.swift in Sources */ = {isa = PBXBuildFile; fileRef = 660248AC1FD1EE78004C0147 /* MDMSpringTimingCurve.swift */; };
6619E1D91FA0ED0300F3AB25 /* MDMModalMovementTimingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6619E1D81FA0ED0300F3AB25 /* MDMModalMovementTimingTests.m */; };
663ED7C51EDF1F0C0096B2A9 /* ExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663ED7C01EDF1F0C0096B2A9 /* ExampleViewController.swift */; };
663ED7C61EDF1F0C0096B2A9 /* ExampleViews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663ED7C11EDF1F0C0096B2A9 /* ExampleViews.swift */; };
663ED7C71EDF1F0C0096B2A9 /* HexColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663ED7C21EDF1F0C0096B2A9 /* HexColor.swift */; };
663ED7C81EDF1F0C0096B2A9 /* Layout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663ED7C31EDF1F0C0096B2A9 /* Layout.swift */; };
663ED7C91EDF1F0C0096B2A9 /* ModalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663ED7C41EDF1F0C0096B2A9 /* ModalViewController.swift */; };
663ED8011EE628BA0096B2A9 /* MDMMotionCurveTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663ED8001EE628BA0096B2A9 /* MDMMotionCurveTests.swift */; };
663ED8031EE6299A0096B2A9 /* MDMMotionCurveTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 663ED8021EE6299A0096B2A9 /* MDMMotionCurveTests.m */; };
664C8C531FD5A555004ED471 /* CAMediaTimingFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664C8C521FD5A555004ED471 /* CAMediaTimingFunctionTests.swift */; };
664C8C551FD5A7B7004ED471 /* MDMRepetitionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664C8C541FD5A7B7004ED471 /* MDMRepetitionTests.swift */; };
664C8C571FD5A831004ED471 /* MDMRepetitionOverTimeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664C8C561FD5A831004ED471 /* MDMRepetitionOverTimeTests.swift */; };
664C8C591FD5A8AC004ED471 /* MDMAnimationTraitsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664C8C581FD5A8AC004ED471 /* MDMAnimationTraitsTests.swift */; };
666FAA841D384A6B000363DA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 666FAA831D384A6B000363DA /* AppDelegate.swift */; };
666FAA8B1D384A6B000363DA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 666FAA8A1D384A6B000363DA /* Assets.xcassets */; };
666FAA8E1D384A6B000363DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 666FAA8C1D384A6B000363DA /* LaunchScreen.storyboard */; };
Expand Down Expand Up @@ -47,14 +50,17 @@
09CEA5DEA01BA723D08D84E6 /* Pods-UnitTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.release.xcconfig"; path = "../../../Pods/Target Support Files/Pods-UnitTests/Pods-UnitTests.release.xcconfig"; sourceTree = "<group>"; };
2DE76D4D35953D836F578CDE /* Pods-MotionInterchangeCatalog.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MotionInterchangeCatalog.debug.xcconfig"; path = "../../../Pods/Target Support Files/Pods-MotionInterchangeCatalog/Pods-MotionInterchangeCatalog.debug.xcconfig"; sourceTree = "<group>"; };
4AAB8EBB088513D48896641A /* Pods-MotionInterchangeCatalog.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MotionInterchangeCatalog.release.xcconfig"; path = "../../../Pods/Target Support Files/Pods-MotionInterchangeCatalog/Pods-MotionInterchangeCatalog.release.xcconfig"; sourceTree = "<group>"; };
660248AC1FD1EE78004C0147 /* MDMSpringTimingCurve.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MDMSpringTimingCurve.swift; sourceTree = "<group>"; };
6619E1D81FA0ED0300F3AB25 /* MDMModalMovementTimingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MDMModalMovementTimingTests.m; sourceTree = "<group>"; };
663ED7C01EDF1F0C0096B2A9 /* ExampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleViewController.swift; sourceTree = "<group>"; };
663ED7C11EDF1F0C0096B2A9 /* ExampleViews.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleViews.swift; sourceTree = "<group>"; };
663ED7C21EDF1F0C0096B2A9 /* HexColor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HexColor.swift; sourceTree = "<group>"; };
663ED7C31EDF1F0C0096B2A9 /* Layout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Layout.swift; sourceTree = "<group>"; };
663ED7C41EDF1F0C0096B2A9 /* ModalViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModalViewController.swift; sourceTree = "<group>"; };
663ED8001EE628BA0096B2A9 /* MDMMotionCurveTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MDMMotionCurveTests.swift; sourceTree = "<group>"; };
663ED8021EE6299A0096B2A9 /* MDMMotionCurveTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MDMMotionCurveTests.m; sourceTree = "<group>"; };
664C8C521FD5A555004ED471 /* CAMediaTimingFunctionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CAMediaTimingFunctionTests.swift; sourceTree = "<group>"; };
664C8C541FD5A7B7004ED471 /* MDMRepetitionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDMRepetitionTests.swift; sourceTree = "<group>"; };
664C8C561FD5A831004ED471 /* MDMRepetitionOverTimeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDMRepetitionOverTimeTests.swift; sourceTree = "<group>"; };
664C8C581FD5A8AC004ED471 /* MDMAnimationTraitsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDMAnimationTraitsTests.swift; sourceTree = "<group>"; };
666FAA801D384A6B000363DA /* MotionInterchangeCatalog.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MotionInterchangeCatalog.app; sourceTree = BUILT_PRODUCTS_DIR; };
666FAA831D384A6B000363DA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = Catalog/AppDelegate.swift; sourceTree = "<group>"; };
666FAA8A1D384A6B000363DA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -162,9 +168,12 @@
666FAA971D384A6B000363DA /* tests */ = {
isa = PBXGroup;
children = (
663ED8001EE628BA0096B2A9 /* MDMMotionCurveTests.swift */,
663ED8021EE6299A0096B2A9 /* MDMMotionCurveTests.m */,
664C8C521FD5A555004ED471 /* CAMediaTimingFunctionTests.swift */,
664C8C581FD5A8AC004ED471 /* MDMAnimationTraitsTests.swift */,
6619E1D81FA0ED0300F3AB25 /* MDMModalMovementTimingTests.m */,
664C8C541FD5A7B7004ED471 /* MDMRepetitionTests.swift */,
664C8C561FD5A831004ED471 /* MDMRepetitionOverTimeTests.swift */,
660248AC1FD1EE78004C0147 /* MDMSpringTimingCurve.swift */,
);
name = tests;
path = ../../../tests/unit;
Expand Down Expand Up @@ -478,9 +487,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
663ED8031EE6299A0096B2A9 /* MDMMotionCurveTests.m in Sources */,
663ED8011EE628BA0096B2A9 /* MDMMotionCurveTests.swift in Sources */,
664C8C531FD5A555004ED471 /* CAMediaTimingFunctionTests.swift in Sources */,
660248AE1FD1EE78004C0147 /* MDMSpringTimingCurve.swift in Sources */,
6619E1D91FA0ED0300F3AB25 /* MDMModalMovementTimingTests.m in Sources */,
664C8C571FD5A831004ED471 /* MDMRepetitionOverTimeTests.swift in Sources */,
664C8C591FD5A8AC004ED471 /* MDMAnimationTraitsTests.swift in Sources */,
664C8C551FD5A7B7004ED471 /* MDMRepetitionTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
43 changes: 43 additions & 0 deletions scripts/v1_to_v2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
#
# Copyright 2017-present The Material Motion Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Migration script from v1 to v2 interchange APIs.

if [ "$#" -ne 1 ]; then
echo "Usage: $(basename $0) <path>"
exit 1
fi

search_path="$1"

replace_objc() {
find "$search_path" -type f -name "*.h" | xargs sed -i '' "$1"
find "$search_path" -type f -name "*.m" | xargs sed -i '' "$1"
}

replace_swift() {
find "$search_path" -type f -name "*.swift" | xargs sed -i '' "$1"
}

replace_all() {
replace_objc "$1"
replace_swift "$1"
}

replace_all "s/timing.curve/traits.timingCurve/g"
replace_all "s/traits.curve/traits.timingCurve/g"
replace_objc "s/MDMMotionTiming/MDMAnimationTraits */g"
replace_swift "s/MotionTiming/MDMAnimationTraits/g"
44 changes: 44 additions & 0 deletions src/CAMediaTimingFunction+MDMTimingCurve.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <QuartzCore/QuartzCore.h>
#import <Foundation/Foundation.h>

#import "MDMTimingCurve.h"

// A CAMediaTimingFunction is a timing curve - we simply define its conformity to our protocol here.
@interface CAMediaTimingFunction () <MDMTimingCurve>
@end

@interface CAMediaTimingFunction (MotionInterchangeExtension)

/**
Returns a instance of the timing function with its control points reversed.
*/
- (nonnull CAMediaTimingFunction *)mdm_reversed;

/**
Returns the first control point of the timing function.
*/
@property(nonatomic, assign, readonly) CGPoint mdm_point1;

/**
Returns the second control point of the timing function.
*/
@property(nonatomic, assign, readonly) CGPoint mdm_point2;

@end

49 changes: 49 additions & 0 deletions src/CAMediaTimingFunction+MDMTimingCurve.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "CAMediaTimingFunction+MDMTimingCurve.h"

@implementation CAMediaTimingFunction (MotionInterchangeExtension)

- (CAMediaTimingFunction *)mdm_reversed {
float pt1[2];
float pt2[2];
[self getControlPointAtIndex:1 values:pt1];
[self getControlPointAtIndex:2 values:pt2];

float reversedPt1[2];
float reversedPt2[2];
reversedPt1[0] = 1 - pt2[0];
reversedPt1[1] = 1 - pt2[1];
reversedPt2[0] = 1 - pt1[0];
reversedPt2[1] = 1 - pt1[1];
return [CAMediaTimingFunction functionWithControlPoints:reversedPt1[0] :reversedPt1[1]
:reversedPt2[0] :reversedPt2[1]];
}

- (CGPoint)mdm_point1 {
float point[2];
[self getControlPointAtIndex:1 values:point];
return CGPointMake(point[0], point[1]);
}

- (CGPoint)mdm_point2 {
float point[2];
[self getControlPointAtIndex:2 values:point];
return CGPointMake(point[0], point[1]);
}

@end
115 changes: 115 additions & 0 deletions src/MDMAnimationTraits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h>

#import "MDMRepetitionTraits.h"
#import "MDMTimingCurve.h"

/**
A generic representation of animation traits.
*/
@interface MDMAnimationTraits: NSObject

/**
Initializes the instance with the provided duration and kCAMediaTimingFunctionEaseInEaseOut timing
curve.
@param duration The animation will occur over this length of time, in seconds.
*/
- (nonnull instancetype)initWithDuration:(NSTimeInterval)duration;

/**
Initializes the instance with the provided duration, delay, and
kCAMediaTimingFunctionEaseInEaseOut timing curve.
@param delay The amount of time, in seconds, to wait before starting the animation.
@param duration The animation will occur over this length of time, in seconds, after the delay time
has passed.
*/
- (nonnull instancetype)initWithDelay:(NSTimeInterval)delay duration:(NSTimeInterval)duration;

/**
Initializes the instance with the provided duration, delay, and timing curve.
@param delay The amount of time, in seconds, to wait before starting the animation.
@param duration The animation will occur over this length of time, in seconds, after the delay time
has passed.
@param timingCurve If provided, defines the acceleration timing for the animation. If nil, the
animation will be treated as instant and the duration/delay will be ignored.
*/
- (nonnull instancetype)initWithDelay:(NSTimeInterval)delay
duration:(NSTimeInterval)duration
timingCurve:(nullable id<MDMTimingCurve>)timingCurve;

/**
Initializes an animation trait with the provided timing curve, duration, delay, and repetition.
@param duration The animation will occur over this length of time, in seconds, after the delay time
has passed.
@param delay The amount of time, in seconds, to wait before starting the animation.
@param timingCurve If provided, defines the acceleration timing for the animation. If nil, the
animation will be treated as instant and the duration/delay will be ignored.
@param repetition The repetition traits of the animation. Most often an instance of MDMRepetition
or MDMRepetitionOverTime. If nil, the animation will not repeat.
*/
- (nonnull instancetype)initWithDelay:(NSTimeInterval)delay
duration:(NSTimeInterval)duration
timingCurve:(nullable id<MDMTimingCurve>)timingCurve
repetition:(nullable id<MDMRepetitionTraits>)repetition
NS_DESIGNATED_INITIALIZER;

#pragma mark - Traits

/**
The amount of time, in seconds, before this animation's value interpolation should begin.
*/
@property(nonatomic, assign, readonly) NSTimeInterval delay;

/**
The amount of time, in seconds, over which this animation should interpolate between its values.
*/
@property(nonatomic, assign, readonly) NSTimeInterval duration;

/**
The velocity and acceleration of the animation over time.
*/
@property(nonatomic, strong, nullable, readonly) id<MDMTimingCurve> timingCurve;

/**
The repetition characteristics of the animation.
*/
@property(nonatomic, strong, nullable, readonly) id<MDMRepetitionTraits> repetition;

#pragma mark - Unavailable

/**
Unavailable.
*/
- (nonnull instancetype)init NS_UNAVAILABLE;

@end

@interface MDMAnimationTraits (SystemTraits)

/**
Animation traits for an iOS modal presentation slide animation.
*/
@property(nonatomic, class, strong, nonnull, readonly) MDMAnimationTraits *systemModalMovement;

@end

71 changes: 71 additions & 0 deletions src/MDMAnimationTraits.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "MDMAnimationTraits.h"

#import "CAMediaTimingFunction+MDMTimingCurve.h"
#import "MDMSpringTimingCurve.h"

@implementation MDMAnimationTraits

- (instancetype)init {
[self doesNotRecognizeSelector:_cmd];
return nil;
}

- (nonnull instancetype)initWithDuration:(NSTimeInterval)duration {
return [self initWithDelay:0 duration:duration];
}

- (instancetype)initWithDelay:(NSTimeInterval)delay duration:(NSTimeInterval)duration {
CAMediaTimingFunction *easeInOut =
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return [self initWithDelay:delay duration:duration timingCurve:easeInOut];
}

- (instancetype)initWithDelay:(NSTimeInterval)delay
duration:(NSTimeInterval)duration
timingCurve:(id<MDMTimingCurve>)timingCurve {
return [self initWithDelay:delay duration:duration timingCurve:timingCurve repetition:nil];
}

- (instancetype)initWithDelay:(NSTimeInterval)delay
duration:(NSTimeInterval)duration
timingCurve:(id<MDMTimingCurve>)timingCurve
repetition:(id<MDMRepetitionTraits>)repetition {
self = [super init];
if (self) {
_duration = duration;
_delay = delay;
_timingCurve = timingCurve;
_repetition = repetition;
}
return self;
}

@end

@implementation MDMAnimationTraits (SystemTraits)

+ (MDMAnimationTraits *)systemModalMovement {
MDMSpringTimingCurve *timingCurve = [[MDMSpringTimingCurve alloc] initWithMass:3
tension:1000
friction:500];
return [[MDMAnimationTraits alloc] initWithDelay:0 duration:0.500 timingCurve:timingCurve];
}

@end

Loading

0 comments on commit 3a28e22

Please sign in to comment.