This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the UIKit damping ratio APIs to their own class. (#31)
It was becoming difficult to document and test the behavior of the spring timing curve when it had both the explicit APIs and the UIKit dampingRatio variant in the same class. Making the UIKit API a generator simplifies the implementation.
- Loading branch information
Showing
6 changed files
with
179 additions
and
191 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
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,91 @@ | ||
/* | ||
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 "MDMTimingCurve.h" | ||
|
||
@class MDMSpringTimingCurve; | ||
|
||
/** | ||
A spring timing curve generator based on UIKit duration/dampingRatio-based coefficients. | ||
*/ | ||
@interface MDMSpringTimingCurveGenerator : NSObject <NSCopying, MDMTimingCurve> | ||
|
||
/** | ||
Initializes the timing curve with the given UIKit spring damping ratio. | ||
@param duration The desired duration of the spring animation. | ||
@param dampingRatio From the UIKit documentation: "When `dampingRatio` is 1, the animation will | ||
smoothly decelerate to its final model values without oscillating. Damping ratios less than 1 will | ||
oscillate more and more before coming to a complete stop." | ||
*/ | ||
- (nonnull instancetype)initWithDuration:(NSTimeInterval)duration | ||
dampingRatio:(CGFloat)dampingRatio; | ||
|
||
/** | ||
Initializes the timing curve with the given UIKit spring damping ratio and initial velocity. | ||
@param duration The desired duration of the spring animation. | ||
@param dampingRatio From the UIKit documentation: "When `dampingRatio` is 1, the animation will | ||
smoothly decelerate to its final model values without oscillating. Damping ratios less than 1 will | ||
oscillate more and more before coming to a complete stop." | ||
@param initialVelocity From the UIKit documentation: "You can use the initial spring velocity to | ||
specify how fast the object at the end of the simulated spring was moving before it was attached. | ||
It's a unit coordinate system, where 1 is defined as travelling the total animation distance in a | ||
second. So if you're changing an object's position by 200pt in this animation, and you want the | ||
animation to behave as if the object was moving at 100pt/s before the animation started, you'd | ||
pass 0.5. You'll typically want to pass 0 for the velocity." | ||
*/ | ||
- (nonnull instancetype)initWithDuration:(NSTimeInterval)duration | ||
dampingRatio:(CGFloat)dampingRatio | ||
initialVelocity:(CGFloat)initialVelocity | ||
NS_DESIGNATED_INITIALIZER; | ||
|
||
#pragma mark - Traits | ||
|
||
/** | ||
The desired duration of the spring animation. | ||
*/ | ||
@property(nonatomic, assign) NSTimeInterval duration; | ||
|
||
/** | ||
From the UIKit documentation: "When `dampingRatio` is 1, the animation will | ||
smoothly decelerate to its final model values without oscillating. Damping ratios less than 1 will | ||
oscillate more and more before coming to a complete stop." | ||
*/ | ||
@property(nonatomic, assign) CGFloat dampingRatio; | ||
|
||
/** | ||
From the UIKit documentation: "You can use the initial spring velocity to | ||
specify how fast the object at the end of the simulated spring was moving before it was attached. | ||
It's a unit coordinate system, where 1 is defined as travelling the total animation distance in a | ||
second. So if you're changing an object's position by 200pt in this animation, and you want the | ||
animation to behave as if the object was moving at 100pt/s before the animation started, you'd | ||
pass 0.5. You'll typically want to pass 0 for the velocity." | ||
*/ | ||
@property(nonatomic, assign) CGFloat initialVelocity; | ||
|
||
/** | ||
Creates and returns a new spring timing curve instance with the current configuration. | ||
*/ | ||
- (nonnull MDMSpringTimingCurve *)springTimingCurve; | ||
|
||
/** Unavailable. */ | ||
- (nonnull instancetype)init NS_UNAVAILABLE; | ||
|
||
@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,78 @@ | ||
/* | ||
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 "MDMSpringTimingCurveGenerator.h" | ||
|
||
#import "MDMSpringTimingCurve.h" | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@implementation MDMSpringTimingCurveGenerator | ||
|
||
- (instancetype)initWithDuration:(NSTimeInterval)duration dampingRatio:(CGFloat)dampingRatio { | ||
return [self initWithDuration:duration dampingRatio:dampingRatio initialVelocity:0]; | ||
} | ||
|
||
- (nonnull instancetype)initWithDuration:(NSTimeInterval)duration | ||
dampingRatio:(CGFloat)dampingRatio | ||
initialVelocity:(CGFloat)initialVelocity { | ||
self = [super init]; | ||
if (self) { | ||
_duration = duration; | ||
_dampingRatio = dampingRatio; | ||
_initialVelocity = initialVelocity; | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark - NSCopying | ||
|
||
- (id)copyWithZone:(NSZone *)zone { | ||
return [[[self class] allocWithZone:zone] initWithDuration:self.duration | ||
dampingRatio:self.dampingRatio | ||
initialVelocity:self.initialVelocity]; | ||
} | ||
|
||
- (MDMSpringTimingCurve *)springTimingCurve { | ||
UIView *view = [[UIView alloc] init]; | ||
[UIView animateWithDuration:self.duration | ||
delay:0 | ||
usingSpringWithDamping:self.dampingRatio | ||
initialSpringVelocity:self.initialVelocity | ||
options:0 | ||
animations:^{ | ||
view.center = CGPointMake(100, 100); | ||
} completion:nil]; | ||
|
||
NSString *animationKey = [view.layer.animationKeys firstObject]; | ||
NSAssert(animationKey != nil, @"Unable to extract animation timing curve: no animation found."); | ||
#pragma clang diagnostic push | ||
// CASpringAnimation is a private API on iOS 8 - we're able to make use of it because we're | ||
// linking against the public API on iOS 9+. | ||
#pragma clang diagnostic ignored "-Wpartial-availability" | ||
CASpringAnimation *springAnimation = | ||
(CASpringAnimation *)[view.layer animationForKey:animationKey]; | ||
NSAssert([springAnimation isKindOfClass:[CASpringAnimation class]], | ||
@"Unable to extract animation timing curve: unexpected animation type."); | ||
#pragma clang diagnostic pop | ||
|
||
return [[MDMSpringTimingCurve alloc] initWithMass:springAnimation.mass | ||
tension:springAnimation.stiffness | ||
friction:springAnimation.damping | ||
initialVelocity:self.initialVelocity]; | ||
} | ||
|
||
@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
Oops, something went wrong.