更新:
1.修正了一些笔误和添加了shouldLeftStill,shouldRightStill两个属性,这两个属性可以使侧边栏不移动,而你可以通过RootSplitBuffDelegate协议来控制侧边栏的子视图的移动过程,以实现更丰富的交互**
Method Swizzling 提示(very important):
为确保UINavigationController的pop手势和侧滑手势不冲突,
在使用UINavigationController的pop手势时,对于navigationController.viewControllers的第二个(index为1)的viewController,若重写了viewDidApear: 则必须调用[super viewDidAppear:];
对UIViewController的
“presentViewController:animated:completion:”方法进行了Method-Swizzling,以确保模态视图显示的正确性。如果该swizzling不足以满足需求,用户可根据自行需要进行标准的method-swizzling.
RootSplitBuff依赖FrameBuff(用于快速获取UIView和CALayer的位置和尺寸)
</br>
此侧边栏工具是由BuffKit组件中的RootSplitBuff实现.
Demo地址:https://github.com/FlashHand/BuffKit
或通过pod导入:pod 'BuffKit'
RootSplitBuff 功能:
- 支持AutoLayout,横竖屏兼容。
- 支持通过左右边缘滑动手势显示或隐藏侧边栏
- 支持设置横屏和竖屏显示不同的背景图片
- 支持透视3D效果,缩放效果,平移效果,以及自定义
- 对左右侧滑手势进行了优先处理
- 支持协议回调
- 通过修改属性的默认值即可以实现十分丰富的侧边栏效果
效果图:
配置参数:
在 RootSplitBuff中默认值设置为:
_splitStyle=BuffSplitStyleCovered;
_dimColor=[UIColor lightGrayColor];
_dimOpacity=0.5;
_leftWidth=200;
_rightWidth=200;
_leftAnimationDuration=0.3;
_rightAnimationDuration=0.3;
_mainEndOffsetForLeft=0;
_mainEndOffsetForRight=0;
_mainScale=0.8;
_mainRotateAngle=1;
上面这些属性都是可以重写的
注意:_mainEndOffsetForLeft,_mainEndOffsetForRight只会在BuffSplitStyleCovered情况下有效,其他情况下会被忽略。
_mainScale只能用于BuffSplitStyleScaled情况。
_mainRotateAngle只能用于BuffSplitStylePerspective情况,BuffSplitStylePerspective情况下会忽略_leftWidth和_rightWidth
使用示例:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect screenBounds=[[UIScreen mainScreen]bounds];
_window=[[UIWindow alloc]initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
BuffListViewController *buffListVC=[[BuffListViewController alloc]init];
[buffListVC setTitle:@"BuffKit"];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:buffListVC];
//设置横竖屏背景图片
[[RootSplitBuff rootViewController]setRootBackgroundPortraitImage:[UIImage imageNamed:@"WallPaper.jpg"]];
[[RootSplitBuff rootViewController]setRootBackgroundLandscapeImage:[UIImage imageNamed:@"WallPaper2.jpg"]];
//设置主视图
[[RootSplitBuff rootViewController]setBfMainViewController:navi];
BuffLeftViewController *lvc=[[BuffLeftViewController alloc]init];
[[RootSplitBuff rootViewController]setBfLeftViewController:lvc];
//激活左边栏滑动手势
[RootSplitBuff activeLeftPanGesture];
BuffRightViewController *rvc=[[BuffRightViewController alloc]init];
[[RootSplitBuff rootViewController]setBfRightViewController:rvc];
//激活右边栏滑动手势
[RootSplitBuff activeRightPanGesture];
[_window setRootViewController:[RootSplitBuff rootViewController]];
//设置样式为透视旋转
[[RootSplitBuff rootViewController]setSplitStyle:BuffSplitStylePerspective];
//设置旋转角度
[[RootSplitBuff rootViewController]setMainRotateAngle:1.2];
//shouldLeftStill,shouldRightStill两个属性,这两个属性可以使侧边栏不移动,而你可以通过RootSplitBuffDelegate协议来控制侧边栏的子视图的移动过程,以实现更丰富的交互
[RootSplitBuff rootViewController].shouldLeftStill=YES;
[RootSplitBuff rootViewController].shouldRightStill=YES;
[_window makeKeyAndVisible];
return YES;
}
显示侧边栏:
[RootSplitBuff showLeftViewController];
[RootSplitBuff showRightViewController];
Method-swizzling注意:
我对UIViewContoller的"presentViewController:animated:completion:"方法的IMP进行了转换,请注意可能会导致VC的presentingViewController为[RootSplitBuff rootViewController]。所以获取presentingViewController前你需要确定你了解presentingViewController是什么。如果你也对该方法进行IMP转换,请确保你使用的是标准method-swizzling方法,method-swizzling实现如下:
@implementation UIViewController (RootSplitBuff)
#pragma mark UIViewController Method swizzling+forwarding
+ (void)load {
static dispatch_once_t rootSplitBuffToken;
dispatch_once(&rootSplitBuffToken, ^{
Class class = [UIViewController class];
SEL origSel = @selector(presentViewController:animated:completion:);
SEL swizSel = @selector(bf_swizzled_presentViewController:animated:completion:);
Method origMethod = class_getInstanceMethod(class, origSel);
Method swizMethod = class_getInstanceMethod(class, swizSel);
BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
if (didAddMethod) {
class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
}
else {
method_exchangeImplementations(origMethod, swizMethod);
}
});
}
-(void)bf_swizzled_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{
if (self==[RootSplitBuff rootViewController]) {
[self bf_swizzled_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
else {
if ([RootSplitBuff rootViewController].isRootViewContollerShowing){
[[RootSplitBuff rootViewController] bf_swizzled_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
else {
[self bf_swizzled_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
}
}
@end
BuffKit包含了对Foundation的扩展以及app常用的基础功能,目前仍在开发中,主干功能不多,但已经稳定可用,其它功能可以前往https://github.com/FlashHand/BuffKit 或 http://r4l.xyz查看。
如果您有任何问题或建议,欢迎通过简书留言: )