Skip to content

Commit

Permalink
refactor(ios): update transform converter and animation demo
Browse files Browse the repository at this point in the history
add protect when handling buffer
  • Loading branch information
wwwcg committed Nov 19, 2024
1 parent 5a020db commit 56b7dfe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ export default class AnimationExample extends React.Component {
],
repeatCount: 'loop',
});
// TODO iOS暂不支持文字颜色渐变动画
this.txtColorAnimationSet = new AnimationSet({
children: [
{
Expand Down Expand Up @@ -565,7 +564,7 @@ export default class AnimationExample extends React.Component {
}]}
/>
</View>
<Text style={styles.title}>颜色渐变动画(文字渐变仅Android支持)</Text>
<Text style={styles.title}>颜色渐变动画</Text>
<View style={styles.buttonContainer}>
<View
style={styles.button}
Expand Down Expand Up @@ -611,8 +610,7 @@ export default class AnimationExample extends React.Component {
><Text ref={(ref) => {
this.textColorRef = ref;
}} style={[styles.colorText, {
// TODO iOS暂不支持文字颜色渐变动画
color: Platform.OS === 'android' ? this.txtColorAnimationSet : 'white',
color: this.txtColorAnimationSet,
}]}>颜色渐变背景和文字</Text></View>
</View>

Expand Down
3 changes: 1 addition & 2 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,7 @@ - (void)batchDidComplete {

- (void)handleBuffer:(NSArray *)buffer {
NSArray *requestsArray = [HippyConvert NSArray:buffer];

if (HIPPY_DEBUG && requestsArray.count <= HippyBridgeFieldParams) {
if (requestsArray.count <= HippyBridgeFieldParams) {
HippyLogError(@"Buffer should contain at least %tu sub-arrays. Only found %tu", HippyBridgeFieldParams + 1, requestsArray.count);
return;
}
Expand Down
26 changes: 16 additions & 10 deletions renderer/native/ios/utils/HippyConvert+NativeRender.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ + (CATransform3D)CATransform3D:(id)json {
}
// legacy matrix support
if ([(NSArray *)json count] == kMatrixArrayLength && [json[0] isKindOfClass:[NSNumber class]]) {
HippyLogWarn(
@"[HippyConvert CATransform3D:] has deprecated a matrix as input. Pass an array of configs (which can contain a matrix key) instead.");
HippyLogWarn(@"[HippyConvert CATransform3D:] has deprecated a matrix as input. \
Pass an array of configs (which can contain a matrix key) instead.");
return [self CATransform3DFromMatrix:json];
}

CGFloat zeroScaleThreshold = FLT_EPSILON;

CATransform3D next;
for (NSDictionary *transformConfig in (NSArray<NSDictionary *> *)json) {
if (transformConfig.count != 1) {
HippyLogError(@"[%@], a CATransform3D. You must specify exactly one property per transform object.", json);
Expand All @@ -91,10 +92,13 @@ + (CATransform3D)CATransform3D:(id)json {
NSString *property = transformConfig.allKeys[0];
id value = HippyNilIfNull(transformConfig[property]);
if ([property isEqualToString:@"matrix"]) {
transform = [self CATransform3DFromMatrix:value];
next = [self CATransform3DFromMatrix:value];
transform = CATransform3DConcat(next, transform);

} else if ([property isEqualToString:@"perspective"]) {
transform.m34 = -1 / [value floatValue];
next = CATransform3DIdentity;
next.m34 = -1 / [value floatValue];
transform = CATransform3DConcat(next, transform);

} else if ([property isEqualToString:@"rotateX"]) {
CGFloat rotate = [self convertToRadians:value];
Expand All @@ -111,18 +115,16 @@ + (CATransform3D)CATransform3D:(id)json {
} else if ([property isEqualToString:@"scale"]) {
CGFloat scale = [value floatValue];
scale = ABS(scale) < zeroScaleThreshold ? zeroScaleThreshold : scale;
transform.m34 = 0.f;
transform = CATransform3DScale(transform, scale, scale, scale);
transform = CATransform3DScale(transform, scale, scale, 1);

} else if ([property isEqualToString:@"scaleX"]) {
CGFloat scale = [value floatValue];
scale = ABS(scale) < zeroScaleThreshold ? zeroScaleThreshold : scale;
transform.m34 = 0.f;
transform = CATransform3DScale(transform, scale, 1, 1);

} else if ([property isEqualToString:@"scaleY"]) {
CGFloat scale = [value floatValue];
scale = ABS(scale) < zeroScaleThreshold ? zeroScaleThreshold : scale;
transform.m34 = 0.f;
transform = CATransform3DScale(transform, 1, scale, 1);

} else if ([property isEqualToString:@"translate"]) {
Expand All @@ -146,11 +148,15 @@ + (CATransform3D)CATransform3D:(id)json {

} else if ([property isEqualToString:@"skewX"]) {
CGFloat skew = [self convertToRadians:value];
transform.m21 = tanf(skew);
next = CATransform3DIdentity;
next.m21 = tanf(skew);
transform = CATransform3DConcat(next, transform);

} else if ([property isEqualToString:@"skewY"]) {
CGFloat skew = [self convertToRadians:value];
transform.m12 = tanf(skew);
next = CATransform3DIdentity;
next.m12 = tanf(skew);
transform = CATransform3DConcat(next, transform);

} else {
HippyLogError(@"Unsupported transform type for a CATransform3D: %@.", property);
Expand Down

0 comments on commit 56b7dfe

Please sign in to comment.