From 7c9e13158d0ea79a502e015f28fea8d7b65b18f4 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 26 Nov 2021 11:03:38 +0800 Subject: [PATCH] iOS: Use Animator to control blur effect --- .../pub/doric/shader/AeroEffectViewNode.java | 4 +- .../Classes/Shader/DoricAeroEffectViewNode.m | 1 + .../Classes/Shader/DoricBlurEffectViewNode.m | 48 ++++++++++++++++--- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/AeroEffectViewNode.java b/doric-android/doric/src/main/java/pub/doric/shader/AeroEffectViewNode.java index 368ddfd5..ecb54bd7 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/AeroEffectViewNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/AeroEffectViewNode.java @@ -52,9 +52,9 @@ public class AeroEffectViewNode extends StackNode { ((AeroEffectView) view).setEffectiveRect(new Rect(x, y, x + width, y + height)); } } else if ("style".equals(name)) { - if(prop.isString()){ + if (prop.isString()) { ((AeroEffectView) view).setStyle(prop.asString().value()); - }else{ + } else { ((AeroEffectView) view).setStyle(null); } } else { diff --git a/doric-iOS/Pod/Classes/Shader/DoricAeroEffectViewNode.m b/doric-iOS/Pod/Classes/Shader/DoricAeroEffectViewNode.m index bab7de69..55823dd1 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricAeroEffectViewNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricAeroEffectViewNode.m @@ -29,6 +29,7 @@ - (UIView *)build { UIView *ret = [super build]; UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:effect]; + self.visualEffectView.userInteractionEnabled = NO; [ret addSubview:self.visualEffectView]; self.visualEffectView.doricLayout.widthSpec = DoricLayoutMost; self.visualEffectView.doricLayout.heightSpec = DoricLayoutMost; diff --git a/doric-iOS/Pod/Classes/Shader/DoricBlurEffectViewNode.m b/doric-iOS/Pod/Classes/Shader/DoricBlurEffectViewNode.m index 23bf5ff6..2f87fe97 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricBlurEffectViewNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricBlurEffectViewNode.m @@ -20,18 +20,52 @@ #import #import "DoricBlurEffectViewNode.h" + +@interface DoricBlurEffectView : UIVisualEffectView +@property(nonatomic, strong) UIViewPropertyAnimator *animator; +@property(nonatomic, assign) NSUInteger radius; +@end + +@implementation DoricBlurEffectView +- (instancetype)initWithEffect:(UIVisualEffect *)effect { + if (self = [super initWithEffect:effect]) { + _animator = [[UIViewPropertyAnimator alloc] + initWithDuration:1 + curve:UIViewAnimationCurveLinear + animations:^{ + self.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; + }]; + _animator.fractionComplete = MIN(1, MAX(0, 15.0f / 200)); + if (@available(iOS 11, *)) { + _animator.pausesOnCompletion = YES; + } + } + return self; +} + +- (void)didMoveToWindow { + [super didMoveToWindow]; +} + +- (void)setRadius:(NSUInteger)radius { + _radius = radius; + self.animator.fractionComplete = MIN(1, MAX(0, (radius / 200.f))); +} + +- (void)dealloc { + [self.animator stopAnimation:YES]; +} +@end + @interface DoricBlurEffectViewNode () -@property(nonatomic, strong) UIVisualEffectView *visualEffectView; +@property(nonatomic, strong) DoricBlurEffectView *visualEffectView; @end @implementation DoricBlurEffectViewNode - - (UIView *)build { UIView *ret = [super build]; - UIVisualEffect *endEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; - - self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:endEffect]; - + self.visualEffectView = [[DoricBlurEffectView alloc] initWithEffect:nil]; + self.visualEffectView.userInteractionEnabled = NO; [ret addSubview:self.visualEffectView]; self.visualEffectView.doricLayout.widthSpec = DoricLayoutMost; self.visualEffectView.doricLayout.heightSpec = DoricLayoutMost; @@ -40,7 +74,7 @@ - (UIView *)build { - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop { if ([name isEqualToString:@"radius"]) { - + self.visualEffectView.radius = ((NSNumber *) prop).unsignedIntValue; } else if ([name isEqualToString:@"effectiveRect"]) { NSUInteger x = ((NSNumber *) prop[@"x"]).unsignedIntegerValue; NSUInteger y = ((NSNumber *) prop[@"y"]).unsignedIntegerValue;