iOS Dev 山寨迅雷看看的自定义的UITabbar(会动的UITabbar)

按照迅雷看看的自定义的UITabbar的仿写

这里把Tabbar藏了起来,好像是藏在一个按键中,按键可以随处拖动,点击按键显示出Tabbar,就像是Assistive Touch一样,,首先我们要为这个可以拖动的UIButton改变一些东西,所以自定义一个UIButton让它拥有拖动功能,

//  MyUIButton.m
//  Build1016
//
//  Created by Wintelsui on 17/10/12.
//  Copyright (c) 2012年 Wintelsui. All rights reserved.
//

#import "MyUIButton.h"

@implementation MyUIButton
@synthesize MoveEnable;
@synthesize MoveEnabled;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    MoveEnabled = NO;
    [super touchesBegan:touches withEvent:event];
    if (!MoveEnable) {
        return;
    }
    NSLog(@"MoveEnabled-NO");
    UITouch *touch = [touches anyObject];
    beginpoint = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    MoveEnabled = YES;
    NSLog(@"MoveEnabled-YES");
    if (!MoveEnable) {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint nowPoint = [touch locationInView:self];    
    
    float offsetX = nowPoint.x - beginpoint.x;
    float offsetY = nowPoint.y - beginpoint.y;
    self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
    
    if (self.center.x > (self.superview.frame.size.width-self.frame.size.width/2)) {
        CGFloat x = self.superview.frame.size.width-self.frame.size.width/2;
        self.center = CGPointMake(x, self.center.y + offsetY);
    }else if (self.center.x < self.frame.size.width/2){
        CGFloat x = self.frame.size.width/2;
        self.center = CGPointMake(x, self.center.y + offsetY);
    }
    if (self.center.y > (self.superview.frame.size.height-self.frame.size.height/2-49)) {
        CGFloat x = self.center.x;
        CGFloat y = self.superview.frame.size.height-self.frame.size.height/2-49;
        self.center = CGPointMake(x, y);
    }else if (self.center.y < self.frame.size.height/2){
        CGFloat x = self.center.x;
        CGFloat y = self.frame.size.height/2;
        self.center = CGPointMake(x, y);
    }
}

之后我们开始自定义这个Tabbar,其中我用一个UIImageView的图片,表达了它加载出来时侯的效果,但是貌似有点问题,问题就是好像是没有动画一样………..

 

//
//  MyCustomTabBarController.m
//  Build1016
//
//  Created by Wintelsui on 16/10/12.
//  Copyright (c) 2012年 Wintelsui. All rights reserved.
//

#import "MyCustomTabBarController.h"

@interface MyCustomTabBarController ()

@end

@implementation MyCustomTabBarController
@synthesize btn1, btn2, btn3, btn4, btnh;
@synthesize tabBarView,showbuttonview;

- (void)setNoHighlightTabBar
{
    int tabCount = [self.viewControllers count] > 4 ? 4 : [self.viewControllers count];
    
    NSArray * tabBarSubviews = [self.tabBar subviews];
    for(int i = [tabBarSubviews count] - 1; i > [tabBarSubviews count] - tabCount - 1; i--)
    {
        for(UIView * v in [[tabBarSubviews objectAtIndex:i] subviews])
        {
            //是否需要有空位置(Tabbar ----1101)
            if(v && [NSStringFromClass([v class]) isEqualToString:@"Appindexctrl"])
            {//the v is the highlight view.
                NSLog(@"the v is the highlight view.");
                [v removeFromSuperview];
                break;
            }
        }
    }
}

- (void)makeTabBarHidden:(BOOL)hide
{
    if ([self.view.subviews count]<2) {
        return;
    }
    UIView *contentView;
    if([[self.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
    {
        contentView=[self.view.subviews objectAtIndex:1];
    }else {
        contentView=[self.view.subviews objectAtIndex:0];
    }
    if(hide)
    {
        contentView.frame=self.view.bounds;
        
    }else {//
        contentView.frame = CGRectMake(self.view.bounds.origin.x,
                                       self.view.bounds.origin.y   ,
                                       self.view.bounds.size.width,
                                       self.view.bounds.size.height - self.tabBar.frame.size.height);
    }
    self.tabBar.hidden=hide;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self addCustomElements];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
	
	[self hideTabBar];
	
}

- (void)hideTabBar
{
	for(UIView *view in self.view.subviews)
	{
		if([view isKindOfClass:[UITabBar class]])
		{
			view.hidden = YES;
			break;
		}
	}
}

- (void)hideNewTabBar
{
    self.tabBarView.hidden = 1;
}

- (void)showNewTabBar
{
    self.tabBarView.hidden = 0;
}

//做了修改 设置tab bar
- (void)addCustomElements
{
	int btn_x = 10;
	int btn_y = 3;
	int btn_with = 60;//60
	int btn_height = 42;
	
	int label_y = 30;
	int label_height = 12;
	
	int bview_x = 0;
	int bview_y = 0;
	int bview_width= self.view.frame.size.width/4;
	int bview_height= 48;
    
	NSLog(@"%f*%f",self.tabBarController.tabBar.frame.size.height,self.view.window.rootViewController.view.frame.size.height);

    Mybutton = [MyUIButton buttonWithType:UIButtonTypeCustom];
    Mybutton.MoveEnable = YES;
    Mybutton.frame = CGRectMake(280, 280, 40, 40);
    
    //TabBar上按键图标这设置
    [Mybutton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"converse.png"]] forState:UIControlStateNormal];
    [Mybutton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"converse.png"]] forState:UIControlStateSelected];
    [Mybutton setTag:10];
    
    [Mybutton addTarget:self action:@selector(tabbarbtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Mybutton];
    
    
    //tab bar view
	tabBarView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-110, self.view.frame.size.height/2-100, 200 , 200)] ;
    tabBarView.backgroundColor = [UIColor clearColor];
    
    //设置TabBar背景
    NSString *path=[[NSBundle mainBundle] pathForResource:@"Yuanview" ofType:@"png"];
    UIImage *img=[UIImage imageWithContentsOfFile:path];
    
    UIImageView *backGroundView=[[UIImageView alloc] initWithImage:img];
    backGroundView.frame = CGRectMake(0, 0, 200, 200);
    [tabBarView insertSubview:backGroundView atIndex:1];
    [tabBarView addSubview:backGroundView];
	[self.view addSubview:tabBarView];
    
    //set tabbar button 一个循环就够了不用写那么多
    for (int i=0; i<4; i++) {
        
        CGRect rect;
        rect.size.width = 60;
        rect.size.height = 60;
        switch (i) {
            case 0:
                rect.origin.x = 100-30;
                rect.origin.y = 40-30;
                break;
            case 1:
                rect.origin.x = 160-30;
                rect.origin.y = 100-30;
                break;
            case 2:
                rect.origin.x = 100-30;
                rect.origin.y = 160-30;
                break;
            case 3:
                rect.origin.x = 40-30;
                rect.origin.y = 100-30;
                break;
        }
        
        UIView *tabbgView = [[UIView alloc] initWithFrame:rect];
        // tabbgView.backgroundColor = [UIColor clearColor];
        [self.tabBarView addSubview:tabbgView];
        
        UIButton *tabBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tabBarButton.frame = CGRectMake(0, 0, 60, 60);
        
            //TabBar上按键图标这设置
            [tabBarButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"converse2.png"]] forState:UIControlStateNormal];
            [tabBarButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"converse2.png"]] forState:UIControlStateSelected];
            [tabBarButton setTag:i];
            
            [tabBarButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [tabbgView addSubview:tabBarButton];
            
            
            //设置标题
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(bview_x, label_y, btn_with, label_height)];
            titleLabel.textAlignment = UITextAlignmentCenter;
            titleLabel.adjustsFontSizeToFitWidth = YES;
            titleLabel.font = [UIFont systemFontOfSize:12];
            titleLabel.textColor = [UIColor whiteColor];
            titleLabel.backgroundColor = [UIColor clearColor];
            [tabBarButton addSubview:titleLabel];
            
            if (tabBarButton.tag==0) {
                self.btn1 = tabBarButton;
                [tabBarButton setSelected:true];
                titleLabel.text = @"";
            }
            if (tabBarButton.tag==1) {
                self.btn2 = tabBarButton;
                titleLabel.text = @"";
            }
            if (tabBarButton.tag==2) {
                self.btn3 = tabBarButton;
                titleLabel.text = @"";
            }
            if (tabBarButton.tag==3) {
                self.btn4 = tabBarButton;
                titleLabel.text = @"";
            }
    }
    
    UIView *tabbgViewd = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 40, 40)];
    [self.tabBarView addSubview:tabbgViewd];
    UIButton *tabBarButto = [UIButton buttonWithType:UIButtonTypeCustom];
    tabBarButto.frame = CGRectMake( 0, 0, 40 , 40);
    
    [tabBarButto setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"converse.png"]] forState:UIControlStateNormal];
    [tabBarButto setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"converse.png"]] forState:UIControlStateSelected];
    
    [tabBarButto addTarget:self action:@selector(tabbarbtn:) forControlEvents:UIControlEventTouchUpInside];
    tabBarButto.tag = 9;
    [tabbgViewd addSubview:tabBarButto];
    [tabBarView setHidden:YES];
}

- (void)buttonClicked:(id)sender
{
    NSLog(@"%d",[sender tag]);
	switch([sender tag])
	{
		case 0:
			[btn1 setSelected:true];
			[btn2 setSelected:false];
			[btn3 setSelected:false];
            [btn4 setSelected:false];
			break;
		case 1:
            [btn1 setSelected:false];
			[btn2 setSelected:true];
			[btn3 setSelected:false];
            [btn4 setSelected:false];
			break;
		case 2:
			[btn1 setSelected:false];
			[btn2 setSelected:false];
			[btn3 setSelected:true];
            [btn4 setSelected:false];
			break;
		case 3:
			[btn1 setSelected:false];
			[btn2 setSelected:false];
			[btn3 setSelected:false];
            [btn4 setSelected:true];
			break;
	}
    
    
    [[self.viewControllers objectAtIndex:[sender tag]] popToRootViewControllerAnimated:YES];
    self.selectedIndex = [sender tag];
	
}

- (void)hidethetabbar{
    [self hideTabBar];
    [self hideNewTabBar];
    [self makeTabBarHidden:YES];
}

- (void)showthetabbar{
    
    [self makeTabBarHidden:NO];
    [self showNewTabBar];
}

- (void)viewDidLoad
{
    tabbarbig = 0;
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidethetabbar) name:@"hihetabbar" object:[UIApplication sharedApplication]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showthetabbar) name:@"showtabbar" object:[UIApplication sharedApplication]];
}

- (void)tabbarbtn:(id)sender{
    NSLog(@"tabbarbtn");
    if (((UIButton*)sender).tag == 10) {
        if (!((MyUIButton*)sender).MoveEnabled) {
            if (tabbarbig == 0) {
                [self ShowA:((MyUIButton*)sender)];
                tabbarbig = 1;
            }else{
                [self ShowB];
                tabbarbig = 0;
            }
        }
    }else{
        [self ShowB];
        tabbarbig = 0;
    }
}

- (void)ShowA:(MyUIButton *) bt{
    CGRect rect = bt.frame;
    if (bt.center.x > self.view.frame.size.width - 100) {
        rect.origin.x = self.view.frame.size.width - 100 - bt.frame.size.width/2;
    }else if (bt.center.x < 100){
        rect.origin.x = 100 - bt.frame.size.width/2;
    }
    if (bt.center.y > self.view.frame.size.height - 100 - 49) {
        rect.origin.y = self.view.frame.size.height - 100 - bt.frame.size.height/2 - 49;
    }else if(bt.center.y < 100){
        rect.origin.y = 100  - bt.frame.size.height/2;
    }
    bt.frame = rect;
    bt.hidden = YES;
    newview = [[UIView alloc] initWithFrame:rect];
    [self.view addSubview:newview];
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    imagev.image = [UIImage imageNamed:@"TabbarYuan.png"];
    [newview addSubview:imagev];
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        CGRect rect1 = newview.frame;
        rect1.origin.x -= (100-bt.frame.size.width/2);
        rect1.origin.y -= (100-bt.frame.size.height/2);
        rect1.size.width = 200;
        rect1.size.height = 200;
        newview.frame = rect1;
    } completion:^(BOOL finished) {
        
    }];
    tabBarView.hidden = YES;
    CGRect rect2 = tabBarView.frame;
    rect2.origin.x = newview.frame.origin.x;
    rect2.origin.y = newview.frame.origin.y;
    tabBarView.frame = rect2;
    tabBarView.hidden = NO;
    [newview removeFromSuperview];
}

- (void)ShowB{
    CGRect rect = tabBarView.frame;
    
    newview = [[UIView alloc] initWithFrame:rect];
    [self.view addSubview:newview];
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    imagev.image = [UIImage imageNamed:@"TabbarYuan.png"];
    [newview addSubview:imagev];
    tabBarView.hidden = YES;
    
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        CGRect rect1 = newview.frame;
        rect1.origin.x += (100-tabBarView.frame.size.width/2);
        rect1.origin.y += (100-tabBarView.frame.size.height/2);
        rect1.size.width = 40;
        rect1.size.height = 40;
        newview.frame = rect1;
    } completion:^(BOOL finished) {
        [Mybutton setHidden:NO];
    }];
    [newview removeFromSuperview];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

 

这样我们就完成了.



  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
iOS自定义 TabBar 主要分为以下几个步骤: 1. 创建自定义 TabBar 创建一个继承于 UITabBar 的类,重写初始化方法和 layoutSubviews 方法,实现自定义 TabBar 的样式和布局。 2. 实现自定义 TabBarItem 创建一个继承于 UIButton 的类,用于实现自定义TabBarItem 样式,例如添加图片、文字等。 3. 设置自定义 TabBarItem 在自定义 TabBar 的初始化方法中,添加自定义TabBarItem,将其添加到 TabBar 上。 4. 替换系统 TabBar 在 AppDelegate 中,找到 TabBarController 的 tabBar 属性,将其替换为自定义TabBar。 示例代码: 自定义 TabBar 类: ``` class CustomTabBar: UITabBar { var items: [CustomTabBarItem] = [] override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setup() { // 隐藏默认的 TabBar self.tintColor = .clear self.backgroundImage = UIImage() self.shadowImage = UIImage() self.backgroundColor = .white } override func layoutSubviews() { super.layoutSubviews() // 设置自定义 TabBarItem 的布局 let itemWidth = self.frame.size.width / CGFloat(items.count) var itemIndex: CGFloat = 0 for item in items { item.frame = CGRect(x: itemWidth * itemIndex, y: 0, width: itemWidth, height: self.frame.size.height) itemIndex += 1 } } } ``` 自定义 TabBarItem 类: ``` class CustomTabBarItem: UIButton { var title: String? var normalImage: UIImage? var selectedImage: UIImage? override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setup() { // 设置 TabBarItem 的样式 self.imageView?.contentMode = .scaleAspectFit self.titleLabel?.font = UIFont.systemFont(ofSize: 12) self.setTitleColor(.gray, for: .normal) self.setTitleColor(.black, for: .selected) } override func layoutSubviews() { super.layoutSubviews() // 设置 TabBarItem 的布局 let imageHeight = self.frame.size.height * 0.6 self.imageView?.frame = CGRect(x: (self.frame.size.width - imageHeight) / 2, y: 5, width: imageHeight, height: imageHeight) self.titleLabel?.frame = CGRect(x: 0, y: self.frame.size.height - 20, width: self.frame.size.width, height: 20) } func set(title: String?, normalImage: UIImage?, selectedImage: UIImage?) { self.title = title self.normalImage = normalImage self.selectedImage = selectedImage // 设置 TabBarItem 的标题和图片 self.setTitle(title, for: .normal) self.setImage(normalImage, for: .normal) self.setImage(selectedImage, for: .selected) } } ``` 在自定义 TabBar 的初始化方法中,添加自定义TabBarItem: ``` class CustomTabBar: UITabBar { var items: [CustomTabBarItem] = [] override init(frame: CGRect) { super.init(frame: frame) setup() // 添加 TabBarItem let item1 = CustomTabBarItem() item1.set(title: "首页", normalImage: UIImage(named: "home_normal"), selectedImage: UIImage(named: "home_selected")) self.addSubview(item1) items.append(item1) let item2 = CustomTabBarItem() item2.set(title: "消息", normalImage: UIImage(named: "message_normal"), selectedImage: UIImage(named: "message_selected")) self.addSubview(item2) items.append(item2) let item3 = CustomTabBarItem() item3.set(title: "我的", normalImage: UIImage(named: "mine_normal"), selectedImage: UIImage(named: "mine_selected")) self.addSubview(item3) items.append(item3) } // ... } ``` 在 AppDelegate 中,找到 TabBarController 的 tabBar 属性,将其替换为自定义TabBar: ``` func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // ... let tabBarController = UITabBarController() tabBarController.viewControllers = [viewController1, viewController2, viewController3] // 替换为自定义 TabBar let customTabBar = CustomTabBar(frame: tabBarController.tabBar.frame) tabBarController.setValue(customTabBar, forKey: "tabBar") // ... return true } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值