实现自定义动画以在iPad上显示指定视图的模态视图,可以使用以下方法:
class CustomModalViewController: UIViewController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
modalPresentationStyle = .custom
transitioningDelegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension CustomModalViewController: UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return CustomPresentationController(presentedViewController: presented, presenting: presenting)
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomPresentAnimationController()
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomDismissAnimationController()
}
}
class CustomPresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
let containerViewSize = containerView?.bounds.size ?? .zero
let presentedViewSize = presentedViewController.preferredContentSize
let originX = (containerViewSize.width - presentedViewSize.width) / 2
let originY = (containerViewSize.height - presentedViewSize.height) / 2
return CGRect(x: originX, y: originY, width: presentedViewSize.width, height: presentedViewSize.height)
}
}
class CustomPresentAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let toViewController = transitionContext.viewController(forKey: .to)!
let containerView = transitionContext.containerView
toViewController.view.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
containerView.addSubview(toViewController.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
toViewController.view.transform = .identity
}, completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
class CustomDismissAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewController(forKey: .from)!
let containerView = transitionContext.containerView
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
fromViewController.view.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
}, completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
let modalViewController = CustomModalViewController()
present(modalViewController, animated: true, completion: nil)
这样就可以实现在iPad上显示指定视图的模态视图,并使用自定义动画效果。
领取专属 10元无门槛券
手把手带您无忧上云