如何像下面的示例一样进行自定义转换?

How do I do custom transition like in the below example?

这是例子。 example

我想在这里了解示例中的徽标如何将 VC1 移动到 VC2。 你能举个简单的例子吗? İt 可以是任何东西。

请不要向我推荐第三方。

我前段时间写了一篇博客 post 关于如何制作自定义过渡和动画,虽然这更像是 App Store 的指南,它可以用于任何类型的过渡动画。

https://medium.com/@eric.dockery283/custom-view-transitions-like-the-new-app-store-a2a1181229b6

要调查的事情是 UIViewControllerAnimatedTransitioning

总而言之,您将需要 2 个视图控制器,即登录视图控制器和动画的结束状态视图控制器。您将使用 UIViewControllerAnimatedTransitioning 来处理从一个视图控制器到另一个视图控制器的转换。我的例子是:

import UIKit
class TransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning {
  let durationExpanding = 0.75
  let durationClosing = 0.5
  var presenting = true
  var originFrame = CGRect.zero
  var dismissCompletion: (()->Void)?
  func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
   if presenting {
    return durationExpanding
   }
   return durationClosing
  }
  func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
   let containerView = transitionContext.containerView
   guard let toView = transitionContext.view(forKey: .to), let detailView = presenting ? toView: transitionContext.view(forKey: .from) else {
      return
   }
   let initialFrame = presenting ? originFrame : detailView.frame
   let finalFrame = presenting ? detailView.frame : originFrame
   let xScaleFactor = presenting ? initialFrame.width / finalFrame.width : finalFrame.width / initialFrame.width
   let yScaleFactor = presenting ? initialFrame.height / finalFrame.height : finalFrame.height / initialFrame.height
   let scaleTransform = CGAffineTransform(scaleX: xScaleFactor,
y: yScaleFactor)
   if presenting {
     detailView.transform = scaleTransform
     detailView.center = CGPoint( x: initialFrame.midX, y: initialFrame.midY)
     detailView.clipsToBounds = true
   }
   containerView.addSubview(toView)
   containerView.bringSubview(toFront: detailView)
   if presenting {
    //update opening animation
    UIView.animate(withDuration: durationExpanding, delay:0.0,
usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, //gives it some bounce to make the transition look neater than if you have defaults
    animations: {
        detailView.transform = CGAffineTransform.identity
        detailView.center = CGPoint(x: finalFrame.midX, y: finalFrame.midY)
},
    completion:{_ in
        transitionContext.completeTransition(true)
   }
   )
} else {
//update closing animation
     UIView.animate(withDuration: durationClosing, delay:0.0, options: .curveLinear,
      animations: {
       detailView.transform = scaleTransform
       detailView.center = CGPoint(x: finalFrame.midX, y: finalFrame.midY)
},
  completion:{_ in
    if !self.presenting {
      self.dismissCompletion?()
     }
     transitionContext.completeTransition(true)
   }
  )
  }
 }
}

您需要在您的登录视图控制器中设置您的转换委托:

extension ViewController: UIViewControllerTransitioningDelegate {
   func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
     guard let originFrame = selectedCell.superview?.convert(selectedCell.frame, to: nil) else {
      return transition
     }
    transition.originFrame = originFrame
    transition.presenting = true
    selectedCell.isHidden = true
    return transition
  }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
       transition.presenting = false
       return transition
    }
}

这应该允许您按照需要的方式自定义 views/animations。

另一个有用的 link:https://www.raywenderlich.com/146692/ios-animation-tutorial-custom-view-controller-presentation-transitions-2