效果图如下:
仁伯安.gif
前言
左滑重点不在动画效果的实现,而在于左边视图事件响应后,怎么push进入主视图的NavigationController,如果主视图是TabBar的话,要获取当前TabBar的selecteItem,并push进入对应的导航容器中,正文以最为典型的TabBar作为Main主视图。
实现步骤:
1、首先实现工具类EVNSideSlideViewController,支持左滑和右滑,左视图-主视图-右视图,记得用的时候要将EVNSideSlideViewController做为你的根视图;
2、左右视图的设计及主视图控制器的设计,按照原来做;
3、使用单例类记录主视图TabBar的选择状态,以便于push到当前页面所在的NavigationController中;
4、左右视图单击事件处理,详情见,正文的第三部分。
一、工具类EVNSideSlideViewController实现
下面的BaseViewController可以改成UIViewController
EVNSideSlideViewController.h
//
// EVNSideSlideViewController.h
// MMB-Saler
//
// Created by developer on 16/6/28.
// Copyright © 2016年 仁伯安. All rights reserved.
//
#import "BaseViewController.h"
@interface EVNSideSlideViewController : BaseViewController
{
@private
UIImageView * imgBackground;
CGFloat scalef;
}
@property (strong, nonatomic) UIViewController *leftViewController;
@property (strong, nonatomic) UIViewController *mainViewController;
@property (strong, nonatomic) UIViewController *righViewController;
/**
* 滑动速度系数,建议在[0.5, 1]之间,默认为0.5
*/
@property (assign, nonatomic) CGFloat sideSlideSpeed;
/**
* 初始化,带有左右滑功能的视图
*
* @param LeftViewController 左边设置的视图
* @param MainViewController 主视图,可以是TabBarController
* @param RighViewController 右滑显示的视图
* @param image 当前视图背景图
*
* @return EVNSideSlideViewController object
*/
- (instancetype)initWithLeftViewController:(UIViewController *)LeftViewController andMainViewController:(UIViewController *)MainViewController andRightViewController:(UIViewController *)RighViewController andBackgroundImage:(UIImage *)image;
/**
* 显示主视图控制器
*/
- (void)showMainViewController;
/**
* 显示左视图控制器
*/
- (void)showLeftViewController;
/**
* 显示右视图控制器
*/
- (void)showRighViewController;
@end
EVNSideSlideViewController.m
//
// EVNSideSlideViewController.m
// MMB-Saler
//
// Created by developer on 16/6/28.
// Copyright © 2016年 仁伯安. All rights reserved.
//
#import "EVNSideSlideViewController.h"
@interface EVNSideSlideViewController ()
@end
@implementation EVNSideSlideViewController
- (instancetype)initWithLeftViewController:(UIViewController *)LeftViewController andMainViewController:(UIViewController *)MainViewController andRightViewController:(UIViewController *)RighViewController andBackgroundImage:(UIImage *)image
{
self = [super init];
if(self)
{
_sideSlideSpeed = 0.5;
_leftViewController = LeftViewController;
_mainViewController = MainViewController;
_righViewController = RighViewController;
UIImageView * imgview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[imgview setImage:image];
[self.view addSubview:imgview];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
/**
* 添加滑动手势
*/
UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[_mainViewController.view addGestureRecognizer:panGes];
_leftViewController.view.hidden = YES;
_righViewController.view.hidden = YES;
[self.view addSubview:_leftViewController.view];
[self.view addSubview:_righViewController.view];
[self.view addSubview:_mainViewController.view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 滑动手势
*
* @param panGestureRecognizer 滑动手势
*/
- (void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer
{
CGPoint point = [panGestureRecognizer translationInView:self.view];
scalef = (point.x*_sideSlideSpeed + scalef);
/**
* 根据视图位置判断是左滑还是右边滑动
*/
if (panGestureRecognizer.view.frame.origin.x >= 0)
{
panGestureRecognizer.view.center = CGPointMake(panGestureRecognizer.view.center.x + point.x*_sideSlideSpeed,panGestureRecognizer.view.center.y);
panGestureRecognizer.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - scalef/1000, 1 - scalef/1000);
[panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];
_righViewController.view.hidden = YES;
_leftViewController.view.hidden = NO;
}
else
{
panGestureRecognizer.view.center = CGPointMake(panGestureRecognizer.view.center.x + point.x*_sideSlideSpeed,panGestureRecognizer.view.center.y);
panGestureRecognizer.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scalef/1000, 1 + scalef/1000);
[panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];
_righViewController.view.hidden = NO;
_leftViewController.view.hidden = YES;
}
/**
* 有效手势判断、并调整相应的视图
*/
if (panGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
if (scalef > 120*_sideSlideSpeed)
{
[self showLeftViewController];
}
else if (scalef < - 120*_sideSlideSpeed)
{
[self showRighViewController];
}
else
{
[self showMainViewController];
scalef = 0;
}
}
}
- (void)showMainViewController
{
[UIView beginAnimations:nil context:nil];
_mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
_mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2.f, [UIScreen mainScreen].bounds.size.height/2.f);
[UIView commitAnimations];
}
- (void)showLeftViewController
{
[UIView beginAnimations:nil context:nil];
_mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
_mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width * 3/2.f - 88.f, [UIScreen mainScreen].bounds.size.height/2.f);
[UIView commitAnimations];
}
- (void)showRighViewController
{
[UIView beginAnimations:nil context:nil];
_mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
_mainViewController.view.center = CGPointMake(-[UIScreen mainScreen].bounds.size.width/2.f + 88.f, [UIScreen mainScreen].bounds.size.height/2.f);
[UIView commitAnimations];
}
@end
二、CommonHelper 单例记录TabBar的状态
CommonHelper.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CommonHelper : NSObject<NSCopying, NSMutableCopying>
@property (strong, nonatomic) UIViewController *rootViewController;
+ (instancetype)shareInstance;
- (void)setObjValue;
@end
CommonHelper.m
#import "CommonHelper.h"
static id _instance;
@implementation CommonHelper
///**
// * 只要系统中引用了该类,程序运行,就会主动调用load(不用手动调用,而且只会加载1次)
// */
//+ (void)load
//{
// _instance = [[CommonHelper alloc] init];
//}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
+ (instancetype)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
return _instance;
}
// 设置当前window的根视图
- (void)setObjValue
{
[_instance setRootViewController:[[UIApplication sharedApplication] keyWindow].rootViewController];
}
@end
三、左、右视图点击事件触发写法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CommonHelper *common = [CommonHelper shareInstance];
[common setObjValue];
NSLog(@"%@", NSStringFromClass([common.rootViewController class]));
EVNSideSlideViewController *sideSlideViewController = (EVNSideSlideViewController *)common.rootViewController;
[sideSlideViewController showMainViewController];
// NSLog(@"%@", NSStringFromClass(sideSlideViewController.mainViewController));
BuyerTabbarCtrl *toolController = (BuyerTabbarCtrl *)sideSlideViewController.mainViewController;
NSInteger selectIndex = toolController.selectedIndex;
// NSLog(@"%@", NSStringFromClass(toolController.viewControllers[0]));
// 关于
AboutMaibeiViewController * aboutMaibeiViewController = [[AboutMaibeiViewController alloc] init];
aboutMaibeiViewController.hidesBottomBarWhenPushed = YES;
[toolController.viewControllers[selectIndex] pushViewController:aboutMaibeiViewController animated:YES];
}
网友评论