Skip to content

Commit

Permalink
feat: add iOS 17+ spring animations (#13891)
Browse files Browse the repository at this point in the history
* feat: add spring animations

* fix(ios): fix typo in iOS < 17 cases
  • Loading branch information
hansemannn committed Jan 11, 2024
1 parent e364209 commit be053b0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
10 changes: 10 additions & 0 deletions apidoc/Titanium/UI/Animation.yml
Expand Up @@ -189,6 +189,16 @@ properties:
platforms: [iphone, ipad, macos]
since: "8.1.0"

- name: bounce
summary: The animation bounce. If set, the animation uses the iOS 17+ spring animation.
description: |
When `bounce` is 0, there are no bounces, positive values indicate increasing amounts of bounciness up to a maximum
of 1.0 (corresponding to undamped oscillation), and negative values indicate overdamped springs with a minimum value of -1.0.
type: Number
platforms: [iphone, ipad, macos]
since: "12.2.0"
osver: { ios: { min: "17.0" } }

- name: top
summary: Value of the `top` property at the end of the animation.
type: Number
Expand Down
1 change: 1 addition & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiAnimation.h
Expand Up @@ -139,6 +139,7 @@
@property (nonatomic, retain, readwrite) TiProxy *view;
@property (nonatomic, retain, readwrite) NSNumber *dampingRatio;
@property (nonatomic, retain, readwrite) NSNumber *springVelocity;
@property (nonatomic, retain, readwrite) NSNumber *bounce;

+ (TiAnimation *)animationFromArg:(id)args context:(id<TiEvaluator>)context create:(BOOL)yn;

Expand Down
32 changes: 24 additions & 8 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiAnimation.m
Expand Up @@ -23,7 +23,7 @@ @implementation TiAnimation
@synthesize delegate;
@synthesize zIndex, left, right, top, bottom, width, height;
@synthesize duration, color, backgroundColor, opacity, opaque, view;
@synthesize visible, curve, repeat, autoreverse, delay, transform, transition, dampingRatio, springVelocity;
@synthesize visible, curve, repeat, autoreverse, delay, transform, transition, dampingRatio, springVelocity, bounce;
@synthesize animatedView, callback, isReverse, reverseAnimation, resetState;

- (id)initWithDictionary:(NSDictionary *)properties_ context:(id<TiEvaluator>)context_ callback:(KrollCallback *)callback_
Expand Down Expand Up @@ -99,6 +99,7 @@ - (id)initWithDictionary:(NSDictionary *)properties_ context:(id<TiEvaluator>)co
SET_FLOAT_PROP(delay, properties);
SET_FLOAT_PROP(dampingRatio, properties);
SET_FLOAT_PROP(springVelocity, properties);
SET_FLOAT_PROP(bounce, properties);
SET_INT_PROP(curve, properties);
SET_INT_PROP(repeat, properties);
SET_BOOL_PROP(visible, properties);
Expand Down Expand Up @@ -159,6 +160,7 @@ - (void)dealloc
RELEASE_TO_NIL(view);
RELEASE_TO_NIL(dampingRatio);
RELEASE_TO_NIL(springVelocity);
RELEASE_TO_NIL(bounce);
RELEASE_TO_NIL(properties);
[animatedViewProxy release];
[super dealloc];
Expand Down Expand Up @@ -638,13 +640,27 @@ - (void)animate:(id)args
};

if (dampingRatio != nil || springVelocity != nil) {
[UIView animateWithDuration:animationDuration
delay:([delay doubleValue] / 1000)
usingSpringWithDamping:[dampingRatio floatValue]
initialSpringVelocity:[springVelocity floatValue]
options:options
animations:animation
completion:complete];
#ifdef __IPHONE_17_0
if ([TiUtils isIOSVersionOrGreater:@"17.0"] && bounce != nil) {
[UIView animateWithSpringDuration:animationDuration
bounce:[bounce floatValue]
initialSpringVelocity:[springVelocity floatValue]
delay:([delay doubleValue] / 1000)
options:options
animations:animation
completion:complete];
} else {
#endif
[UIView animateWithDuration:animationDuration
delay:([delay doubleValue] / 1000)
usingSpringWithDamping:[dampingRatio floatValue]
initialSpringVelocity:[springVelocity floatValue]
options:options
animations:animation
completion:complete];
#ifdef __IPHONE_17_0
}
#endif
} else {
[UIView animateWithDuration:animationDuration
delay:([delay doubleValue] / 1000)
Expand Down

0 comments on commit be053b0

Please sign in to comment.