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

 

这样我们就完成了.

iOS Dev 山寨迅雷看看的自定义的UITabbar(会动的UITabbar)_第1张图片


你可能感兴趣的:(object,Class,action,UIView,initialization,delay)