iOS抽屉效果和侧边菜单

版权所有,禁止匿名转载;禁止商业使用。

1、效果演示

源码下载地址

1. 抽屉效果演示


iOS开发,侧边菜单

1. 侧边菜单演示


iOS开发,侧边菜单

2、使用说明


构造方法 initialization


/// 构造方法(左控制器 & 右控制器 & 背景图片)
-(instancetype)initWithLeftController:(UIViewController *)leftController
  andMainController:(UIViewController *)mainController
                   andRightController:(UIViewController *)rightController
                   andBackgroundImage:(UIImage *)image;
/// 构造方法(左控制器 & 右控制器)
-(instancetype)initWithLeftController:(UIViewController *)leftController
  andMainController:(UIViewController *)mainController
                   andRightController:(UIViewController *)rightController;
/// 构造方法(左控制器 & 右控制器)
-(instancetype)initWithLeftController:(UIViewController *)leftController andMainView:(UIViewController *)mainController;
/// 构造方法(右控制器)
-(instancetype)initWithRightView:(UIViewController *)rightController andMainView:(UIViewController *)mainController;


视图控制方法 View control method


/// 恢复位置
-(void)showMainView;
/// 显示左视图
-(void)showLeftView;
/// 显示右视图
-(void)showRighView;
属性 attribute
/// 主视图隐藏后显示比例(0~1)
@property (nonatomic, assign) CGFloat otherScale;
/// 主视图比例 (0~1)
@property (nonatomic, assign) CGFloat mainScale;
/// 滑动速度系数-建议在0.5-1之间。默认为0.5
@property (assign,nonatomic) CGFloat speedf;
/// 是否允许点击视图恢复视图位置。默认为yes
@property (strong) UITapGestureRecognizer *sideslipTapGes;

3、使用方法


AppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // 1. 创建window
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  // 2. 创建控制器
  MainController *main = [[MainController alloc] init];
  LeftController *left = [[LeftController alloc] init];
  RightController *right = [[RightController alloc] init];
  // 3. 创建跟控制器
  JRMenuController *controller = [[JRMenuController alloc] initWithLeftController:left andMainController:main andRightController:right];
  controller.mainScale = 0.8;
  controller.otherScale = 0.6;
  controller.speedf = 0.6;
  // 4. 设置跟控制器
  self.window.rootViewController = controller;
  // 5. 显示 window
  [self.window makeKeyAndVisible];
  return YES;
}

4、 知识点总结


1. 滑动手势方向判断


// 滑动方向
typedef NS_ENUM(NSInteger, CameraMoveDirection) {
  kCameraMoveDirectionNone,
  kCameraMoveDirectionUp,
  kCameraMoveDirectionDown,
  kCameraMoveDirectionRight,
  kCameraMoveDirectionLeft,
};
 //创建滑动手势
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
#pragma mark - 判断滑动方向
- ( void )handleSwipe:( UIPanGestureRecognizer *)gesture {
  CGPoint translation = [gesture translationInView: self.view];
  if (gesture.state == UIGestureRecognizerStateBegan )
  {
    self.direction = kCameraMoveDirectionNone;
  }
  else if (gesture.state == UIGestureRecognizerStateChanged && self.direction == kCameraMoveDirectionNone)
  {
    // 获取 方向
    self.direction = [ self determineCameraDirectionIfNeeded:translation];
  }
  if (self.direction == kCameraMoveDirectionRight && self.leftControl != nil) {
    [self handlePan:gesture];
  }
  if (self.direction == kCameraMoveDirectionLeft && self.righControl != nil) {
    [self handlePan:gesture];
  }
}
// 获取方向
- ( CameraMoveDirection )determineCameraDirectionIfNeeded:( CGPoint )translation
{
  if (self.direction != kCameraMoveDirectionNone)
    return self.direction;
  if (fabs(translation.x) > gestureMinimumTranslation)
  {
    BOOL gestureHorizontal = NO;
    if (translation.y == 0.0 )
      gestureHorizontal = YES;
    else
      gestureHorizontal = (fabs(translation.x / translation.y) > 5.0 );
    if (gestureHorizontal)
    {
      if (translation.x > 0.0 )
        return kCameraMoveDirectionRight;
      else
        return kCameraMoveDirectionLeft;
    }
  }
  else if (fabs(translation.y) > gestureMinimumTranslation)
  {
    BOOL gestureVertical = NO;
    if (translation.x == 0.0 )
      gestureVertical = YES;
    else
      gestureVertical = (fabs(translation.y / translation.x) > 5.0 );
    if (gestureVertical)
    {
      if (translation.y > 0.0 )
        return kCameraMoveDirectionDown;
      else
        return kCameraMoveDirectionUp;
    }
  }
  return self.direction;
}


2. 滑动手势和tableview滑动共存


#pragma mark - UIGestureRecognizerDelegate
// 设置手势 滑动 和 tableView 滚动并存
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([otherGestureRecognizer.view isKindOfClass:[UITableView class]]) {
        return YES;
    }
    return NO;

}

3. tableviewcell 分割线设置贯穿整个tableView(补充)


// cell 行分割线 设置
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
    [cell setSeparatorInset: UIEdgeInsetsZero];
  }
  if ([cell respondsToSelector:@selector(setLayoutManager:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
  }
}
- (void)viewDidLayoutSubviews {
  if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
     [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
  }
  if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
  }
}


0 0