This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Spec: https://material-motion.gitbooks.io/material-motion-starmap/content/specifications/motion_family/tween.html Closes #7 Reviewers: O4 Material Motion Apple platform reviewers, O2 Material Motion, markwei Reviewed By: O4 Material Motion Apple platform reviewers, O2 Material Motion, markwei Subscribers: markwei Tags: #material_motion Differential Revision: http://codereview.cc/D1683
- Loading branch information
Jeff Verkoeyen
committed
Oct 6, 2016
1 parent
be727d7
commit 0f2a038
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright 2016-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 MaterialMotionRuntime | ||
|
||
/** Interpolate a CALayer property from one value to another. */ | ||
@objc(MDMTween) | ||
public final class Tween: NSObject, Plan { | ||
/** The key path of the property whose value will be tweened. */ | ||
public var keyPath: String | ||
|
||
/** The duration of the animation in seconds. */ | ||
public var duration: CFTimeInterval | ||
|
||
/** The delay of the animation in seconds. */ | ||
public var delay: CFTimeInterval = 0 | ||
|
||
/** The initial value of the tween. See CABasicAnimation documentation for more details. */ | ||
public var from: Any? | ||
|
||
/** The final value of the tween. See CABasicAnimation documentation for more details. */ | ||
public var to: Any? | ||
|
||
/** | ||
The timing function to apply to the animation. | ||
|
||
A nil timing function indicates linear pacing. | ||
*/ | ||
public var timingFunction: CAMediaTimingFunction? | ||
|
||
@objc(initWithKeyPath:duration:) | ||
public init(_ keyPath: String, duration: CFTimeInterval) { | ||
self.keyPath = keyPath | ||
self.duration = duration | ||
super.init() | ||
} | ||
|
||
/** The performer that will fulfill this plan. */ | ||
public func performerClass() -> AnyClass { | ||
return TweenPerformer.self | ||
} | ||
/** Returns a copy of this plan. */ | ||
public func copy(with zone: NSZone? = nil) -> Any { | ||
let tween = Tween(keyPath, duration: duration) | ||
tween.from = from | ||
tween.to = to | ||
tween.timingFunction = timingFunction | ||
tween.delay = delay | ||
return tween | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright 2016-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 UIKit | ||
import MaterialMotionRuntime | ||
|
||
class TweenPerformer: NSObject, PlanPerforming, ComposablePerforming { | ||
let target: CALayer | ||
required init(target: Any) { | ||
if let view = target as? UIView { | ||
self.target = view.layer | ||
} else { | ||
self.target = target as! CALayer | ||
} | ||
} | ||
|
||
func add(plan: Plan) { | ||
let tween = plan as! Tween | ||
|
||
let animation = CABasicAnimation(keyPath: tween.keyPath) | ||
animation.fromValue = tween.from | ||
animation.toValue = tween.to | ||
animation.timingFunction = tween.timingFunction | ||
animation.duration = tween.duration | ||
animation.beginTime = tween.delay | ||
|
||
let transaction = Transaction() | ||
transaction.add(plan: animation, to: target) | ||
emitter.emit(transaction: transaction) | ||
} | ||
|
||
var emitter: TransactionEmitting! | ||
func set(transactionEmitter: TransactionEmitting) { | ||
emitter = transactionEmitter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2016-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 <XCTest/XCTest.h> | ||
|
||
@import MaterialMotionCoreAnimationFamily; | ||
|
||
@interface ObjectiveCAPITests : XCTestCase | ||
@end | ||
|
||
@implementation ObjectiveCAPITests | ||
|
||
- (void)testTweenAPI { | ||
MDMTween *tween = [[MDMTween alloc] initWithKeyPath:@"opacity" duration:0.1]; | ||
tween.delay = 0.1; | ||
tween.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; | ||
tween.from = @2; | ||
tween.to = @2; | ||
XCTAssertEqual(tween.keyPath, @"opacity"); | ||
XCTAssertEqual(tween.duration, 0.1); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2016-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 XCTest | ||
import MaterialMotionRuntime | ||
import MaterialMotionCoreAnimationFamily | ||
|
||
class TweenTests: XCTestCase { | ||
|
||
func testDidPerformAndIdle() { | ||
let animation = Tween("opacity", duration: 0.1) | ||
|
||
let view = UIView() | ||
|
||
let scheduler = Scheduler() | ||
let delegate = TestableSchedulerDelegate() | ||
delegate.didIdleExpectation = expectation(description: "Did idle") | ||
scheduler.delegate = delegate | ||
|
||
let transaction = Transaction() | ||
transaction.add(plan: animation, to: view) | ||
scheduler.commit(transaction: transaction) | ||
|
||
waitForExpectations(timeout: 0.3) | ||
|
||
XCTAssertEqual(scheduler.activityState, .idle) | ||
} | ||
} |