1. 程式人生 > >iOS自定義轉場動畫(4)——自定義模態跳轉之dismiss與手勢驅動

iOS自定義轉場動畫(4)——自定義模態跳轉之dismiss與手勢驅動

Dismiss

效果:

1、新建PresentTransition繼承NSObject,並在.h中遵守UIViewControllerAnimatedTransitioning協議。

2、實現協議的兩個方法,並在其中編寫 Push 的動畫。類似Present,只需要修改少量程式碼:

// 返回動畫的時間
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return 0.8;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    ViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
SecondViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView * container = [transitionContext containerView]; [container addSubview:toVC.view]; [container bringSubviewToFront:fromVC.view]; // 改變m34 CATransform3D transfrom = CATransform3DIdentity;
transfrom.m34 = -0.002; container.layer.sublayerTransform = transfrom; // 設定archPoint和position CGRect initalFrame = [transitionContext initialFrameForViewController:fromVC]; toVC.view.frame = initalFrame; fromVC.view.frame = initalFrame; fromVC.view.layer.anchorPoint = CGPointMake(0
, 0.5); fromVC.view.layer.position = CGPointMake(0, initalFrame.size.height / 2.0); // 新增陰影效果 CAGradientLayer * shadowLayer = [[CAGradientLayer alloc] init]; shadowLayer.colors =@[ [UIColor colorWithWhite:0 alpha:1], [UIColor colorWithWhite:0 alpha:0.5], [UIColor colorWithWhite:1 alpha:0.5] ]; shadowLayer.startPoint = CGPointMake(0, 0.5); shadowLayer.endPoint = CGPointMake(1, 0.5); shadowLayer.frame = initalFrame; UIView * shadow = [[UIView alloc] initWithFrame:initalFrame]; shadow.backgroundColor = [UIColor clearColor]; [shadow.layer addSublayer:shadowLayer]; [fromVC.view addSubview:shadow]; shadow.alpha = 0; // 動畫 [UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:2 animations:^{ fromVC.view.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0); shadow.alpha = 1.0; } completion:^(BOOL finished) { fromVC.view.layer.anchorPoint = CGPointMake(0.5, 0.5); fromVC.view.layer.position = CGPointMake(CGRectGetMidX(initalFrame), CGRectGetMidY(initalFrame)); fromVC.view.layer.transform = CATransform3DIdentity; [shadow removeFromSuperview]; [transitionContext completeTransition: ![transitionContext transitionWasCancelled]]; // 如果引數寫成yes,當用戶取消pop時,會繼續執行動畫,也就是讓detailVC消失,設定成這個引數,會避免這樣的錯誤 }]; }

3、在ViewController.m中加入一個方法即可:

// dismiss動畫
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return [[DismissTransition alloc] init];
}

加入手勢驅動

1、想要同時實現 present 和 dismiss 手勢,就需要給兩個 viewController.view 新增手勢。首先在 ViewController 中給自己新增一個螢幕右邊的手勢,在init SecondViewController時給它的view新增一個螢幕左邊的手勢,讓它們使用同一個手勢監聽方法,都交給viewController處理,也就是self

在ViewController中封裝了一個增加手勢的方法,引數為承載手勢的view和手勢

// 新增手勢的方法
- (void)addScreenEdgePanGestureRecognizer:(UIView *)view edges:(UIRectEdge)edges{
    UIScreenEdgePanGestureRecognizer * edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)]; // viewController和SecondViewController的手勢都由self管理
    edgePan.edges = edges;
    [view addGestureRecognizer:edgePan];
}

2、在viewDidLoad()和用於present的按鈕的點選方法中分別加入手勢:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.transitioningDelegate = self;
    [self addScreenEdgePanGestureRecognizer:self.view edges:UIRectEdgeRight]; //self.view增加右側的手勢,用於push
}
- (void)presentClick{
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    secondVC.transitioningDelegate = self; // 必須second同樣設定delegate才有動畫
    [self addScreenEdgePanGestureRecognizer:secondVC.view edges:UIRectEdgeLeft];
    [self presentViewController:secondVC animated:YES completion:^{
    }];
}

3、實現手勢的監聽方法,首先定義一個屬性:

@property (nonatomic, retain) UIPercentDrivenInteractiveTransition * percentDrivenTransition;

4、因為有兩個手勢,要區別他們使用的是 KeyWindow。手勢監聽方法:

// 手勢的監聽方法
- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)edgePan{
    CGFloat progress = fabs([edgePan translationInView:[UIApplication sharedApplication].keyWindow].x / [UIApplication sharedApplication].keyWindow.bounds.size.width);// 有兩個手勢,所以這裡計算百分比使用的是 KeyWindow

    if(edgePan.state == UIGestureRecognizerStateBegan){
        self.percentDrivenTransition = [[UIPercentDrivenInteractiveTransition alloc] init];
        if(edgePan.edges == UIRectEdgeRight){
            // present,避免重複,直接呼叫點選方法
            [self presentClick];
        }else if(edgePan.edges == UIRectEdgeLeft){
            [self dismissViewControllerAnimated:YES completion:^{
            }];
        }
    }else if(edgePan.state == UIGestureRecognizerStateChanged){
        [self.percentDrivenTransition updateInteractiveTransition:progress];
    }else if(edgePan.state == UIGestureRecognizerStateCancelled || edgePan.state == UIGestureRecognizerStateEnded){
        if(progress > 0.5){
            [_percentDrivenTransition finishInteractiveTransition];
        }else{
            [_percentDrivenTransition cancelInteractiveTransition];
        }
        _percentDrivenTransition = nil;
    }
}

5、最後實現 UIViewControllerTransitioningDelegate 協議的另外兩個方法,分別返回 Present 和 Dismiss 動畫的百分比。

// 百分比present
- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator{
    return _percentDrivenTransition;
}
// 百分比dismiss
- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator{
    return _percentDrivenTransition;
}

現在,關於Modal的自定義跳轉動畫就完成了