Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

* [ios] add perspective support for rotate3d #532

Merged
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
1 change: 1 addition & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@property (nonatomic, strong, readonly) WXLength *translateY;
@property (nonatomic, assign, readonly) float scaleX;
@property (nonatomic, assign, readonly) float scaleY;
@property (nonatomic, assign) float perspective;

- (instancetype)initWithCSSValue:(NSString *)cssValue origin:(NSString *)origin instance:(WXSDKInstance *)instance;

Expand Down
33 changes: 24 additions & 9 deletions ios/sdk/WeexSDK/Sources/Component/WXTransform.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ @implementation WXTransform
float _rotateX;
float _rotateY;
float _rotateZ;
float _perspective;

CATransform3D _nativeTransform;
BOOL _useNativeTransform;
Expand All @@ -59,6 +60,9 @@ - (instancetype)initWithCSSValue:(NSString *)cssValue origin:(NSString *)origin
_rotateZ = 0.0;
_rotateAngle = 0.0;

// default is parallel projection
_perspective = CGFLOAT_MAX;

[self parseTransform:cssValue];
[self parseTransformOrigin:origin];
}
Expand Down Expand Up @@ -147,19 +151,19 @@ - (CATransform3D)nativeTransformWithView:(UIView *)view
if (_useNativeTransform) {
return _nativeTransform;
}
CATransform3D nativeTransform3d = [self nativeTransformWithoutRotateWithView:view];

if (_rotateX != 0) {
CATransform3D rotateXTransform = CATransform3DMakeRotation(_rotateX, 1, 0, 0);
nativeTransform3d = CATransform3DConcat(nativeTransform3d, rotateXTransform);
CATransform3D nativeTransform3d = [self nativeTransformWithoutRotateWithView:view];

if (_rotateAngle != 0 || _rotateZ != 0) {
nativeTransform3d = CATransform3DRotate(nativeTransform3d, _rotateAngle?:_rotateZ, 0, 0, 1);
}

if (_rotateY != 0) {
CATransform3D rotateYTransform = CATransform3DMakeRotation(_rotateY, 0, 1, 0);
nativeTransform3d = CATransform3DConcat(nativeTransform3d, rotateYTransform);
nativeTransform3d = CATransform3DRotate(nativeTransform3d, _rotateY, 0, 1, 0);
}
if (_rotateAngle != 0 || _rotateZ != 0) {
CATransform3D rotateZTransform = CATransform3DMakeRotation(_rotateAngle?:_rotateZ, 0, 0, 1);
nativeTransform3d = CATransform3DConcat(nativeTransform3d, rotateZTransform);

if (_rotateX != 0) {
nativeTransform3d = CATransform3DRotate(nativeTransform3d, _rotateX, 1, 0, 0);
}

return nativeTransform3d;
Expand All @@ -169,6 +173,9 @@ - (CATransform3D)nativeTransformWithoutRotateWithView:(UIView *)view
{
CATransform3D nativeTansform3D = CATransform3DIdentity;

if (_perspective && !isinf(_perspective)) {
nativeTansform3D.m34 = -1.0/_perspective*[UIScreen mainScreen].scale;
}
if (!view || view.bounds.size.width <= 0 || view.bounds.size.height <= 0) {
return nativeTansform3D;
}
Expand Down Expand Up @@ -330,6 +337,14 @@ - (void)parseRotatez:(NSArray *)value
_rotateZ = [self getAngle:value[0]];
}

- (void)parsePerspective:(NSArray *)value
{
_perspective = [value[0] doubleValue];
if (_perspective <= 0 ) {
_perspective = CGFLOAT_MAX;
}
}

- (void)parseTranslate:(NSArray *)value
{
WXLength *translateX;
Expand Down
5 changes: 5 additions & 0 deletions ios/sdk/WeexSDK/Sources/Module/WXAnimationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ - (void)_createCAAnimation:(WXAnimationInfo *)info
[delegate applyTransform];
}
} else {
CATransform3D transform = layer.transform;
if (info.target->_transform.perspective && !isinf(info.target->_transform.perspective)) {
transform.m34 = -1.0/info.target->_transform.perspective*[UIScreen mainScreen].scale;
layer.transform = transform;
}
[layer addAnimation:animation forKey:info.propertyName];
}
}
Expand Down