From d24b8a178927b501bdd0a6bca66135c87d9ee1f0 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 30 Nov 2019 18:30:46 +0800 Subject: [PATCH] feat:add iOS AnimationSet --- demo/src/AnimatorDemo.ts | 6 +- iOS/Pod/Classes/Shader/DoricViewNode.m | 92 ++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/demo/src/AnimatorDemo.ts b/demo/src/AnimatorDemo.ts index 7e87b27c..1b067fd5 100644 --- a/demo/src/AnimatorDemo.ts +++ b/demo/src/AnimatorDemo.ts @@ -173,19 +173,19 @@ class AnimatorDemo extends Panel { translate.toTranslationX = 200 translate.fromTranslationY = 10 translate.toTranslationY = 200 - translate.duration = 1000 + translate.duration = 3000 const scale = new ScaleAnimation scale.fromScaleX = 1 scale.toScaleX = 5 scale.fromScaleY = 1 scale.toScaleY = 5 - scale.duration = 1000 + scale.duration = 3000 const rotation = new RotationAnimation rotation.fromRotation = 0 rotation.toRotation = 6 - rotation.duration = 1000 + rotation.duration = 3000 animationSet.addAnimation(translate) animationSet.addAnimation(scale) diff --git a/iOS/Pod/Classes/Shader/DoricViewNode.m b/iOS/Pod/Classes/Shader/DoricViewNode.m index ffb39d7b..fc6f87ae 100644 --- a/iOS/Pod/Classes/Shader/DoricViewNode.m +++ b/iOS/Pod/Classes/Shader/DoricViewNode.m @@ -27,6 +27,7 @@ #import "DoricConstant.h" #import "DoricSuperNode.h" #import "DoricExtensions.h" +#import "DoricPromise.h" void DoricAddEllipticArcPath(CGMutablePathRef path, CGPoint origin, @@ -309,4 +310,95 @@ - (void)blendLayoutConfig:(NSDictionary *)params { } } +- (void)doAnimation:(id)params withPromise:(DoricPromise *)promise { + CAAnimation *animation = [self parseAnimation:params]; + animation.removedOnCompletion = NO; + animation.fillMode = kCAFillModeForwards; + [self.view.layer addAnimation:animation forKey:nil]; +} + +- (CAAnimation *)parseAnimation:(id)params { + if ([params isKindOfClass:[NSArray class]]) { + NSArray *anims = params; + CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; + NSMutableArray *animations = [NSMutableArray new]; + [anims forEach:^(id obj) { + [animations addObject:[self parseAnimation:obj]]; + }]; + animationGroup.animations = animations; + return animationGroup; + } else if ([params isKindOfClass:[NSDictionary class]]) { + NSArray *changeables = params[@"changeables"]; + NSString *type = params[@"type"]; + if ([@"TranslationAnimation" isEqualToString:type]) { + __block CGPoint from = self.view.layer.position; + __block CGPoint to = self.view.layer.position; + CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; + [changeables forEach:^(NSDictionary *obj) { + NSString *key = obj[@"key"]; + if ([@"translationX" isEqualToString:key]) { + from.x += [obj[@"fromValue"] floatValue]; + to.x += [obj[@"toValue"] floatValue]; + } else if ([@"translationY" isEqualToString:key]) { + from.y += [obj[@"fromValue"] floatValue]; + to.y += [obj[@"toValue"] floatValue]; + } + }]; + animation.fromValue = [NSValue valueWithCGPoint:from]; + animation.toValue = [NSValue valueWithCGPoint:to]; + return animation; + } else { + CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; + NSMutableArray *animations = [NSMutableArray new]; + [changeables forEach:^(NSDictionary *obj) { + [animations addObject:[self parseChangeable:obj]]; + }]; + animationGroup.animations = animations; + animationGroup.duration = [params[@"duration"] floatValue] / 1000; + if (params[@"delay"]) { + animationGroup.beginTime = CACurrentMediaTime() + [params[@"delay"] floatValue] / 1000; + } + + return animationGroup; + } + + + } + return nil; +} + +- (CAAnimation *)parseChangeable:(NSDictionary *)params { + NSString *key = params[@"key"]; + CABasicAnimation *animation = [CABasicAnimation animation]; + if ([@"scaleX" isEqualToString:key]) { + animation.keyPath = @"transform.scale.x"; + animation.fromValue = params[@"fromValue"]; + animation.toValue = params[@"toValue"]; + } else if ([@"scaleY" isEqualToString:key]) { + animation.keyPath = @"transform.scale.y"; + animation.fromValue = params[@"fromValue"]; + animation.toValue = params[@"toValue"]; + } else if ([@"rotation" isEqualToString:key]) { + animation.keyPath = @"transform.rotation.z"; + animation.fromValue = @([params[@"fromValue"] floatValue] * M_PI); + animation.toValue = @([params[@"toValue"] floatValue] * M_PI); + } else if ([@"bgColor" isEqualToString:key]) { + animation.keyPath = @"backgroundColor"; + animation.fromValue = params[@"fromValue"]; + animation.toValue = params[@"toValue"]; + } + if (params[@"repeatCount"]) { + NSInteger repeatCount = [params[@"repeatCount"] integerValue]; + if (repeatCount < 0) { + repeatCount = NSNotFound; + } + animation.repeatCount = repeatCount; + } + if (params[@"repeatMode"]) { + NSInteger repeatMode = [params[@"repeatMode"] integerValue]; + animation.autoreverses = repeatMode == 2; + } + return animation; +} + @end