【干货】立方体菜单栏

我们在iPhone或者iPad中,除了导航栏,标签栏之外,最常用的界面切换就是菜单栏了,今天就给大家分享点干货,很简单的代码。为了方便,我能把实现都放在VC里, 大家在写工程的时候可不要和我学,尽量减轻VC的负担,好了不费话了, 下面上代码块。

属性


@property (nonatomic, strong) UIButton *menuOne;
@property (nonatomic, strong) UIButton *menuTwo;
@property (nonatomic, strong) UIButton *menuThree;

初始化

-(void)config{
        
    //        menuOne的创建
    
    self.menuOne = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    
    [self.view addSubview:_menuOne];
    
    [self.menuOne setShowsTouchWhenHighlighted:YES];//高亮
    
    self.menuOne.backgroundColor = [UIColor cyanColor];
    
    [self.menuOne setTitle:@"菜单1" forState:UIControlStateNormal];
    
    [self.menuOne setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.menuOne setTag:100];
    
    
    
    
    //MenuTwo的创建
    
    
    self.menuTwo = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];


    [self.menuTwo setShowsTouchWhenHighlighted:YES];//高亮
    
    [self.menuTwo setBackgroundColor:[UIColor orangeColor]];
    [self.menuTwo setTitle:@"菜单2" forState:UIControlStateNormal];
    [self.menuTwo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.menuTwo setTag:101];
    
    //        [self.menuTwo addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_menuTwo];
    
    
    //MenuThree的创建
    
    
    self.menuThree = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

    [self.menuThree setShowsTouchWhenHighlighted:YES];//高亮
    
    [self.menuThree setBackgroundColor:[UIColor redColor]];
    [self.menuThree setTitle:@"菜单3" forState:UIControlStateNormal];
    [self.menuThree setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.menuThree setTag:102];
    
    //        [self.menuThree addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_menuThree];
    
    
    
    //向左滑动的手势
    
    UISwipeGestureRecognizer *sgrLeft = [[UISwipeGestureRecognizer alloc]init];
    [sgrLeft setDirection:UISwipeGestureRecognizerDirectionLeft];//设置方向
    
    [sgrLeft addTarget:self  action:@selector(selectMenuItemLeft)];
    [self.view addGestureRecognizer:sgrLeft];
    
   // 向右滑动的手势
    
    UISwipeGestureRecognizer *sgrRight = [[UISwipeGestureRecognizer alloc]init];
    [sgrRight setDirection:UISwipeGestureRecognizerDirectionRight];//设置方向
    
    [sgrRight addTarget:self  action:@selector(selectMenuItemRight)];
    [self.view addGestureRecognizer:sgrRight];
    

    
    
}

方法的实现

-(void)selectMenuItemLeft{
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.8;//动画时间
    animation.type = @"cube";
    animation.subtype = kCATransitionFromLeft;//设置方向
    
    UIButton *lastView = (UIButton *)[self.view.subviews lastObject];//找到最上层的view
    
    if (lastView.tag == 100) {
        [self.view bringSubviewToFront:self.menuThree];
        
    }else if (lastView.tag == 101){
        [self.view bringSubviewToFront:self.menuOne];
        
    }else if (lastView.tag == 102){
        
        [self.view bringSubviewToFront:self.menuTwo];
    }
    
    [self.view.layer addAnimation:animation forKey:@"animation"];
    
}

-(void)selectMenuItemRight{
    
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.8;//动画时间
    animation.type = @"cube";
    animation.subtype = kCATransitionFromRight;//设置方向
    
    UIButton *lastView = [self.view.subviews lastObject];//找到最上层的view
    
    if (lastView.tag == 100) {
        [self.view bringSubviewToFront:self.menuTwo];
        
    }else if (lastView.tag == 101){
        [self.view bringSubviewToFront:self.menuThree];
        
    }else if (lastView.tag == 102){
        
        [self.view bringSubviewToFront:self.menuOne];
    }
    
    [self.view.layer addAnimation:animation forKey:@"animation"];
    
    
}
最后把方法掉进VC里大家就可以看到效果了,好了大家赶紧去试试吧,第一次写,写的不好,希望大家多多指点, 谢谢大家了。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,081评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,269评论 4 61
  • 今儿个学习了新的知识,同理心,这是对情商也是销售技巧的提升。 里头几条评论对我感触很深,其中一条说到,如果你和一个...
    梦游世界阅读 210评论 0 0
  • 一、UIStepper 步进器 步进器就是一个左右加减的控件。样式如下图: 二、UIStepper的属性和方法介绍...
    NetWork小贱阅读 2,831评论 0 0
  • 原创干货分享·精致工匠精神 谈到设计,会涉及很多内容,从创意、色彩、配图、文字等等内容,即使是大师也可以和你很久很...
    鬼灯森林阅读 1,350评论 9 68