【问题标题】:How to animate the view with pre-existing animation?如何使用预先存在的动画为视图设置动画?
【发布时间】:2017-09-29 04:32:33
【问题描述】:

我想用 UIView 的预先存在的动画为视图制作动画。 我正在使用此代码为 UIView 设置动画 -

CATransition *transition = [[CATransition alloc] init];
        transition.duration = 0.1;
        transition.type = kCATransitionPush;
        transition.subtype = kCATransitionFromLeft;
        [viewToAnimate.layer addAnimation:transition forKey:kCATransition];

我想为其他视图设置动画,以及这个动画。

【问题讨论】:

  • 您可以使用 CAAnimationGroup 链接多个动画,您尝试过吗?

标签: ios uiviewanimation


【解决方案1】:

您需要做的是链接您的动画。这是来自Apple 的示例。

let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.fromValue = 1
fadeOut.toValue = 0
fadeOut.duration = 1

let expandScale = CABasicAnimation()
expandScale.keyPath = "transform"
expandScale.valueFunction = CAValueFunction(name: kCAValueFunctionScale)
expandScale.fromValue = [1, 1, 1]
expandScale.toValue = [3, 3, 3]

let fadeAndScale = CAAnimationGroup()
fadeAndScale.animations = [fadeOut, expandScale]
fadeAndScale.duration = 1

【讨论】:

    【解决方案2】:

    您可以使用以下基本 iOS 动画: UIView .animate(withDuration: TimeInterval) { 代码
    }

    在代码段中,您可以编写更改不同视图的 x 位置。每当您单击按钮时,相应视图的 x 坐标将动画为 0。

    【讨论】:

      【解决方案3】:
      Try this
      
      func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
      
          let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
          let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
          let finalFrameForVC = transitionContext.finalFrameForViewController(toViewController)
          let containerView = transitionContext.containerView()
          let bounds = UIScreen.mainScreen().bounds
          toViewController.view.frame = CGRectOffset(finalFrameForVC, 0, bounds.size.height)
          containerView.addSubview(toViewController.view)
      
          UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .CurveLinear, animations: {
              fromViewController.view.alpha = 0.5
              toViewController.view.frame = finalFrameForVC
              }, completion: {
                  finished in
                  transitionContext.completeTransition(true)
                  fromViewController.view.alpha = 1.0
          })
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多