AnimationSwitchRootViewController
UIWindow 的扩展 - 在 UIWindow 中动画切换根视图控制器。支持 Objective-C 和 Swift 5。如果需要纯 Objective-C,请使用 AnimationSwitchRootViewController/ObjC。
功能
- 使用动画在 UIWindow 中更改根视图控制器。
- 从 UIApplication 静态函数 sharedWindow() 获取主 UIWindow。
- 支持带有背景颜色、UIVIew 或 UIImage 的 CATransaction。
- 支持动画移动快照(手动)。
需求
- iOS 8.0+
- Xcode 10.2
- Swift 5.0
通讯
- 如果您需要帮助,请访问 provir.ru
- 如果您找到了错误,请打开一个问题。
- 如果您有功能请求,请打开一个问题。
- 如果您想贡献,请提交一个拉取请求。
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它:
$ gem install cocoapods
构建 AnimationSwitchRootViewController 1.0.0+ 需要 CocoaPods 1.6.0+。
要使用 CocoaPods 将 AnimationSwitchRootViewController 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target '<Your Target Name>' do
pod 'AnimationSwitchRootViewController', '~> 1.0'
end
然后,运行以下命令:
$ pod install
如果您需要纯 Objective-C,使用 AnimationSwitchRootViewController/ObjC
pod 'AnimationSwitchRootViewController/ObjC', '~> 1.0'
Carthage
Carthage 是一个去中心化的依赖管理器,它会构建您的依赖项并提供二进制框架。
您可以使用以下命令通过 Homebrew 安装 Carthage:
$ brew update
$ brew install carthage
要使用 Carthage 将 AnimationSwitchRootViewController 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它:
github "ProVir/AnimationSwitchRootViewController" ~> 1.0
运行 carthage update
命令来构建框架,并将构建好的 AnimationSwitchRootViewController.framework
拖入您的 Xcode 项目中。
如果您需要纯 Objective-C,使用 AnimationSwitchRootViewControllerObjC - 将构建好的 AnimationSwitchRootViewControllerObjC.framework
拖入您的 Xcode 项目。
手动安装
如果您不希望使用上述任何依赖管理器,您可以将 AnimationSwitchRootViewController 手动集成到您的项目中。
将项目中的 AnimationSwitchRootViewController
目录下的文件复制到项目中。如果您需要纯 Objective-C,请不要复制 *.swift
文件。
使用
Swift中接口(也支持Objective-C)
public extension UIApplication {
@objc public static func sharedWindow() -> UIWindow?
}
public extension UIWindow {
///Transition Direction - equal CATransitionSubType.
@objc public enum TransitionDirection : Int {
case fromRight
case fromLeft
case fromTop
case fromBottom
}
/**
Core Animation: custom transition and temp background window.
- Parameters:
- rootViewController: ViewController to switch.
- transition: Custom core animation transition.
- backgroundColor: Background color when animation in process. Default is black.
- backgroundView: View for background when animation in process.
*/
@objc public func switchRootViewController(to rootViewController: UIViewController,
withTransition transition: CATransition,
backgroundColor: UIColor? = nil,
backgroundView: UIView? = nil)
/**
Core Animation: push transition and temp background window.
- Parameters:
- rootViewController: ViewController to switch.
- direction: Direction animation. Equal CATransitionSubType in CATransition.
- duration: Duration animation. Default = 0.3.
- backgroundColor: Background color when animation in process. Default is black.
- backgroundView: View for background when animation in process.
*/
@objc public func switchRootViewControllerPushTransition(to rootViewController: UIViewController,
direction: TransitionDirection,
duration: CGFloat = 0.3,
backgroundColor: UIColor? = nil,
backgroundView: UIView? = nil)
/**
Core Animation: push transition and temp background window with image as background.
- Parameters:
- rootViewController: ViewController to switch.
- direction: Direction animation. Equal CATransitionSubType in CATransition.
- duration: Duration animation. Default = 0.3.
- backgroundColor: Background color when animation in process. Default is black.
- backgroundImage: Image for background when animation in process.
*/
@objc public func switchRootViewControllerPushTransition(to rootViewController: UIViewController,
direction: TransitionDirection,
duration: CGFloat = 0.3,
backgroundColor: UIColor? = nil,
backgroundImage: UIImage)
/**
Manually animation: move snapshots views (old and new).
- Parameters:
- rootViewController: ViewController to switch.
- direction: Direction animation. Equal CATransitionSubType in CATransition.
- inAnimation: Animation position for new ViewController.
- outAnimation: Animation position for old ViewController.
- duration: Duration animation. Default = 0.4.
- options: UIViewAnimationOptions for UIView animations.
*/
@objc public func switchRootViewControllerManuallyMove(to rootViewController: UIViewController,
direction: TransitionDirection,
inAnimation: Bool = true,
outAnimation: Bool = true,
duration: CGFloat = 0.4,
options: UIViewAnimationOptions = [])
}
AnimationSwitchRootViewControllerObjC中支持)
@interface UIApplication (PVRootWindow)
+ (nullable UIWindow*) sharedWindow;
@end
@interface UIWindow (PVAnimationSwitchRootViewController)
//Core Animation: transition and temp background window
- (void) setRootViewController:(UIViewController *)rootViewController
withTransition:(CATransition*)transition
backgroundColor:(nullable UIColor*)color
andBackgroundView:(nullable UIView*)view;
//Manually animation: move snapshots
- (void) setRootViewController:(UIViewController *)rootViewController
withManuallyInAnimation:(BOOL)inAnimation
outAnimation:(BOOL)outAnimation
routeType:(NSString*)transitionSubType
duration:(CGFloat)duration
options:(UIViewAnimationOptions)options;
//Simplify function
- (void) setRootViewController:(UIViewController *)rootViewController
pushTransactionRoute:(NSString*)transitionSubType
duration:(CGFloat)duration
backgroundColor:(nullable UIColor*)color
andBackgroundView:(nullable UIView*)view;
- (void) setRootViewController:(UIViewController *)rootViewController
pushTransactionRoute:(NSString*)transitionSubType
duration:(CGFloat)duration
backgroundColor:(nullable UIColor*)color
andBackgroundImage:(nullable UIImage*)image;
- (void) setRootViewController:(UIViewController *)rootViewController
pushManuallyRoute:(NSString*)transitionSubType
duration:(CGFloat)duration;
@end
UIApplication.sharedWindow()?.switchRootViewControllerPushTransition(to: newViewController,
direction: .fromRight,
backgroundColor: UIColor.white)
UIApplication.sharedWindow()?.switchRootViewControllerManuallyMove(to: newViewController,
direction: .fromLeft)
UIApplication.sharedWindow()?.switchRootViewControllerPushTransition(to: newViewController,
direction: .fromRight,
backgroundColor: UIColor.white)
UIApplication.sharedWindow()?.switchRootViewControllerManuallyMove(to: newViewController,
direction: .fromLeft)
更多示例可以在本仓库的项目中找到。
作者
许可证
AnimationSwitchRootViewController以MIT许可证发布。请参阅LICENSE获取详细信息。