#import "ViewController.h" #import "CGTabbar.h" #import "QHCListView.h" #import "QHJavaListView.h" #import "QHOCListView.h" @interface ViewController ()<CGTabbarDelegate> @property(nonatomic,strong)NSArray *books; @property(nonatomic,weak)QHOCListView *ocListView; @property(nonatomic,weak)QHJavaListView *javaListView; @property(nonatomic,weak)QHCListView *cListView; @end @implementation ViewController -(QHOCListView *)ocListView { if (_ocListView == nil) { QHOCListView *ocListView = [QHOCListView ocListView]; [self.view addSubview:ocListView]; _ocListView = ocListView; } return _ocListView; } -(QHJavaListView *)javaListView { if (_javaListView == nil) { QHJavaListView *javaListView = [QHJavaListView javaListView]; [self.view addSubview:javaListView]; _javaListView = javaListView; NSLog(@"+++++++++++"); } return _javaListView; } -(QHCListView *)cListView { if (_cListView == nil) { QHCListView *cListView = [QHCListView cListView] ; [self.view addSubview:cListView]; _cListView = cListView; } return _cListView; } - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle]pathForResource:@"books.plist" ofType:nil]; _books = [NSArray arrayWithContentsOfFile:path]; //NSLog(@"%@",_books); CGTabbar *tabbar = [CGTabbar tabbar]; [self.view addSubview:tabbar]; UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom]; [self configButton:btn1 andTitile:@"OC" andImageName:@"upomp_button_keyboard3" andSelectedImageName:@"upomp_button_keyboard3_highlighted"]; [self configButton:btn2 andTitile:@"Java" andImageName:@"upomp_button_keyboard3" andSelectedImageName:@"upomp_button_keyboard3_highlighted"]; [self configButton:btn3 andTitile:@"C" andImageName:@"upomp_button_keyboard3" andSelectedImageName:@"upomp_button_keyboard3_highlighted"]; tabbar.delegate = self; tabbar.items = @[btn1,btn2,btn3]; NSLog(@"%@",_books); // Do any additional setup after loading the view, typically from a nib. } -(void)configButton:(UIButton *)btn andTitile:(NSString *)title andImageName:(NSString *)imageName andSelectedImageName:(NSString *)selectedImageName { [btn setTitle:title forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected]; } -(void)tabbarItemSelected:(CGTabbar *)tabbar andItem:(UIButton *)item andIndex:(NSInteger)index { if (index==0) { [self.view bringSubviewToFront:self.ocListView]; NSLog(@"--------------"); } else if(index ==1) { [self.view bringSubviewToFront:self.javaListView]; } else if(index ==2) { [self.view bringSubviewToFront:self.cListView]; } [self.view bringSubviewToFront:tabbar]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface QHOCListView : UIView +(id)ocListView; @property(nonatomic,strong)NSArray *books; @end
#import "QHOCListView.h" @implementation QHOCListView +(id)ocListView { return [[self alloc]init]; } -(void)willMoveToSuperview:(UIView *)newSuperview { NSString *path = [[NSBundle mainBundle]pathForResource:@"books.plist" ofType:nil]; _books = [NSArray arrayWithContentsOfFile:path]; NSLog(@"%@",_books); self.frame = newSuperview.bounds; self.backgroundColor = [UIColor redColor]; int totalColumns = 3; // 应用的尺寸 CGFloat bookW = 90; CGFloat bookH = 125; // 间隙 = (view的宽度 - 3 * 应用宽度) / 4 CGFloat marginX = (self.frame.size.width - totalColumns * bookW) / (totalColumns + 1); CGFloat marginY = 15; // 根据应用个数创建对应的框(index) for (int index = 0; index<self.books.count; index++) { // 创建 UIView *bookView = [[UIView alloc] init]; // 设置背景色 bookView.backgroundColor = [UIColor cyanColor]; // 计算框的位置 // 计算行号和列号 int row = index / totalColumns; int col = index % totalColumns; // 计算x和y CGFloat bookX = marginX + col * (bookW + marginX); CGFloat bookY = 20 + row * (bookH + marginY); // 设置frame bookView.frame = CGRectMake(bookX, bookY, bookW, bookH); //添加框到控制器的view [self addSubview:bookView]; // 添加控件 // index位置对应的应用信息 NSDictionary *bookInfo = self.books[index]; // 添加图片 //UIImageView *iconView = [[UIImageView alloc] init]; UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setBackgroundImage:[UIImage imageNamed:bookInfo[@"icon"]] forState:UIControlStateNormal]; // 设置位置 CGFloat iconW = 60; CGFloat iconH = 80; CGFloat iconX = (bookW - iconW) * 0.5; CGFloat iconY = 0; btn.frame = CGRectMake(iconX, iconY, iconW, iconH); [bookView addSubview:btn]; // 添加名字 UILabel *nameLabel = [[UILabel alloc] init]; // 设置位置 CGFloat nameW = bookW; CGFloat nameH = 20; CGFloat nameX = 0; CGFloat nameY = iconY + iconH; nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH); // 设置文字 nameLabel.text = bookInfo[@"name"]; // 设置字体 nameLabel.font = [UIFont systemFontOfSize:13]; // 设置文字居中对齐 nameLabel.textAlignment = NSTextAlignmentCenter; [bookView addSubview:nameLabel]; // 添加下载按钮 UIButton *downloadBtn = [[UIButton alloc] init]; // 设置位置 CGFloat downloadX = 12; CGFloat downloadY = nameY + nameH; CGFloat downloadW = bookW - 2 * downloadX; CGFloat downloadH = 20; downloadBtn.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH); // 设置默认的背景 UIImage *normalImage = [UIImage imageNamed:@"upomp_button_keyboard3"]; [downloadBtn setBackgroundImage:normalImage forState:UIControlStateNormal]; // 设置高亮的背景 UIImage *highImage = [UIImage imageNamed:@"upomp_button_keyboard3_highlighted"]; [downloadBtn setBackgroundImage:highImage forState:UIControlStateHighlighted]; // 设置按钮的文字 [downloadBtn setTitle:@"download" forState:UIControlStateNormal]; // 设置按钮文字的字体 downloadBtn.titleLabel.font = [UIFont systemFontOfSize:13]; [bookView addSubview:downloadBtn]; } } @end
#import <UIKit/UIKit.h> @class CGTabbar; @protocol CGTabbarDelegate <NSObject> -(void)tabbarItemSelected:(CGTabbar *)tabbar andItem:(UIButton *)item andIndex:(NSInteger)index; @end @interface CGTabbar : UIView @property(nonatomic,strong)NSArray *items; @property(nonatomic,assign)id<CGTabbarDelegate>delegate; +(id)tabbar; @end
#import "CGTabbar.h" @implementation CGTabbar +(id)tabbar { return [[self alloc]init]; } -(id)initWithFrame:(CGRect)frame { if (self =[super initWithFrame:frame]) { } return self; } -(void)btnTouch:(UIButton *)btn { if (btn.selected) return; for(UIButton *tmp in self.items) { tmp.selected = NO; } btn.selected = YES; [_delegate tabbarItemSelected:self andItem:btn andIndex:btn.tag]; } -(void)setItems:(NSArray *)items { _items =items; CGFloat btnW = self.frame.size.width/items.count; for (int i = 0; i < items.count; i++) { UIButton *btn = items[i]; [self addSubview:btn]; CGFloat btnX = i*btnW; CGFloat btnY = 0; CGFloat btnH = self.frame.size.height; btn.frame = CGRectMake(btnX, btnY, btnW, btnH); [btn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside]; btn.tag = i; [self btnTouch:items[0]]; } } -(void)willMoveToSuperview:(UIView *)newSuperview { CGFloat selfW = newSuperview.bounds.size.width; CGFloat selfH = 49; CGFloat selfX = 0; CGFloat selfY = newSuperview.bounds.size.height-selfH; self.frame = CGRectMake(selfX, selfY, selfW, selfH); self.backgroundColor = [UIColor grayColor]; } @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-26 22:27:15