TabBar滑动效果

效果:
<img src="https://img-blog.csdn.net/20151112124907132?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

.h文件

#import <UIKit/UIKit.h>
#import "NowTableViewController.h"
#import "HappyOTableViewController.h"
#import "MarketTableViewController.h"

//继承自 UITabBarController(注意!)
@interface ShoppingViewController : UITabBarController  <UIApplicationDelegate>
{
    NowTableViewController *listViewControllerNow;
    HappyOTableViewController *listViewControllerHappy;
    MarketTableViewController *listViewControllerMarket;
    UITableViewController *currentViewController;

    @private UIImageView *_selectView;
}
@end




.m文件

//添加导航控制器到视图上
-(void)loadViewControllers
{
    listViewControllerNow = [[NowTableViewController alloc] initWithStyle:UITableViewStylePlain];
    
    listViewControllerHappy = [[HappyOTableViewController alloc] init];
    listViewControllerMarket = [[MarketTableViewController alloc] init];
    
    NSArray *viewControllers = [NSArray arrayWithObjects:listViewControllerNow,listViewControllerHappy, listViewControllerMarket, nil];
    
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    
    [tabBarController setViewControllers:viewControllers];

    [self setViewControllers:viewControllers animated:YES];
    
}
/**
 *  加载tabBar
 */
-(void)loadCustomTabBar
{
    
    /**1、
     初始化自定义TabBar背景
     */
    UIImageView *tabBarBG=[[UIImageView alloc]initWithFrame:CGRectMake(0,SCREENHEIGHT/3-SCREENHEIGHT/3, SCREENWIDTH,SCREENHEIGHT/15)];
    tabBarBG.userInteractionEnabled=YES;
    tabBarBG.backgroundColor=[UIColor blackColor];
    [self.view addSubview:tabBarBG];
    
    /**3、
     初始化自定义按钮选中背景
     */
    
    
    _selectView=[[UIImageView alloc]initWithFrame:CGRectMake(2, 4, SCREENWIDTH/3,SCREENHEIGHT/15-8)];
    _selectView.image=[UIImage imageNamed:@"滑动图片"];
    [[_selectView layer] setCornerRadius:20.0];
    [tabBarBG addSubview:_selectView];
    
    /**2、
     *  初始化自定义TabBarItem—>UIButton
     */
    float coordinateX=0;
    for (int index=0; index<3;index++)
    {

        //初始化按钮位置
        UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.tag=index;
        button.frame=CGRectMake(0+coordinateX, 4, SCREENWIDTH/3, SCREENHEIGHT/15-8);
//        [button.layer setCornerRadius:8.0];
        
        //为按钮填加名字
        NSString *Index=[NSString stringWithFormat:@"%d",index];   //当前按钮的索引
        NSDictionary *buttonName=[NSDictionary dictionaryWithObjectsAndKeys:@"正在进行",@"0",@"乐享聚点",@"1",@"购物商城",@"2", nil];
        [button setTitle:[buttonName objectForKey:Index] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
        
        //为按钮填加背景图片
        NSString *imageName=[NSString stringWithFormat:@"%d",index+1];
        [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [tabBarBG addSubview:button];
        
        //为按钮填加转换视图控制器动作
        [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
        
        coordinateX+=SCREENWIDTH/3;
    }
}

/**
 *  根据按钮的点击事件跳转视图控制器
 *
 *  @param button 跳转的按钮
 */
-(void)changeViewController:(UIButton *)button
{
    //获取点击按钮的tag
    self.selectedIndex=button.tag;
    
    //为点击事件添加动画
    [UIView beginAnimations:nil context:NULL];
    
    _selectView.frame=CGRectMake(SCREENWIDTH/3*button.tag, 4, SCREENWIDTH/3,SCREENHEIGHT/15-8);
    [[_selectView layer] setCornerRadius:10.0];
    NSLog(@"跳转到%ld",(long)button.tag);
    [UIView commitAnimations];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值