Skip to content

Commit 4b00d09

Browse files
Support UIView block based animation methods.
1 parent 1e098b5 commit 4b00d09

File tree

6 files changed

+338
-10
lines changed

6 files changed

+338
-10
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// UIView+JavaScriptBridge.h
3+
// JavaScriptBridge
4+
//
5+
// Created by kishikawa katsumi on 2014/01/25.
6+
// Copyright (c) 2014 kishikawa katsumi. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIView (JavaScriptBridge)
12+
13+
@end
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
//
2+
// UIView+JavaScriptBridge.m
3+
// JavaScriptBridge
4+
//
5+
// Created by kishikawa katsumi on 2014/01/25.
6+
// Copyright (c) 2014 kishikawa katsumi. All rights reserved.
7+
//
8+
9+
#import "UIView+JavaScriptBridge.h"
10+
#import "JSBMessageForwarding.h"
11+
12+
@implementation UIView (JavaScriptBridge)
13+
14+
+ (void)__performWithoutAnimation:(JSValue *)function
15+
{
16+
JSContext *context = [JSContext currentContext];
17+
id currentSelf = context[@"self"];
18+
19+
void(^actionsWithoutAnimation)() = NULL;
20+
if (!function.isUndefined) {
21+
actionsWithoutAnimation = ^() {
22+
dispatchFunction(currentSelf, actionsWithoutAnimation, nil);
23+
};
24+
}
25+
26+
[self performWithoutAnimation:actionsWithoutAnimation];
27+
}
28+
29+
+ (void)__animateWithDuration:(NSTimeInterval)duration
30+
delay:(NSTimeInterval)delay
31+
options:(UIViewAnimationOptions)options
32+
animations:(JSValue *)animationsFunction
33+
completion:(JSValue *)completionFunction
34+
{
35+
JSContext *context = [JSContext currentContext];
36+
id currentSelf = context[@"self"];
37+
38+
void(^animations)() = NULL;
39+
if (!animationsFunction.isUndefined) {
40+
animations = ^() {
41+
dispatchFunction(currentSelf, animationsFunction, nil);
42+
};
43+
}
44+
45+
void(^completion)(BOOL) = NULL;
46+
if (!completionFunction.isUndefined) {
47+
completion = ^(BOOL finished) {
48+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
49+
if (finished) {
50+
[parameters addObject:@YES];
51+
} else {
52+
[parameters addObject:@NO];
53+
}
54+
55+
dispatchFunction(currentSelf, completionFunction, parameters);
56+
};
57+
}
58+
59+
[self animateWithDuration:duration delay:delay options:options animations:animations completion:completion];
60+
}
61+
62+
+ (void)__animateWithDuration:(NSTimeInterval)duration
63+
animations:(JSValue *)animationsFunction
64+
completion:(JSValue *)completionFunction
65+
{
66+
JSContext *context = [JSContext currentContext];
67+
id currentSelf = context[@"self"];
68+
69+
void(^animations)() = NULL;
70+
if (!animationsFunction.isUndefined) {
71+
animations = ^() {
72+
dispatchFunction(currentSelf, animationsFunction, nil);
73+
};
74+
}
75+
76+
void(^completion)(BOOL) = NULL;
77+
if (!completionFunction.isUndefined) {
78+
completion = ^(BOOL finished) {
79+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
80+
if (finished) {
81+
[parameters addObject:@YES];
82+
} else {
83+
[parameters addObject:@NO];
84+
}
85+
86+
dispatchFunction(currentSelf, completionFunction, parameters);
87+
};
88+
}
89+
90+
[self animateWithDuration:duration animations:animations completion:completion];
91+
}
92+
93+
+ (void)__animateWithDuration:(NSTimeInterval)duration animations:(JSValue *)animationsFunction
94+
{
95+
JSContext *context = [JSContext currentContext];
96+
id currentSelf = context[@"self"];
97+
98+
void(^animations)() = NULL;
99+
if (!animationsFunction.isUndefined) {
100+
animations = ^() {
101+
dispatchFunction(currentSelf, animationsFunction, nil);
102+
};
103+
}
104+
105+
[self animateWithDuration:duration animations:animations];
106+
}
107+
108+
+ (void)__animateWithDuration:(NSTimeInterval)duration
109+
delay:(NSTimeInterval)delay
110+
usingSpringWithDamping:(CGFloat)dampingRatio
111+
initialSpringVelocity:(CGFloat)velocity
112+
options:(UIViewAnimationOptions)options
113+
animations:(JSValue *)animationsFunction
114+
completion:(JSValue *)completionFunction
115+
{
116+
JSContext *context = [JSContext currentContext];
117+
id currentSelf = context[@"self"];
118+
119+
void(^animations)() = NULL;
120+
if (!animationsFunction.isUndefined) {
121+
animations = ^() {
122+
dispatchFunction(currentSelf, animationsFunction, nil);
123+
};
124+
}
125+
126+
void(^completion)(BOOL) = NULL;
127+
if (!completionFunction.isUndefined) {
128+
completion = ^(BOOL finished) {
129+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
130+
if (finished) {
131+
[parameters addObject:@YES];
132+
} else {
133+
[parameters addObject:@NO];
134+
}
135+
136+
dispatchFunction(currentSelf, completionFunction, parameters);
137+
};
138+
}
139+
140+
[self animateWithDuration:duration delay:delay usingSpringWithDamping:dampingRatio initialSpringVelocity:velocity options:options animations:animations completion:completion];
141+
}
142+
143+
+ (void)__transitionWithView:(UIView *)view
144+
duration:(NSTimeInterval)duration
145+
options:(UIViewAnimationOptions)options
146+
animations:(JSValue *)animationsFunction
147+
completion:(JSValue *)completionFunction
148+
{
149+
JSContext *context = [JSContext currentContext];
150+
id currentSelf = context[@"self"];
151+
152+
void(^animations)() = NULL;
153+
if (!animationsFunction.isUndefined) {
154+
animations = ^() {
155+
dispatchFunction(currentSelf, animationsFunction, nil);
156+
};
157+
}
158+
159+
void(^completion)(BOOL) = NULL;
160+
if (!completionFunction.isUndefined) {
161+
completion = ^(BOOL finished) {
162+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
163+
if (finished) {
164+
[parameters addObject:@YES];
165+
} else {
166+
[parameters addObject:@NO];
167+
}
168+
169+
dispatchFunction(currentSelf, completionFunction, parameters);
170+
};
171+
}
172+
173+
[self transitionWithView:view duration:duration options:options animations:animations completion:completion];
174+
}
175+
176+
+ (void)__transitionFromView:(UIView *)fromView
177+
toView:(UIView *)toView
178+
duration:(NSTimeInterval)duration
179+
options:(UIViewAnimationOptions)options
180+
completion:(JSValue *)completionFunction
181+
{
182+
JSContext *context = [JSContext currentContext];
183+
id currentSelf = context[@"self"];
184+
185+
void(^completion)(BOOL) = NULL;
186+
if (!completionFunction.isUndefined) {
187+
completion = ^(BOOL finished) {
188+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
189+
if (finished) {
190+
[parameters addObject:@YES];
191+
} else {
192+
[parameters addObject:@NO];
193+
}
194+
195+
dispatchFunction(currentSelf, completionFunction, parameters);
196+
};
197+
}
198+
199+
[self transitionFromView:fromView toView:toView duration:duration options:options completion:completion];
200+
}
201+
202+
+ (void)__performSystemAnimation:(UISystemAnimation)animation
203+
onViews:(NSArray *)views
204+
options:(UIViewAnimationOptions)options
205+
animations:(JSValue *)animationsFunction
206+
completion:(JSValue *)completionFunction
207+
{
208+
JSContext *context = [JSContext currentContext];
209+
id currentSelf = context[@"self"];
210+
211+
void(^parallelAnimations)() = NULL;
212+
if (!animationsFunction.isUndefined) {
213+
parallelAnimations = ^() {
214+
dispatchFunction(currentSelf, animationsFunction, nil);
215+
};
216+
}
217+
218+
void(^completion)(BOOL) = NULL;
219+
if (!completionFunction.isUndefined) {
220+
completion = ^(BOOL finished) {
221+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
222+
if (finished) {
223+
[parameters addObject:@YES];
224+
} else {
225+
[parameters addObject:@NO];
226+
}
227+
228+
dispatchFunction(currentSelf, completionFunction, parameters);
229+
};
230+
}
231+
232+
[self performSystemAnimation:animation onViews:views options:options animations:parallelAnimations completion:completion];
233+
}
234+
235+
+ (void)__animateKeyframesWithDuration:(NSTimeInterval)duration
236+
delay:(NSTimeInterval)delay
237+
options:(UIViewKeyframeAnimationOptions)options
238+
animations:(JSValue *)animationsFunction
239+
completion:(JSValue *)completionFunction
240+
{
241+
JSContext *context = [JSContext currentContext];
242+
id currentSelf = context[@"self"];
243+
244+
void(^animations)() = NULL;
245+
if (!animationsFunction.isUndefined) {
246+
animations = ^() {
247+
dispatchFunction(currentSelf, animationsFunction, nil);
248+
};
249+
}
250+
251+
void(^completion)(BOOL) = NULL;
252+
if (!completionFunction.isUndefined) {
253+
completion = ^(BOOL finished) {
254+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
255+
if (finished) {
256+
[parameters addObject:@YES];
257+
} else {
258+
[parameters addObject:@NO];
259+
}
260+
261+
dispatchFunction(currentSelf, completionFunction, parameters);
262+
};
263+
}
264+
265+
[self animateKeyframesWithDuration:duration delay:delay options:options animations:animations completion:completion];
266+
}
267+
268+
+ (void)__addKeyframeWithRelativeStartTime:(double)frameStartTime
269+
relativeDuration:(double)frameDuration
270+
animations:(JSValue *)animationsFunction
271+
{
272+
JSContext *context = [JSContext currentContext];
273+
id currentSelf = context[@"self"];
274+
275+
void(^animations)() = NULL;
276+
if (!animationsFunction.isUndefined) {
277+
animations = ^() {
278+
dispatchFunction(currentSelf, animationsFunction, nil);
279+
};
280+
}
281+
282+
[self addKeyframeWithRelativeStartTime:frameDuration relativeDuration:frameDuration animations:animations];
283+
}
284+
285+
@end

Classes/iOS/FrameworkSupport/UIKit/JSBUIView.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,26 @@
6161
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;
6262
+ (void)setAnimationsEnabled:(BOOL)enabled;
6363
+ (BOOL)areAnimationsEnabled;
64-
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation;
65-
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
66-
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
67-
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
68-
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
69-
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
70-
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;
71-
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray *)views options:(UIViewAnimationOptions)options animations:(void (^)(void))parallelAnimations completion:(void (^)(BOOL finished))completion;
72-
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
73-
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations;
64+
JSExportAs(performWithoutAnimation,
65+
+ (void)__performWithoutAnimation:(JSValue *)actionsWithoutAnimation);
66+
JSExportAs(animateWithDurationDelayOptionsAnimationsCompletion,
67+
+ (void)__animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(JSValue *)animations completion:(JSValue *)completion);
68+
JSExportAs(animateWithDurationAnimationsCompletion,
69+
+ (void)__animateWithDuration:(NSTimeInterval)duration animations:(JSValue *)animations completion:(JSValue *)completion);
70+
JSExportAs(animateWithDurationAnimations,
71+
+ (void)__animateWithDuration:(NSTimeInterval)duration animations:(JSValue *)animations);
72+
JSExportAs(animateWithDurationDelayUsingSpringWithDampingInitialSpringVelocityOptionsAnimationsCompletion,
73+
+ (void)__animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(JSValue *)animations completion:(JSValue *)completion);
74+
JSExportAs(transitionWithViewDurationOptionsAnimationsCompletion,
75+
+ (void)__transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(JSValue *)animations completion:(JSValue *)completion);
76+
JSExportAs(transitionFromViewToViewDurationOptionsCompletion,
77+
+ (void)__transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(JSValue *)completion);
78+
JSExportAs(performSystemAnimationOnViewsOptionsAnimationsCompletion,
79+
+ (void)__performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray *)views options:(UIViewAnimationOptions)options animations:(JSValue *)parallelAnimations completion:(JSValue *)completion);
80+
JSExportAs(animateKeyframesWithDurationDelayOptionsAnimationsCompletion,
81+
+ (void)__animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(JSValue *)animations completion:(JSValue *)completion);
82+
JSExportAs(addKeyframeWithRelativeStartTimeRelativeDurationAnimations,
83+
+ (void)__addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(JSValue *)animations);
7484
+ (BOOL)requiresConstraintBasedLayout;
7585

7686
- (id)initWithFrame:(CGRect)frame;

Examples/HelloWorld/HelloWorld.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
144BD83618938E2C00C7E217 /* UIView+JavaScriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 144BD83518938E2C00C7E217 /* UIView+JavaScriptBridge.m */; };
1011
149A45F018796C5D007C2CA7 /* JSBMessageForwarding.m in Sources */ = {isa = PBXBuildFile; fileRef = 149A45EF18796C5D007C2CA7 /* JSBMessageForwarding.m */; };
1112
149A45F718796C66007C2CA7 /* ALAsset+JavaScriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 149A45F218796C66007C2CA7 /* ALAsset+JavaScriptBridge.m */; };
1213
149A45F818796C66007C2CA7 /* ALAssetsGroup+JavaScriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 149A45F418796C66007C2CA7 /* ALAssetsGroup+JavaScriptBridge.m */; };
@@ -59,6 +60,8 @@
5960
/* End PBXBuildFile section */
6061

6162
/* Begin PBXFileReference section */
63+
144BD83418938E2C00C7E217 /* UIView+JavaScriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+JavaScriptBridge.h"; sourceTree = "<group>"; };
64+
144BD83518938E2C00C7E217 /* UIView+JavaScriptBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+JavaScriptBridge.m"; sourceTree = "<group>"; };
6265
149A45EF18796C5D007C2CA7 /* JSBMessageForwarding.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSBMessageForwarding.m; sourceTree = "<group>"; };
6366
149A45F118796C66007C2CA7 /* ALAsset+JavaScriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ALAsset+JavaScriptBridge.h"; sourceTree = "<group>"; };
6467
149A45F218796C66007C2CA7 /* ALAsset+JavaScriptBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "ALAsset+JavaScriptBridge.m"; sourceTree = "<group>"; };
@@ -864,6 +867,8 @@
864867
149A45EF18796C5D007C2CA7 /* JSBMessageForwarding.m */,
865868
149A46211879B4A6007C2CA7 /* NSURLConnection+JavaScriptBridge.h */,
866869
149A46221879B4A6007C2CA7 /* NSURLConnection+JavaScriptBridge.m */,
870+
144BD83418938E2C00C7E217 /* UIView+JavaScriptBridge.h */,
871+
144BD83518938E2C00C7E217 /* UIView+JavaScriptBridge.m */,
867872
14A0455F187829B4004831E8 /* UIControl+JavaScriptBridge.h */,
868873
14A04560187829B4004831E8 /* UIControl+JavaScriptBridge.m */,
869874
149A4617187984E8007C2CA7 /* ACAccountStore+JavaScriptBridge.h */,
@@ -1878,6 +1883,7 @@
18781883
14A04835187829CB004831E8 /* JSBCoreTelephony.m in Sources */,
18791884
14A0482B187829CB004831E8 /* JSBAccounts.m in Sources */,
18801885
14A0453D18782920004831E8 /* AppDelegate.m in Sources */,
1886+
144BD83618938E2C00C7E217 /* UIView+JavaScriptBridge.m in Sources */,
18811887
14A04833187829CB004831E8 /* JSBCoreMIDI.m in Sources */,
18821888
14A04830187829CB004831E8 /* JSBCoreData.m in Sources */,
18831889
14A0483D187829CB004831E8 /* JSBMessageUI.m in Sources */,

0 commit comments

Comments
 (0)