【干货】立方体菜单栏

我们在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里大家就可以看到效果了,好了大家赶紧去试试吧,第一次写,写的不好,希望大家多多指点, 谢谢大家了。

你可能感兴趣的:(【干货】立方体菜单栏)