博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义TabBar
阅读量:7173 次
发布时间:2019-06-29

本文共 6576 字,大约阅读时间需要 21 分钟。

hot3.png

#import 
@interface VCRoot : UIViewController//定义一个工具栏视图@property (retain,nonatomic) UIView* mToolBarView ;@end#import "VCRoot.h"#import "ChechBox.h"@interface VCRoot ()@end@implementation VCRoot- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    //创建工具栏视图对象    CGFloat width = self.view.frame.size.width/5;    _mToolBarView = [[UIView alloc] init] ;    _mToolBarView.frame = CGRectMake(0, self.view.frame.size.height-44, 320, 44) ;        //设置背景颜色    _mToolBarView.backgroundColor = [UIColor colorWithRed:0.3 green:0.0 blue:0.5 alpha:0.8f] ;        [self.view addSubview:_mToolBarView] ;        //按钮到视图中    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;        btn.frame = CGRectMake(0, 2, width, 40) ;        [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside] ;        [btn setTitle:@"按钮" forState:UIControlStateNormal] ;        [_mToolBarView addSubview:btn] ;        UILabel* label = [[UILabel alloc] init] ;    label.frame = CGRectMake(width, 2, width, 40) ;    label.text = @"标签";    label.textAlignment = NSTextAlignmentCenter ;    //标签的手势    UITapGestureRecognizer* tapLabel = [[UITapGestureRecognizer alloc] init] ;        tapLabel.numberOfTapsRequired = 1 ;        [tapLabel addTarget:self action:@selector(pressLabel)] ;    //将标签添加一个手势    [label addGestureRecognizer:tapLabel] ;    label.userInteractionEnabled = YES ;        //自定义按钮    UIButton* btnCustom = [UIButton buttonWithType:UIButtonTypeCustom] ;        btnCustom.frame = CGRectMake(width*2, 2, width, 40) ;        [btnCustom setImage:[UIImage imageNamed:@"iphone-on.png"] forState:UIControlStateNormal];    [btnCustom setImage:[UIImage imageNamed:@"iphone-off.png"] forState:UIControlStateHighlighted];    [btnCustom addTarget:self action:@selector(pressBtnCus) forControlEvents:UIControlEventTouchUpInside] ;        [_mToolBarView addSubview:label] ;    [_mToolBarView addSubview:btnCustom] ;        UIImageView* iView = [[UIImageView alloc] init] ;    iView.frame = CGRectMake(width*3, 2, width, 40) ;        iView.image = [UIImage imageNamed:@"pumpkin"] ;    //响应交互事件手势事件    //imageView默认为NO    iView.userInteractionEnabled = YES ;        UITapGestureRecognizer* tapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)] ;    //两次点击    tapTwo.numberOfTapsRequired = 2 ;        [iView addGestureRecognizer:tapTwo] ;        [_mToolBarView addSubview:iView] ;        self.navigationController.toolbar.hidden = YES ;        ChechBox* check = [[ChechBox alloc] init] ;    check.frame = CGRectMake(width*4+width/3, 2, width, 32) ;        check.on = YES ;        [check addTarget:self action:@selector(checkAct:) forControlEvents:UIControlEventValueChanged] ;        [_mToolBarView addSubview:check] ;}-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    static BOOL isShow = NO ;        [UIView beginAnimations:nil context:nil] ;    [UIView setAnimationDuration:1] ;    if (isShow == NO) {        _mToolBarView.frame = CGRectMake(0, self.view.frame.size.height, 320, 44) ;//移动消失//        _mToolBarView.alpha = 0 ;//透明度渐变    }    else    {        _mToolBarView.frame = CGRectMake(0, self.view.frame.size.height-44, 320, 44) ;//        _mToolBarView.alpha = 1 ;    }    isShow = !isShow ;    [UIView commitAnimations] ;}-(void) checkAct:(ChechBox*) checkBox{    NSLog(@"check = %@",checkBox) ;}-(void) pressBtn{    NSLog(@"按钮按下!");}-(void) pressBtnCus{    NSLog(@"自定义按钮按下!");}-(void) pressLabel{    NSLog(@"标签按下!");}-(void) tapImage:(UITapGestureRecognizer*) tapTwo{    NSLog(@"图片被双击!");}@end

 

#import 
//声明一个选择按钮//UIControl:可以响应事件的控件对象//UIControl:继承于UIView@interface ChechBox : UIControl{    //选中图片对象    UIImage* _imageSelect ;    //取消选中图片对象    UIImage* _imageNoSelect ;    //显示到视图上    UIImageView* _imageView ;    //选中状态    BOOL     _on ;        //事件目标对象,当前控件的事件函数的拥有者对象    //_target:事件函数声明实现在对象中    //通常使用试图控器对象作为此值    id       _target ;        //事件函数对象(指针)声明    //用来响应响应的事件处理    //通过外部来赋值    //实现在外部,_target对象中实现    SEL      _selector ;}//定义on属性@property (assign,nonatomic) BOOL on ;//重新定义setOn函数-(void) setOn:(BOOL)on ;//设置选中状态图片-(void) setSelectedImage:(UIImage*) image ;//设置取消状态的图片-(void) setNoSelectedImage:(UIImage*) image;//添加事件函数声明-(void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents ;@end#import "ChechBox.h"@implementation ChechBox- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code                //加载图片对象        _imageSelect = [UIImage imageNamed:@"selected"] ;                _imageNoSelect = [UIImage imageNamed:@"noselected"] ;                //创建视图        _imageView = [[UIImageView alloc] initWithImage:_imageNoSelect] ;        _imageView.contentMode =  UIViewContentModeCenter;        //只能通过外部的参数设定位置        //不能设定控件的大小        _imageView.frame = CGRectMake(0, 0, 32, 32);                _target = nil ;        _selector = nil ;        _on = NO ;                [self addSubview:_imageView] ;            }    return self;}-(void) setSelectedImage:(UIImage *)image{    _imageSelect = image ;    _imageView.image = _imageNoSelect ;}-(void) setNoSelectedImage:(UIImage *)image{    _imageNoSelect = image ;    _imageView.image = _imageNoSelect ;}//添加设置事件函数-(void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{//    controlEvents             == 0001100000//    UIControlEventValueChanged== 1000000000//                              == 0000000000    if ((controlEvents &UIControlEventValueChanged) != 0)    {        _target = target ;        _selector = action ;    }}-(void) setOn:(BOOL)on{    //状态改变    if (_on != on)    {        _on = on ;                if (_on == YES)        {            _imageView.image = _imageSelect ;        }        else if (_on == NO)        {            _imageView.image = _imageNoSelect ;        }                //respondsToSelector:target对象能否执行_selector函数        //功能:避免程序由于没有实现_selector,导致程序直接崩溃        //如果实现:返回值为YES        if ([_target respondsToSelector:_selector] == YES)        {            //通过target action模式执行事件函数            [_target performSelector:_selector withObject:self afterDelay:0] ;        }    }}-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    self.on = !self.on ;}@end

 

175439_81mR_2319073.png

175439_z4vj_2319073.png

175439_VmPP_2319073.png

 

转载于:https://my.oschina.net/u/2319073/blog/653029

你可能感兴趣的文章
管理Linux日志文件工具
查看>>
Windows下bat脚本判断端口是否可用
查看>>
oracle创建表
查看>>
解决 Eclipse build workspace 慢,validation javascript 更慢的问题
查看>>
jquery ajax验证用户名是否存在(后台spring mvc)
查看>>
solaris x86安装ORACLE 11.2.0.3软件时因SWAP不足报错: INFO: ld: fatal: mmap anon failed
查看>>
paoding分词
查看>>
jquery动态创建form表单
查看>>
hdu 5361 2015多校联合训练赛#6 最短路
查看>>
php中PHPMailer发送带附件的电子邮件方法
查看>>
DOCKER_HOST have a weird tcp
查看>>
Redisclient连接方式Hiredis简单封装使用,连接池、屏蔽连接细节
查看>>
微信支付:价格问题:如果支付金额是单位是分,不能带小数点
查看>>
微软小冰发威,招摇人工智能
查看>>
HDOJ 4009 Transfer water 最小树形图
查看>>
Maven的SSH搭建以及部署
查看>>
常用函数 __MySQL必知必会
查看>>
JavaScript--语法4--函数1
查看>>
Spring Boot注解(annotation)列表
查看>>
hdu - 4974 - A simple water problem(贪心 + 反证)
查看>>