Ios动画ios开发技术收集收藏ios

iOS UIViewControllerAnimatedTran

2017-01-03  本文已影响377人  不会抽烟

控制器A push to 控制器B 我们可以在A中拦截Push然后替换成自定义的转场并返回。

具体实现步骤:

1.自定义转场:继承NSObject,并遵循UIViewControllerAnimatedTransitioning协议。

2.m文件实现

- (NSTimeInterval)transitionDuration:(id)transitionContext {

    return0.5f;
}

方法,用来设置转场时间。

3.m文件实现

-(void)animateTransition:(id)transitionContext {
  //在这个方法里实现你的创意,也就是转场的核心
  self.transitionContext= transitionContext;

  UIViewController*toVC=   [transitionContextviewControllerForKey:UITransitionContextToViewControllerKey];

  UIViewController*fromVC = [transitionContextviewControllerForKey:UITransitionContextFromViewControllerKey];

  UIView*containerView = [transitionContextcontainerView];

  [containerViewaddSubview:fromVC.view];

  [containerViewaddSubview:toVC.view];

  UIBezierPath*startPath = [UIBezierPathbezierPathWithRect:CGRectMake(0,screenH*0.5-2,screenW,4)];

  UIBezierPath*finalPath = [UIBezierPathbezierPathWithRect:CGRectMake(0,0,screenW,screenH)];

  CAShapeLayer*maskLayer = [CAShapeLayerlayer];

  maskLayer.path= finalPath.CGPath;

  toVC.view.layer.mask= maskLayer;

  CABasicAnimation*maskLayerAnimation = [CABasicAnimationanimationWithKeyPath:@"path"];

  maskLayerAnimation.fromValue= (__bridgeid)(startPath.CGPath);

  maskLayerAnimation.toValue= (__bridgeid)((finalPath.CGPath));

  maskLayerAnimation.duration= [selftransitionDuration:transitionContext];

  maskLayerAnimation.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

  maskLayerAnimation.delegate=self;

  [maskLayeraddAnimation:maskLayerAnimationforKey:@"path"];
}

简单解释一下,转场有一个容器视图,我们需要把A,B控制器的view添加到containerView中,然后根据自己不同的创意来操作两个视图实现转场动画。

4.在转场完成后需要通知转场完成

[self.transitionContextcompleteTransition:![self.transitionContexttransitionWasCancelled]];

可以在动画结束代理方法中实现。

5.控制器A遵循UINavigationControllerDelegate协议并实现方法:

- (id)navigationController:(UINavigationController*)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC {
  if(operation ==UINavigationControllerOperationPush) {//判断是否是push,是就替换自定义的转场
  LiuqsAnimationTransition *push = [[LiuqsAnimationTransition alloc] init];

  return push;

  }else {

  return nil;
}
}```

pushu 已经ok,pop和push类似,具体实现看[demo](https://github.com/LMMIsGood/LiuqsAnimationTransition)
上一篇 下一篇

猜你喜欢

热点阅读