QQ侧滑效果图示

效果图如下:


前言

左滑重点不在动画效果的实现,而在于左边视图事件响应后,怎么push进入主视图的NavigationController,如果主视图是TabBar的话,要获取当前TabBar的selecteItem,并push进入对应的导航容器中,正文以最为典型的TabBar作为Main主视图。

实现步骤:

1、首先实现工具类EVNSideSlideViewController,支持左滑和右滑,左视图-主视图-右视图,记得用的时候要将EVNSideSlideViewController做为你的根视图;

2、左右视图的设计及主视图控制器的设计,按照原来做;

3、使用单例类记录主视图TabBar的选择状态,以便于push到当前页面所在的NavigationController中;

4、左右视图单击事件处理,详情见,正文的第三部分。

下面的BaseViewController可以改成UIViewController

EVNSideSlideViewController.h

////  EVNSideSlideViewController.h//  MMB-Saler////  Created by developer on 16/6/28.//  Copyright © 2016年 仁伯安. All rights reserved.//#import"BaseViewController.h"@interfaceEVNSideSlideViewController:BaseViewController{@privateUIImageView* imgBackground;CGFloatscalef;}@property(strong,nonatomic)UIViewController*leftViewController;@property(strong,nonatomic)UIViewController*mainViewController;@property(strong,nonatomic)UIViewController*righViewController;/**

*  滑动速度系数,建议在[0.5, 1]之间,默认为0.5

*/@property(assign,nonatomic)CGFloatsideSlideSpeed;/**

*  初始化,带有左右滑功能的视图

*

*  @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"@interfaceEVNSideSlideViewController()@end@implementationEVNSideSlideViewController- (instancetype)initWithLeftViewController:(UIViewController*)LeftViewController andMainViewController:(UIViewController*)MainViewController andRightViewController:(UIViewController*)RighViewController andBackgroundImage:(UIImage*)image{self= [superinit];if(self)    {        _sideSlideSpeed =0.5;        _leftViewController = LeftViewController;        _mainViewController = MainViewController;        _righViewController = RighViewController;UIImageView* imgview = [[UIImageViewalloc]initWithFrame:[UIScreenmainScreen].bounds];        [imgview setImage:image];        [self.view addSubview:imgview];    }returnself;}- (void)viewDidLoad{    [superviewDidLoad];/**

    * 添加滑动手势

    */UIPanGestureRecognizer*panGes = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@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 {    [superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}/**

*  滑动手势

*

*  @param panGestureRecognizer 滑动手势

*/- (void)handlePan:(UIPanGestureRecognizer*)panGestureRecognizer{CGPointpoint = [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)        {            [selfshowLeftViewController];        }elseif(scalef < -120*_sideSlideSpeed)        {            [selfshowRighViewController];        }else{            [selfshowMainViewController];            scalef =0;        }    }}- (void)showMainViewController{    [UIViewbeginAnimations:nilcontext:nil];    _mainViewController.view.transform =CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);    _mainViewController.view.center =CGPointMake([UIScreenmainScreen].bounds.size.width/2.f, [UIScreenmainScreen].bounds.size.height/2.f);    [UIViewcommitAnimations];}- (void)showLeftViewController{    [UIViewbeginAnimations:nilcontext:nil];    _mainViewController.view.transform =CGAffineTransformScale(CGAffineTransformIdentity,0.9,0.9);    _mainViewController.view.center =CGPointMake([UIScreenmainScreen].bounds.size.width *3/2.f -88.f, [UIScreenmainScreen].bounds.size.height/2.f);    [UIViewcommitAnimations];}- (void)showRighViewController{    [UIViewbeginAnimations:nilcontext:nil];    _mainViewController.view.transform =CGAffineTransformScale(CGAffineTransformIdentity,0.9,0.9);    _mainViewController.view.center =CGPointMake(-[UIScreenmainScreen].bounds.size.width/2.f +88.f, [UIScreenmainScreen].bounds.size.height/2.f);    [UIViewcommitAnimations];}@end

二、CommonHelper 单例记录TabBar的状态

CommonHelper.h

#import#import@interfaceCommonHelper:NSObject@property(strong,nonatomic)UIViewController*rootViewController; + (instancetype)shareInstance;- (void)setObjValue;@end

CommonHelper.m

#import"CommonHelper.h"staticid_instance;@implementationCommonHelper///**// *  只要系统中引用了该类,程序运行,就会主动调用load(不用手动调用,而且只会加载1次)// *///+ (void)load//{//    _instance = [[CommonHelper alloc] init];//}+ (instancetype)allocWithZone:(struct_NSZone *)zone{staticdispatch_once_tonceToken;dispatch_once(&onceToken, ^{        _instance = [superallocWithZone:zone];    });return_instance;} + (instancetype)shareInstance{staticdispatch_once_tonceToken;dispatch_once(&onceToken, ^{        _instance = [[selfalloc] init];    });return_instance;}- (id)copyWithZone:(NSZone*)zone{return_instance;}- (id)mutableCopyWithZone:(NSZone*)zone{return_instance;}// 设置当前window的根视图- (void)setObjValue{    [_instance setRootViewController:[[UIApplicationsharedApplication] keyWindow].rootViewController];}@end

三、左、右视图点击事件触发写法

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{    CommonHelper *common = [CommonHelper shareInstance];    [common setObjValue];NSLog(@"%@",NSStringFromClass([common.rootViewControllerclass]));    EVNSideSlideViewController *sideSlideViewController = (EVNSideSlideViewController *)common.rootViewController;    [sideSlideViewController showMainViewController];//    NSLog(@"%@", NSStringFromClass(sideSlideViewController.mainViewController));BuyerTabbarCtrl *toolController = (BuyerTabbarCtrl *)sideSlideViewController.mainViewController;NSIntegerselectIndex = toolController.selectedIndex;//    NSLog(@"%@", NSStringFromClass(toolController.viewControllers[0]));// 关于AboutMaibeiViewController * aboutMaibeiViewController = [[AboutMaibeiViewController alloc] init];    aboutMaibeiViewController.hidesBottomBarWhenPushed =YES;    [toolController.viewControllers[selectIndex] pushViewController:aboutMaibeiViewController animated:YES];}



@interface FirstViewController ()

{

    NSArray *arr;

}

@property(nonatomic,strong)UIScrollView *sc1;

@property(nonatomic,strong)UIScrollView *sc2;

@property(nonatomic,strong)UIView *move;



@end


@implementation FirstViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    arr=@[@"新闻",@"体育",@"经济",@"生活",@"女性"];


    self.view.backgroundColor = [UIColor whiteColor];


    self.sc1=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 45)];

    _sc1.contentSize=CGSizeMake(arr.count/5*self.view.frame.size.width, 30);

    _sc1.userInteractionEnabled=YES;

    _sc1.tag=2;

    _sc1.delegate = self;

    _sc1.pagingEnabled=YES;


    _sc1.bounces=NO;

    _sc1.showsVerticalScrollIndicator=NO;

    _sc1.showsHorizontalScrollIndicator=NO;

    [self.view addSubview:_sc1];


    for (int i=0; i

i++) {

        UIButton *but=[[UIButton alloc] initWithFrame:CGRectMake(i*self.view.frame.size.width/5, 0, self.view.frame.size.width/5, 44)];

        [butsetTitle:arr[i] forState:UIControlStateNormal];

        [butsetTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        if (i==1) {

            [butsetTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

        }

        but.tag= i + 100;

        [butaddTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

        [_sc1 addSubview:but];

        self.sc2=[[UIScrollView alloc] initWithFrame:CGRectMake(0,45+64, self.view.frame.size.width, self.view.frame.size.height-45-64-49)];

        _sc2.contentSize=CGSizeMake(self.view.frame.size.width*arr.count, 0);

            _sc2.userInteractionEnabled=YES;

            _sc2.pagingEnabled=YES;

            _sc2.tag=200;

            _sc2.bounces=NO;

            _sc2.delegate=self;

            _sc2.showsVerticalScrollIndicator=NO;

            _sc2.showsHorizontalScrollIndicator=NO;

        for (int i=0; i

i++) {

            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height-40-45-64)];

            view.backgroundColor=[UIColor clearColor];

            if (i==1) {

                UITableView *table=[[UITableView alloc] initWithFrame:self.view.frame];

                table.delegate=self;

                table.dataSource=self;

                table.rowHeight=80;

                [viewaddSubview:table];

            }


            [_sc2 addSubview:view];



        }

        [self.view addSubview:_sc2];



    }

    _move = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 5.0, 44 + 62, self.view.frame.size.width  /5.0, 2)];

    _move.backgroundColor =

[UIColor orangeColor];

    [self.view addSubview:_move];



}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 20;

}


-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

{


    static NSString*ID=@"cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];

    if (cell==nil) {

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    }

    cell.textLabel.text=@"我爱你";

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;


    return cell;

}

-(void)click:(UIButton *)but

{

    _move.frame= CGRectMake(self.view.frame.size.width/5.0*(but.tag - 100), 44+62, self.view.frame.size.width/5.0, 2);

    for (int i=0; i

i++) {

        UIButton *button=[self.view viewWithTag:i+100];

        if (but.tag==button.tag) {

            [buttonsetTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

            [_sc2 setContentOffset:CGPointMake(((but.tag-100)*self.view.frame.size.width), 0)];

        }

        else

        {

            [buttonsetTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        }


    }

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    if (scrollView.tag==200) {

        _move.frame=CGRectMake(scrollView.contentOffset.x/self.view.frame.size.width*(self.view.frame.size.width/5), 44+62, self.view.frame.size.width/5, 2);

        for (int i=0; i

i++) {

            UIButton * button = [self.view viewWithTag:100+i];

            if((button.tag-100) == (scrollView.contentOffset.x/self.view.frame.size.width)){

                [buttonsetTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];


            }else{

                [buttonsetTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

            }

        }

    }

}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,110评论 25 707
  • 一直念叨这冬天还不下雪,念叨着念叨着就下了起来。 看着窗外的雪花由小变大,由小颗粒变成一片片,从天空飘飘洒洒降落下...
    江致一阅读 401评论 2 3
  • 路是自己的,跟着自己心里走即使遇到再大的困难都要坚持下去,即使身边的人不支持你也要坚持下去,张艾婷过去的你就是太依...
    张艾婷阅读 155评论 0 0