Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ios): update transform converter and animation demo #4127

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 24 additions & 21 deletions modules/ios/domutils/HippyDomUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "HippyDefines.h"

#include <memory>

namespace hippy {
inline namespace dom {
struct LayoutResult;
class DomNode;
};
};
#include "dom/dom_listener.h"
#include "dom/dom_node.h"
#include "footstone/hippy_value.h"

NS_ASSUME_NONNULL_BEGIN

HIPPY_EXTERN CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result);

HIPPY_EXTERN UIEdgeInsets UIEdgeInsetsFromLayoutResult(hippy::LayoutResult result);

HIPPY_EXTERN CGSize CGSizeMakeFromLayoutResult(hippy::LayoutResult result);

HIPPY_EXTERN CGRect CGRectMakeFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode);

HIPPY_EXTERN NSDictionary *StylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode);
/// CGRect from hippy::LayoutResult
/// - Parameter result: CGRect
static inline CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result) {
return CGRectMake(result.left, result.top, result.width, result.height);
}

/// UIEdgeInsets from hippy::LayoutResult
/// - Parameter result: UIEdgeInsets
static inline UIEdgeInsets UIEdgeInsetsFromLayoutResult(hippy::LayoutResult result) {
return UIEdgeInsetsMake(result.paddingTop, result.paddingLeft, result.paddingBottom, result.paddingRight);
}

/// CGSize from hippy::LayoutResult
/// - Parameter result: CGSize
static inline CGSize CGSizeMakeFromLayoutResult(hippy::LayoutResult result) {
return CGSizeMake(result.width, result.height);
}

/// OC Props from hippy::DomNode
/// - Parameter domNode: hippy::DomNode
HIPPY_EXTERN NSDictionary *HippyStylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode);

NS_ASSUME_NONNULL_END
39 changes: 8 additions & 31 deletions modules/ios/domutils/HippyDomUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,27 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "HippyDomUtils.h"
#import "HippyFootstoneUtils.h"

#include "dom/dom_listener.h"
#include "dom/dom_node.h"
#include "footstone/hippy_value.h"

CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result) {
return CGRectMake(result.left, result.top, result.width, result.height);
}

UIEdgeInsets UIEdgeInsetsFromLayoutResult(hippy::LayoutResult result) {
return UIEdgeInsetsMake(result.paddingTop, result.paddingLeft, result.paddingBottom, result.paddingRight);
}

CGSize CGSizeMakeFromLayoutResult(hippy::LayoutResult result) {
return CGSizeMake(result.width, result.height);
}

CGRect CGRectMakeFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode) {
return CGRectMakeFromLayoutResult(domNode->GetLayoutResult());
}

NSDictionary *StylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode) {
NSDictionary *HippyStylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode) {
auto &styles = domNode->GetStyleMap();
auto &extStyles = domNode->GetExtStyle();
auto capacity = 0;

if (styles) {
capacity += styles->size();
capacity += styles->size();
}
if (extStyles) {
capacity += extStyles->size();
capacity += extStyles->size();
}
NSMutableDictionary *allStyles = [NSMutableDictionary dictionaryWithCapacity:capacity];
if (styles) {
NSDictionary *dicStyles = UnorderedMapDomValueToDictionary(styles);
[allStyles addEntriesFromDictionary:dicStyles];
NSDictionary *dicStyles = UnorderedMapDomValueToDictionary(styles);
[allStyles addEntriesFromDictionary:dicStyles];
}
if (extStyles) {
NSDictionary *dicExtStyles = UnorderedMapDomValueToDictionary(extStyles);
[allStyles addEntriesFromDictionary:dicExtStyles];
NSDictionary *dicExtStyles = UnorderedMapDomValueToDictionary(extStyles);
[allStyles addEntriesFromDictionary:dicExtStyles];
}
return [allStyles copy];
return allStyles;
}
5 changes: 3 additions & 2 deletions modules/ios/footstoneutils/HippyFootstoneUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,16 @@ id DomValueToOCType(const HippyValue *const pDomValue) {
return value;
}

NSDictionary *UnorderedMapDomValueToDictionary(const std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<HippyValue>>> &domValuesObject) {
NSDictionary *UnorderedMapDomValueToDictionary(const std::shared_ptr<std::unordered_map<std::string,
std::shared_ptr<HippyValue>>> &domValuesObject) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:domValuesObject->size()];
for (auto it = domValuesObject->begin(); it != domValuesObject->end(); it++) {
NSString *key = [NSString stringWithUTF8String:it->first.c_str()];
std::shared_ptr<HippyValue> domValue = it->second;
id value = DomValueToOCType(domValue.get());
[dic setObject:value forKey:key];
}
return [dic copy];
return dic;
}

NSNumber *DomValueToNumber(const HippyValue *const pDomValue) {
Expand Down
6 changes: 3 additions & 3 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,12 @@ - (HippyShadowView *)createShadowViewFromNode:(const std::shared_ptr<hippy::DomN
NSNumber *componentTag = @(domNode->GetId());
NSString *viewName = [NSString stringWithUTF8String:domNode->GetViewName().c_str()];
NSString *tagName = [NSString stringWithUTF8String:domNode->GetTagName().c_str()];
NSMutableDictionary *props = [StylesFromDomNode(domNode) mutableCopy];
NSMutableDictionary *props = [HippyStylesFromDomNode(domNode) mutableCopy];
HippyComponentData *componentData = [self componentDataForViewName:viewName];
HippyShadowView *shadowView = [componentData createShadowViewWithTag:componentTag];
shadowView.rootNode = rootNode;
NSAssert(componentData && shadowView, @"componentData and renderObject must not be nil");
[props setValue: rootTag forKey: @"rootTag"];
HippyAssert(componentData && shadowView, @"componentData and shadowView must not be nil");
[props setValue:rootTag forKey:@"rootTag"];
// Register shadow view
if (shadowView) {
shadowView.hippyTag = componentTag;
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
Loading