自定義push轉(zhuǎn)場(chǎng)動(dòng)畫(下篇寫pop的轉(zhuǎn)場(chǎng)動(dòng)畫,其實(shí)很類似)

好久沒更新自己的簡(jiǎn)書了,悲哀呀...
先來個(gè)效果圖吧


transformAimationGif.gif
  1. 先拽兩個(gè)控制器, 藍(lán)色關(guān)聯(lián)PushViewController, 淺黃色關(guān)聯(lián)PopViewController,如圖:
圖1.png
  1. 關(guān)聯(lián)屬性于Push
圖2.png
  1. 做過自定義轉(zhuǎn)場(chǎng)動(dòng)畫的同學(xué)們應(yīng)該都曉得必須遵守<UIViewControllerAnimatedTransitioning>協(xié)議,那么這里就創(chuàng)建一個(gè)類(CustomPushTransition)專門來實(shí)現(xiàn)此轉(zhuǎn)場(chǎng)效果.在.m必須實(shí)現(xiàn)這兩個(gè)方法:
    - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
    
  2. 代碼如下, 大致思想:創(chuàng)建兩個(gè)圓(利用貝爾曲線),一個(gè)是與push按鈕的size, 另一個(gè)是有足夠覆蓋屏幕的半徑,最后的動(dòng)畫是由這兩個(gè)的path來完成.
    @property (nonatomic, strong) id<UIViewControllerContextTransitioning>customTransitionContext;
    
    - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
        return .5f;
      }
    
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
          self.
          PushViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
          PopViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
          //獲取push控制器中按鈕. 
          UIButton *button = fromVC.push;
          //獲取容器
          UIView *containerView = [transitionContext containerView];
          //添加視圖
          [containerView addSubView:fromVC.view];
          [containerView addSubView:toVC.view];
          
          //繪制小圓, Oval:橢圓形
          UIBezierPath *startBP = [UIBezierPath bezierPathWithOvalInRect:button.frame];
          //繪制大圓, 需先確定半徑,其實(shí)這里用到勾股定理,很好理解的,如圖3:
          CGFloat horizontalSide_X = button.center.x;
          CGFloat verticalSide_Y = CGRectGetMaxY(toVC.view.bounds) - button.center.y;
          CGFloat radius = sqrt( (horizontalSide_X * horizontalSide_X) + (verticalSide_Y * verticalSide_Y) );
          /*
         CGRect CGRectInset (
           CGRect rect,
           CGFloat dx,
           CGFloat dy
         ); 解釋: 該結(jié)構(gòu)體是以rect的中點(diǎn)為中心,插入一個(gè)以dx,dy為參考的放大或縮小的矩形; 正表示縮小,負(fù)表示放大. 
            所以繪制大圓是以小圓(button.frame)為中心點(diǎn),插入一個(gè)radius尺寸的放大的圓
          */
          UIBezierPath *finalBP = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(button.frame, -radius, -radius)];
          CAShapeLayer *maskLayer = [CAShapeLayer layer];
          maskLayer.path = finalBP.CGPath;
          
          //設(shè)置目標(biāo)控制器的mask
          toVC.view.layer.mask = maskLayer;
    
          //執(zhí)行動(dòng)畫
          CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
          animation.fromValue = (__bridge id _Nullable)(startBP.CGPath);
          animation.toValue = (__bridge id _Nullable)(finalBP.CGPath);
          animation.during = [self transitionDuration:transitionContext];
          animation.timingFuncion = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
          animation.delegate = self;
          [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
          }
    

    pragma mark - CABasicAnimation

    • (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
      //告訴系統(tǒng)轉(zhuǎn)場(chǎng)動(dòng)畫完成
      [self.customTransitionContext completeTransition:![self.customTransitionContext transitionWasCancelled]];
      //清除相應(yīng)控制器視圖的mask
      [self.customTransitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
      [self.customTransitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
      }


      圖3.png
5. push的自定義轉(zhuǎn)場(chǎng)基本完成了,現(xiàn)在回到PushViewController上, 控制器須遵守<UINavigationControllerDelegate>協(xié)議, 所以self.navigationCtroller.delegate = self; //注意設(shè)置導(dǎo)航控制器的代理須寫在viewWillAppear中或是viewDidAppear.實(shí)現(xiàn)導(dǎo)航控制器的代理:
#pragma mark - UINavigationControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{

        if (operation == UINavigationControllerOperationPush) {
        return self.pushAnimation;
    }
    return nil;
}

#pragma mark - 懶加載
- (CustomPushTransition *)pushAnimation{
     if (!_pushAnimation) {
     _pushAnimation = [[CustomPushTransition alloc] init];
   }
     return _pushAnimation;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容