如何使tabbar自定义动画仅针对某个视图控制器进行?

时间:2016-02-13 00:57:53

标签: ios swift

我有一个自定义动画,用于选择选项卡时,但我希望仅在单击某个选项卡时才会出现该动画。我猜它与toView的实例化有关。错误http://puu.sh/n5VuA/c3e886957e.png

class TransitioningObject: NSObject, UIViewControllerAnimatedTransitioning {

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
// Get the "from"nd "to" views
let fromView : UIView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView : UIView = transitionContext.viewForKey(UITransitionContextToViewKey)!


transitionContext.containerView()!.addSubview(fromView)
transitionContext.containerView()!.addSubview(toView)

//The "to" view with start "off screen" and slide left pushing the "from" view "off screen"
toView.frame = CGRectMake(toView.frame.width, 0, toView.frame.width, toView.frame.height)
let fromNewFrame = CGRectMake(-1 * fromView.frame.width, 0, fromView.frame.width, fromView.frame.height)

UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
toView.frame = CGRectMake(0, 0, 320, 560)
fromView.frame = fromNewFrame
}) { (Bool) -> Void in

transitionContext.completeTransition(true)
        }

}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.35
    }
}

这是视图控制器代码

override func viewDidLoad() {
    super.viewDidLoad()

    let animatedTransitioningObject = TransitioningObject()
    animatedTransitioningObject.animateTransition(UIViewControllerContextTransitionin) //I get an error here
    // Do any additional setup after loading the view.
}

1 个答案:

答案 0 :(得分:0)

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
  UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  [[transitionContext containerView] addSubview:toViewController.view];
  toViewController.view.alpha = 0;

  [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
    fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
    toViewController.view.alpha = 1;
  } completion:^(BOOL finished) {
      fromViewController.view.transform = CGAffineTransformIdentity;
      [transitionContext completeTransition:![transitionContext transitionWasCancelled]];

}];

}

您的动画功能应该如何显示。

此外,如果您想要一个为您执行此操作的窗格,我建议您查看此窗格

https://github.com/Ramotion/animated-tab-bar Animated Tab bar pod