Skip to content

Commit

Permalink
增加动画、优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Gdxy committed Nov 26, 2018
1 parent bcef6ab commit f53177f
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 16 deletions.
34 changes: 27 additions & 7 deletions Example/Example/TLFirstTableController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ - (void)viewDidLoad {
NSString *tempStr = _isPush ? @"斜角切入(不支持)" : @"斜角切入";
title = @"个人动画收集";
rows = @[@"开门",@"绽放",tempStr,@"向右边倾斜旋转",@"向左边倾斜旋转",@"指定frame:initialFrame --> finalFrame",
@"对指定rect范围,进行缩放和平移",@"对指定rect范围...2[纯净版]"];
@"对指定rect范围,进行缩放和平移",@"对指定rect范围...2[纯净版]",@"圆形"];
}
break;
}
Expand Down Expand Up @@ -619,23 +619,34 @@ - (void)checkerboardAnimateTransition:(id<UIViewControllerContextTransitioning>)
#pragma mark TLAnimator(个人收集)
- (void)presentByTLAnimator:(NSIndexPath *)indexPath {
TLAnimatorType type = indexPath.row;
if (indexPath.row == TLAnimatorRectScale + 1) type = TLAnimatorRectScale;
if (indexPath.row > TLAnimatorRectScale + 1) type = indexPath.row - 1;
TLSecondViewController *vc = [[TLSecondViewController alloc] init];
TLAnimator *animator = [TLAnimator animatorWithType:type];
animator.transitionDuration = 1.f;
// animator.transitionDuration = 1.f;

if (type == TLAnimatorTypeFrame) {

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGRect frame = [self.tableView convertRect:cell.frame toView:[UIApplication sharedApplication].keyWindow];
animator.initialFrame = frame;

}else if (type == TLAnimatorRectScale) {

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGRect frame = [cell convertRect:cell.imageView.frame toView:[UIApplication sharedApplication].keyWindow];
animator.fromRect = frame;
animator.toRect = CGRectMake(0, tl_ScreenH - 210, tl_ScreenW, 210);
animator.isOnlyShowRangeForRect = indexPath.row > TLAnimatorRectScale;

vc.isShowImage = YES;

}else if (type == TLAnimatorCircular) {

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGPoint center = [self.tableView convertPoint:cell.center toView:[UIApplication sharedApplication].keyWindow];
center.x = arc4random_uniform(cell.bounds.size.width - 40) + 20;
animator.center = center;
}

[self presentViewController:vc animator:animator completion:^{
tl_LogFunc;
}];
Expand Down Expand Up @@ -666,7 +677,7 @@ - (void)pushByCATransition:(NSIndexPath *)indexPath {

TLSecondViewController *vc = [[TLSecondViewController alloc] init];
TLCATransitonAnimator *animator = [self CATransitionAnimatorWithIndexPath:indexPath toViewController:vc];
animator.transitionDuration = 3.0;
animator.transitionDuration = 0.5;
[self pushViewController:vc animator:animator];

}
Expand Down Expand Up @@ -700,13 +711,15 @@ - (void)pushByTLAnimation:(NSIndexPath *)indexPath {
TLSecondViewController *vc = [[TLSecondViewController alloc] init];

TLAnimatorType type = indexPath.row;
if (indexPath.row == TLAnimatorRectScale + 1) type = TLAnimatorRectScale;
if (indexPath.row > TLAnimatorRectScale + 1) type = indexPath.row - 1;
TLAnimator *animator = [TLAnimator animatorWithType:type];
animator.transitionDuration = 1.0f;
// animator.transitionDuration = 0.5f;

if (type == TLAnimatorTypeFrame) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGRect frame = [self.tableView convertRect:cell.frame toView:[UIApplication sharedApplication].keyWindow];
animator.initialFrame = frame;

}else if (type == TLAnimatorRectScale) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGRect frame = [cell convertRect:cell.imageView.frame toView:[UIApplication sharedApplication].keyWindow];
Expand All @@ -716,6 +729,13 @@ - (void)pushByTLAnimation:(NSIndexPath *)indexPath {
animator.isOnlyShowRangeForRect = indexPath.row > TLAnimatorRectScale;

vc.isShowImage = YES;

}else if (type == TLAnimatorCircular) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGPoint center = [self.tableView convertPoint:cell.center toView:[UIApplication sharedApplication].keyWindow];
center.x = arc4random_uniform(cell.bounds.size.width - 40) + 20;
animator.center = center;
animator.startRadius = cell.bounds.size.height / 2;
}
[self pushViewController:vc animator:animator];
}
Expand Down
7 changes: 7 additions & 0 deletions Example/Example/TLMenuViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ - (void)alertType:(UIView *)sender {
CGRect bounds = CGRectMake(0, 0, self.view.bounds.size.width * 0.8f, 200.f);
UIView *bView = [self creatViewWithBounds:bounds color:tl_Color(218, 248, 120)];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[bView addGestureRecognizer:tap];

UITextField *textFiled = [[UITextField alloc] init];
textFiled.backgroundColor = tl_Color(255, 255, 255);
textFiled.bounds = CGRectMake(0, 0, bView.bounds.size.width * 0.8f, 30.f);
Expand All @@ -167,6 +170,10 @@ - (void)alertType:(UIView *)sender {
[TLTransition showView:bView popType:TLPopTypeAlert];
}

- (void)tap:(UITapGestureRecognizer *)tap {
[tap.view endEditing:YES];
}

// TLPopTypeActionSheet
- (void)actionSheetType:(UIView *)sender {
CGRect bounds = CGRectMake(0, 0, self.view.bounds.size.width, 300.f);
Expand Down
15 changes: 11 additions & 4 deletions TLTransitions/TransitionController/Animator/TLAnimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@ typedef enum : NSUInteger {
TLAnimatorTypeTiltRight, // 向右边倾斜旋转
TLAnimatorTypeTiltLeft, // 向左边倾斜旋转
TLAnimatorTypeFrame, // 指定初始frame和最终frame(相对keyWindow)【需根据情况对initialFrame、finalFrame初始化】
TLAnimatorRectScale // 指定一个rect范围,对其进行缩放和平移【需对fromRect、toRect初始化】
TLAnimatorRectScale, // 指定一个rect范围,对其进行缩放和平移【需对fromRect、toRect初始化】,【Push时rectView初始化】
TLAnimatorCircular, // 圆形转场,可以指定center(默认,屏幕中心)
} TLAnimatorType;

NS_ASSUME_NONNULL_BEGIN

@interface TLAnimator : NSObject <TLAnimatorProtocol>
@property(nonatomic, assign) TLAnimatorType type;

// 仅在TLAnimatorTypeFrame模式下有效
//****** 仅在TLAnimatorTypeFrame模式下有效 ******//
/// 开始转场的frame,默认:[center, sizeZero]
@property(nonatomic, assign) CGRect initialFrame;
/// 转场结束的frame,默认:系统默认.
@property(nonatomic, assign) CGRect finalFrame;

// 仅在TLAnimatorRectScale模式下有效
//****** 仅在TLAnimatorRectScale模式下有效 ******//
/// 开始转场的frame,默认:[center, sizeZero]
@property(nonatomic, assign) CGRect fromRect;
/// 转场结束的frame,默认:系统默认.
/// 转场结束的frame,默认:系统默认finalFrame.
@property(nonatomic, assign) CGRect toRect;
/// 只显示和缩放fromRect区域,其他区域隐藏。默认:NO,fromRect区域以外也显示,并跟随缩放平移
@property(nonatomic, assign) BOOL isOnlyShowRangeForRect;
/// 缩放平移的View[rectView = 赋值的Viewf的快照],Push的时候必传(push时没能截图成功)
@property(nonatomic, strong) UIView *rectView;

//****** TLAnimatorCircular模式下有效 ******//
/// 圆形转场的center(默认,屏幕中心)
@property(nonatomic, assign) CGPoint center;
/// 默认初始半径(默认:0)
@property(nonatomic, assign) CGFloat startRadius;

+ (instancetype)animatorWithType:(TLAnimatorType)type;
@end

Expand Down
74 changes: 74 additions & 0 deletions TLTransitions/TransitionController/Animator/TLAnimator.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@

#import "TLAnimator.h"

typedef void(^TLAnimationCompletion)(BOOL flag);

@interface TLAnimator ()<CAAnimationDelegate>

/// CAAnimation 完成回调
@property(nonatomic, copy) TLAnimationCompletion animationCompletion;
@end


@implementation TLAnimator


#pragma mark - TLAnimatorProtocol
@synthesize transitionDuration;
@synthesize isPushOrPop;
Expand All @@ -25,6 +35,9 @@ - (CGFloat)percentOfFinishInteractiveTransition {
if (self.type == TLAnimatorTypeTiltLeft || self.type == TLAnimatorTypeTiltRight){
return 0.29f;
}
if (self.type == TLAnimatorCircular){
return 0.f;
}

return 0.5f;
}
Expand Down Expand Up @@ -91,6 +104,9 @@ - (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)tran
case TLAnimatorRectScale:
[self rectScaleTypeTransition:transitionContext presenting:isPresenting];
break;
case TLAnimatorCircular:
[self circularTypeTransition:transitionContext presenting:isPresenting];
break;
default:
break;
}
Expand Down Expand Up @@ -425,5 +441,63 @@ - (void)rectScaleTypeTransition:(id<UIViewControllerContextTransitioning>)transi
}];
}

#pragma mark - TLAnimatorTypeCircular
- (void)circularTypeTransition:(id<UIViewControllerContextTransitioning>)transitionContext presenting:(BOOL)isPresenting {

UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *traget = isPresenting ? toVC : fromVC;
UIView *containerView = transitionContext.containerView;
if(isPresenting) {
[containerView addSubview:toVC.view];
}
if (!isPresenting && self->isPushOrPop) {
[containerView insertSubview:toVC.view belowSubview:fromVC.view];
}

if (CGPointEqualToPoint(_center, CGPointZero)) {
_center = traget.view.center;
}

CGFloat W = traget.view.frame.size.width;
CGFloat H = traget.view.frame.size.height;

CGFloat x = MAX(_center.x, W - _center.x);
CGFloat y = MAX(_center.y, H - _center.y);
CGFloat endRadius = sqrt(x * x + y * y);

CGFloat radius = isPresenting ? self->_startRadius : endRadius;
CGFloat radius2 = !isPresenting ? self->_startRadius : endRadius;
CGRect rect = CGRectMake(_center.x - radius, _center.y - radius, radius * 2, radius * 2);
CGRect rect2 = CGRectMake(_center.x - radius2, _center.y - radius2, radius2 * 2, radius2 * 2);
CGPathRef path = CGPathCreateWithEllipseInRect(rect, nil);
CGPathRef path2 = CGPathCreateWithEllipseInRect(rect2, nil);

CABasicAnimation *anm = [CABasicAnimation animationWithKeyPath:@"path"];
anm.fromValue = (__bridge id _Nullable)(path);
anm.toValue = (__bridge id _Nullable)(path2);
anm.duration = [self transitionDuration:transitionContext];
anm.delegate = self;

CAShapeLayer *mask = [[CAShapeLayer alloc] init];
traget.view.layer.mask = mask;

mask.path = path2;
mask.fillRule = kCAFillRuleEvenOdd;
[mask addAnimation:anm forKey:@"path"];
[mask setNeedsDisplay];
__weak typeof(self) wself = self;
self.animationCompletion = ^(BOOL flag) {
BOOL wasCancelled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!wasCancelled];

wself.animationCompletion = nil;
};
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (self.animationCompletion) {
self.animationCompletion(flag);
}
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
- (TLDirection)directionForDragging;

@optional
/// 手势动画完成多少百分比后,释放手指可以完成转场,少于该值将取消转场。取值范围:[0 ,1),默认:0.5
/// 手势动画完成多少百分比后,释放手指可以完成转场,少于该值将取消转场。取值范围:[0 ,1),默认:0.5 ,小于等于0表示不支持百分比控制
- (CGFloat)percentOfFinishInteractiveTransition;
@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ - (TLDirection)directionForDragging; {
return _directionOfDismiss;
}

- (CGFloat)percentOfFinishInteractiveTransition {
return 0;
}

#pragma mark - creat instancetype
+ (instancetype)animatorWithTransitionType:(CATransitionType)tType
direction:(TLDirection)direction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ - (void)interactivePopRecognizerAction:(UIScreenEdgePanGestureRecognizer *)gestu
NSAssert(animator, @"animator = nil,异常");
if (animator == nil) return;

// TLCATransitonAnimator 不支持百分比控制
if (![animator isMemberOfClass:[TLCATransitonAnimator class]] ||
![animator isMemberOfClass:[TLCATransitonAnimator class]]) {
if ([animator respondsToSelector:@selector(percentOfFinishInteractiveTransition)] &&
[animator percentOfFinishInteractiveTransition] <= 0) {
// 不支持百分比控制
self.transitionDelegate.popGestureRecognizer = nil;
}else {
self.transitionDelegate.popGestureRecognizer = gestureRecognizer;
}

if (animator.isPushOrPop){
[self.navigationController popViewControllerAnimated:YES];
}else {
Expand Down
3 changes: 2 additions & 1 deletion TLTransitions/TransitionView/TLPopViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ @implementation TLPopViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
// self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self updatePreferredContentSizeWithTraitCollection:self.traitCollection];

}
Expand All @@ -40,6 +40,7 @@ - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)trait
- (void)setPopView:(UIView *)popView {
_popView = popView;
[self.view addSubview:popView];
self.view.backgroundColor = popView.backgroundColor;
}

- (void)viewDidLayoutSubviews {
Expand Down

0 comments on commit f53177f

Please sign in to comment.