iOS抽屜效果和側邊菜單,iOS抽屜效果菜單
iOS抽屜效果和側邊菜單源碼1、效果示範1. 抽屜效果示範
1. 側邊菜單示範
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)]; }}