案例描述:實現下圖所示的效果,被時間函數什麼的搞亂了,就只搭了一個框架,具體實現的計時功能等整好了一起上傳~~~ (看看這引入的n個頭文件,俺也是醉了~)
效果圖:
具體代碼:
#import "JRTabBarController.h"
#import "MiaoBiaoNavigationController.h"
#import "MiaoBiaoViewController.h"
#import "JSQNavigationController.h"
#import "JSQViewController.h"
#import "NaoZhongViewController.h"
#import "NZNavigationController.h"
#import "SJSZNavigationController.h"
#import "SJSZViewController.h"
#define kLeftSpace 30 //左邊距
#define kRightSpace 30 //右邊距
#define kMiddleSpace 40 //中間空白距
#define kW self.view.frame.size.width
#define kH self.view.frame.size.height
@interface JRTabBarController ()
//點擊按鈕切換圖片
@property (nonatomic,strong)NSMutableArray * buttonArray;
@end
@implementation JRTabBarController
#pragma mark - 懶加載
- (NSMutableArray *)buttonArray
{
if (_buttonArray==nil)
{
_buttonArray=[NSMutableArray array];
}
return _buttonArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
//1.加載控制器
[self _loadVC];
//2.自定義TabBar
[self _makeTabBar];
}
#pragma mark - 加載控制器
- (void) _loadVC
{
//創建視圖控制器
//世界時間
SJSZViewController * sjVC=[[SJSZViewController alloc]init];
SJSZNavigationController * vc1=[[SJSZNavigationController alloc]initWithRootViewController:sjVC];
//鬧鐘
NaoZhongViewController * nzVC=[[NaoZhongViewController alloc]init];
NZNavigationController * vc2=[[NZNavigationController alloc]initWithRootViewController:nzVC];
//秒錶
MiaoBiaoViewController * mbVC=[[MiaoBiaoViewController alloc]init];
// mbVC.view.backgroundColor=[UIColor redColor];
MiaoBiaoNavigationController * vc3=[[MiaoBiaoNavigationController alloc]initWithRootViewController:mbVC];
//計時器
JSQViewController * jsqVC=[[JSQViewController alloc]init];
JSQNavigationController * vc4=[[JSQNavigationController alloc]initWithRootViewController:jsqVC];
self.viewControllers=@[vc1,vc2,vc3,vc4];
self.selectedViewController=vc3;
}
#pragma mark - 自定義TabBar
- (void) _makeTabBar
{
//1.定製TabBar
UIView * bgView=[[UIView alloc] initWithFrame:CGRectMake(0,kH-49,kW,49)];
bgView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:bgView];
//2.定製button
CGFloat space=(kW-kLeftSpace-kRightSpace-3*kMiddleSpace)/4; //每個小按鈕的寬度
for (int i=0; i<4; i++)
{
UITabBarButton * button=[[UITabBarButton alloc]initWithFrame:CGRectMake(kLeftSpace+i*space+i*kMiddleSpace, 0, space, 49)];
button.backgroundColor=[UIColor whiteColor];
button.tag=i;
NSString * imageName=[NSString stringWithFormat:@"%d",i+1];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
if (i==2)
{
NSString * imageName=[NSString stringWithFormat:@"0%d",i+1];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
[button addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
button.delegateMe=self;
[self.buttonArray addObject:button];
[bgView addSubview:button];
}
}
- (void) changeImage:(UITabBarButton *) button
{
for (int i=0; i<4; i++)
{
if (i!=button.tag)
{
NSString * imageName=[NSString stringWithFormat:@"%d",i+1];
UIButton *butt=self.buttonArray[i];
[butt setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
}
[button.delegateMe changePage:button.tag];
NSString * imageName=[NSString stringWithFormat:@"0%d",(int)(button.tag+1)];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
- (void)changePage:(NSInteger)index
{
[UIView beginAnimations:nil context:nil];
[UIView commitAnimations];
self.selectedIndex=index;
}
@end
#import "MiaoBiaoViewController.h"
#define kW self.view.frame.size.width
#define kH self.view.frame.size.height
@interface MiaoBiaoViewController ()
{
NSDateFormatter *fomatter;
NSInvocationOperation *operation1;
NSInvocationOperation *operation2;
}
//@property (nonatomic,weak)
@end
@implementation MiaoBiaoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"秒錶";
//小時鐘
UILabel * conLabel=[[UILabel alloc]initWithFrame:CGRectMake(267, 85, 110, 50)];
// conLabel.backgroundColor=[UIColor redColor];
conLabel.text=@"00:00.00";
conLabel.font=[UIFont fontWithName:nil size:25];
[self.view addSubview:conLabel];
//秒錶
UILabel * ctLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,160,kW,150)];
// ctLabel.backgroundColor=[UIColor redColor];
ctLabel.text=@"00:00.00";
ctLabel.textAlignment=NSTextAlignmentCenter;
ctLabel.font=[UIFont fontWithName:nil size:75];
[self.view addSubview:ctLabel];
//下方視圖
UIView * bView=[[UIView alloc]initWithFrame:CGRectMake(0,350,kW,300)];
bView.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
[self.view addSubview:bView];
//開始停止按鈕
UIButton * ssButton=[[UIButton alloc]initWithFrame:CGRectMake((kW-200)/3, 30, 100, 100)];
ssButton.backgroundColor=[UIColor whiteColor];
ssButton.layer.cornerRadius=50;
[ssButton setTitle:@"開始" forState:UIControlStateNormal];
[ssButton setTitle:@"停止" forState:UIControlStateSelected];
[ssButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[ssButton setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
ssButton.tag=1;
[ssButton addTarget:self action:@selector(StartStop:) forControlEvents:UIControlEventTouchUpInside];
[bView addSubview:ssButton];
//計次按鈕
UIButton * jcButton=[[UIButton alloc]initWithFrame:CGRectMake(((kW-200)/3)*2+100, 30, 100, 100)];
jcButton.backgroundColor=[UIColor whiteColor];
jcButton.layer.cornerRadius=50;
[jcButton setTitle:@"計次" forState:UIControlStateNormal];
[jcButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[jcButton addTarget:self action:@selector(CountNum) forControlEvents:UIControlEventTouchUpInside];
[bView addSubview:jcButton];
}
- (void)StartStop:(UIButton *) button
{
button.selected = !button.selected;
NSLog(@"%i", button.selected);
NSLog(@"asdasdasd");
}
- (void)CountNum
{
NSLog(@"////////");
}
@end