CustomPresenter
关于
CustomPresenter 是 UIViewControllerAnimatedTransitioning
的一个实现,它是一套用于实现自定义视图控制器转换动画的方法(更多信息请参阅这里)。
该库为视图控制器的显示(CustomControllerPresentationAnimator
)和消失(CustomControllerDismissAnimator
)提供了实现。通过显示上下文可以自定义转换。
protocol CustomControllerPresentationContext {
// Transition properties
var backgroundViewForPresentation: UIView? { get set }
var backgroundAlpha: CGFloat { get }
var duration: TimeInterval { get }
var animationSpringDumping: CGFloat? { get }
var animationInitialSpringVelocity: CGFloat? { get }
// Optional object that controls presentation/dismissal animation
var animationDriver: CustomControllerPresentationAnimationDriver? { get }
// Final frame of presented view controller or initial frame of dismissed one;
// `transitionedView` is presented view during presentation, or dismissed one if we are undergoing dismissal
func controllerFrame(for containerView: UIView, transitionedView: transitionedView) -> CGRect
}
配置
要使用 CustomPresenter 与您的视图控制器,需要遵循以下3个步骤
- 将视图控制器的
modalPresentationStyle
设置为.custom
- 创建一个实现
CustomControllerPresentationContext
的实例,该实例将描述转换(不用担心,所有属性都有默认值,所以需要的努力很少)。 - 确保您的视图控制器成为
transitioningDelegate
并实现代理方法。
class MyViewController: UIViewController {
private class MyPresentationContext: CustomControllerPresentationContext {}
private var myPresentationContext = MyPresentationContext()
}
extension MyViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomControllerPresentationAnimator(presentationContext: myPresentationContext)
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomControllerDismissAnimator(presentationContext: myPresentationContext)
}
}
重要
请注意,如果您在包裹在 UINavigationController
中的视图控制器中进行显示,则必须确保导航控制器上将 modalPresentationStyle
设置为 .custom
,并且将 transitioningDelegate
设置为实现它(如上面示例中所示的)视图控制器。
要求
iOS 8, Swift 4
安装
Cocoapods
CustomPresenter通过CocoaPods提供。要安装它,只需将以下行添加到您的Podfile中
pod 'CustomPresenter'
Carthage
只需将其添加到您的Cartfile
中
github "azubala/CustomPresenter"
作者
Aleksander Zubala | zubala.com
许可证
CustomPresenter在MIT许可证下提供。更多详细信息请参见LICENSE文件。