Open szmichaelyb opened 3 years ago
再做具体业务的时候,发现使用[LeeOpenAnimationConfig]配置自定义动画的时候,除了默认动画之外,还可以使用block动画,但还是略显样式单一,不能实现实际业务期望的动画,希望能支持帧动画。
[LeeOpenAnimationConfig]
containerView]
下面我描述一下帧动画的实现办法,仅为简单实现方式,希望作者能支持,采纳方案,或者实现更完善的解决方案,谢谢~
LEEAlertHelper.h文件
LEEAlertHelper.h
typedef LEEBaseConfigModel * _Nonnull (^LEEConfigToBlockAndBlock)(void(^)(UIView *containerView,void(^animatingBlock)(void),void(^animatedBlock)(void)));
LEEAlert.h文件
LEEAlert.h
@property (nonatomic, copy, readonly) LEEConfigToBlockAndBlock LeeOpenAnimationConfig;
LEEAlert.m文件
LEEAlert.m
@property (nonatomic, copy) void(^modelOpenAnimationConfigBlock)(UIView *containerView, void(^animatingBlock)(void), void(^animatedBlock)(void)); - (LEEConfigToBlockAndBlock)LeeOpenKeyAnimationConfig { return ^(void(^block)(UIView *containerView,void(^animatingBlock)(void),void(^animatedBlock)(void))){ self.modelOpenAnimationConfigBlock = block; return self; }; } /// 在 showAnimationsWithCompletionBlock 方法中 - (void)showAnimationsWithCompletionBlock:(void (^)(void))completionBlock { /// 前面代码省略 /// 帧动画相关 if (self.config.modelOpenAnimationConfigBlock) self.config.modelOpenAnimationConfigBlock(self.containerView,^{ if (!weakSelf) return ; if (weakSelf.config.modelBackgroundStyle == LEEBackgroundStyleTranslucent) { weakSelf.view.backgroundColor = [weakSelf.view.backgroundColor colorWithAlphaComponent:weakSelf.config.modelBackgroundStyleColorAlpha]; } else if (weakSelf.config.modelBackgroundStyle == LEEBackgroundStyleBlur) { weakSelf.backgroundVisualEffectView.effect = [UIBlurEffect effectWithStyle:weakSelf.config.modelBackgroundBlurEffectStyle]; } CGRect containerFrame = weakSelf.containerView.frame; containerFrame.origin.x = (viewWidth - containerFrame.size.width) * 0.5f; containerFrame.origin.y = (viewHeight - containerFrame.size.height) * 0.5f; weakSelf.containerView.frame = containerFrame; weakSelf.containerView.alpha = 1.0f; weakSelf.containerView.transform = CGAffineTransformIdentity; }, ^{ if (!weakSelf) return ; weakSelf.isShowing = NO; [weakSelf.view setUserInteractionEnabled:YES]; if (weakSelf.openFinishBlock) weakSelf.openFinishBlock(); if (completionBlock) completionBlock(); }); }
以前只能使用Block动画
.LeeOpenAnimationConfig(^(void (^animatingBlock)(void), void (^animatedBlock)(void)) { [UIView animateWithDuration:1.0f delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:1.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{ animatingBlock(); // 调用动画中Block } completion:^(BOOL finished) { animatedBlock(); // 调用动画结束Block }]; })
现在可以使用帧动画
/// 帧动画代码 - (void)animationForView:(UIView*)view { NSMutableArray * values = [NSMutableArray array]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.7, 0.7, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.95, 0.95, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.02, 1.02, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.duration = 0.5f; animation.removedOnCompletion = YES; animation.fillMode = kCAFillModeForwards; animation.values = values; [view.layer addAnimation:animation forKey:nil]; } /// 弹窗帧动画代码实现(改造后) @weakify(self) .LeeOpenAnimationConfig(^(UIView *containerView, void(^animatingBlock)(void), void(^animatedBlock)(void)) { @strongify(self) if(animatingBlock) animatingBlock(); // 调用动画中Block [self animationForView:containerView]; if(animatedBlock) animatedBlock(); // 调用动画结束Block })
为便于支持帧动画,建议将布局弹窗或者sheet的容器视图[containerView]返回给外界
1. 通过验证发现,只要将[
containerView]
返回给外界是可以实现帧动画的。LEEAlertHelper.h
文件LEEAlert.h
文件LEEAlert.m
文件2. 外界帧动画代码实现(举例)
以前只能使用Block动画
现在可以使用帧动画