第一个APP 时钟

//  ViewController.h

//  IOS310803

//

//  Created by 张艳锋 on 15/8/3.

//  Copyright (c) 2015年 zyf. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource,UIAlertViewDelegate,UIActionSheetDelegate>

{

//顶部

    UILabel *lable;//你好时间

//中部

    //闹钟

    UIView *clockview;

    UIScrollView *clockscrollView;//存放lable,lable记录已经添加的按钮

    NSString *path;

    UILabel *clocklablenow;//现实时间显示标签.通过闹钟记录字符串与标签内容字符串进行比较,如果相同闹钟响;

    UIButton *colckbutton1;

    UIButton *colckbutton2;

    UITextField *aclock;//提示框中的文本输入框

    NSMutableArray *clocklist;

    //秒表

    UILabel * mblable;

    UIButton * mbbutton1;

    UIButton * mbbutton2;

    

    //计时器

    UIView *jsview;

    UIPickerView *jspickview;

    UILabel *jslable;

    UIButton *jsbutton1;

    UIButton *jsbutton2;

    BOOL isClick1;

    BOOL isClick2;

    NSInteger a,b,c,mus;

 

//尾部

}

@end

 

//

//  ViewController.m

//  IOS310803

//

//  Created by 张艳锋 on 15/8/3.

//  Copyright (c) 2015年 zyf. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

static int t=0;

- (void)viewDidLoad {

    [super viewDidLoad];

    //全局背景

    self.view.backgroundColor=[UIColor lightGrayColor];

    

    //顶部的lable

    lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 375, 80)];

    lable.backgroundColor=[UIColor lightGrayColor];

    //背景色,字体及居中

    [lable setText:@"时间,你好"];

    [lable setTextAlignment:NSTextAlignmentCenter];

    [self.view addSubview:lable];

    //中部(0,100,375,500)

     

    

    [NSTimer    scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

 

    int x=4;

    int y=2;

    NSLog(@"%d,%d",x%y,x/y);

 

    //底部

    /***************UISegmentedControl***************************/

    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"世界时钟",@"闹钟",@"秒表",@"计时器",nil];

    //初始化UISegmentedControl

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];

    segmentedControl.frame = CGRectMake(0, 600, 375, 67);

    segmentedControl.backgroundColor=[UIColor whiteColor];

    segmentedControl.tintColor = [UIColor redColor];

    //添加委托方法

    [segmentedControl addTarget:self action:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:segmentedControl];

}

 

 

-(void)segmentAction:(UISegmentedControl *)Seg{

    

    NSInteger Index = Seg.selectedSegmentIndex;

    

    NSLog(@"Index %li", (long)Index);

    

    switch (Index) {

            

        case 0:

        {

            lable.text=@"世界时钟";

 

        }

            break;

            

        case 1:

        {

            lable.text=@"闹钟";

            //第一层页面,底层view

 

            //添加中部视图底板

            clockview=[[UIView alloc]init];

            clockview.frame=CGRectMake(0, 100, 375, 500);

            clockview.backgroundColor=[UIColor blackColor];

            [self.view addSubview:clockview];

            

            //第二层,时间抓取器+显示时间的标签(底部200像素空余)

            //抓取时间的时间选择器

            

            //最下面一排添加按钮

            colckbutton1=[[UIButton alloc]init];

            colckbutton1.frame=CGRectMake(50, 320, 100, 100);

            colckbutton1.layer.masksToBounds=YES;

            colckbutton1.layer.cornerRadius=mbbutton1.frame.size.width/2;

            colckbutton1.backgroundColor=[UIColor greenColor];

            [colckbutton1 setTitle:@"添加" forState:UIControlStateNormal];

            [colckbutton1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal ];//平时颜色

            [colckbutton1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];//点击时颜色

            

            colckbutton2=[[UIButton alloc]init];

            colckbutton2.frame=CGRectMake(225, 320, 100, 100);//大小

            colckbutton2.layer.masksToBounds=YES;

            colckbutton2.layer.cornerRadius=mbbutton2.frame.size.width/2;//圆形

            colckbutton2.backgroundColor=[UIColor greenColor];

            [colckbutton2 setTitle:@"删除" forState:UIControlStateNormal];

            [colckbutton2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

            [colckbutton2 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];

            //添加事件

            [colckbutton1 addTarget:self action:@selector(addClock) forControlEvents:UIControlEventTouchUpInside];

            [colckbutton2 addTarget:self action:@selector(deleteClock) forControlEvents:UIControlEventTouchUpInside];

            [clockview addSubview:colckbutton1];

            [clockview addSubview:colckbutton2];

  

 

        }

            break;

            

        case 2:

        {

            lable.text=@"秒表";

            //添加jsview,lable,按钮1,按钮2;

            UIView * mbview=[[UIView alloc]init];

            mbview.frame=CGRectMake(0, 100, 375, 500);

            mbview.backgroundColor=[UIColor grayColor];

            [self.view addSubview:mbview];

            

            //lable便签

            mblable=[[UILabel alloc]init];

            mblable.frame=CGRectMake(0, 40, 375, 160);

            mblable.backgroundColor=[UIColor brownColor];//设背景为白色

            mblable.textAlignment=NSTextAlignmentCenter;//设置文字居中

            mblable.textColor=[UIColor blackColor];//设置字体为黑色

            mblable.font=[UIFont systemFontOfSize:50 weight:2];//字体大小,粗细

            [mbview  addSubview:mblable];

            

            //按钮1,按钮2

            mbbutton1=[[UIButton alloc]init];

            mbbutton1.frame=CGRectMake(50, 320, 100, 100);

            mbbutton1.layer.masksToBounds=YES;

            mbbutton1.layer.cornerRadius=mbbutton1.frame.size.width/2;

            mbbutton1.backgroundColor=[UIColor greenColor];

            [mbbutton1 setTitle:@"启动" forState:UIControlStateNormal];

            [mbbutton1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal ];//平时颜色

            [mbbutton1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];//点击时颜色

            

            

            mbbutton2=[[UIButton alloc]init];

            mbbutton2.frame=CGRectMake(225, 320, 100, 100);//大小

            mbbutton2.layer.masksToBounds=YES;

//            mbbutton2.layer.cornerRadius=mbbutton2.frame.size.width/2;//圆形

            mbbutton2.backgroundColor=[UIColor greenColor];

            [mbbutton2 setTitle:@"重置" forState:UIControlStateNormal];

            [mbbutton2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

            [mbbutton2 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];

            //添加事件

            [mbbutton1 addTarget:self action:@selector(startOrstop:) forControlEvents:UIControlEventTouchUpInside];

            [mbbutton2 addTarget:self action:@selector(reSet:) forControlEvents:UIControlEventTouchUpInside];

            [mbview addSubview:mbbutton1];

            [mbview addSubview:mbbutton2];

            a=0;

            b=0;

            c=0;

            mus=0;

            mblable.text=[NSString stringWithFormat:@"%ld:%ld:%ld",(long)a,(long)b,(long)c];

            

            

        }

            break;

            

        case 3:

        {

            

            lable.text=@"计时器";

            //添加jsview,时间选择器,lable,按钮1,按钮2;

            jsview=[[UIView alloc]init];

            jsview.frame=CGRectMake(0, 100, 375, 500);

            jsview.backgroundColor=[UIColor blackColor];

            jslable.textColor=[UIColor whiteColor];

            

            [self.view addSubview:jsview];

            

            //时间选择器

            jspickview=[[UIPickerView alloc]init];

            jspickview=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 375, 120)];

            jspickview.dataSource=self;

            jspickview.delegate=self;

            jspickview.backgroundColor=[UIColor yellowColor];

            [jsview addSubview:jspickview];

            

            //lable便签

            jslable=[[UILabel alloc]init];

            jslable.frame=CGRectMake(0, 120, 375, 200);

            jslable.backgroundColor=[UIColor blueColor];

            jslable.textAlignment=NSTextAlignmentCenter;//设置文字居中

            jslable.font=[UIFont systemFontOfSize:50 weight:5];//设置字体大小粗细

            [jsview  addSubview:jslable];

            

            //按钮1,按钮2

            jsbutton1=[[UIButton alloc]init];

            jsbutton1.frame=CGRectMake(50, 320, 100, 100);

            jsbutton1.layer.masksToBounds=YES;

            jsbutton1.layer.cornerRadius=jsbutton1.frame.size.width/2;

            jsbutton1.backgroundColor=[UIColor greenColor];

            [jsbutton1 setTitle:@"开始计时" forState:UIControlStateNormal];

            [jsbutton1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal ];//平时颜色

            [jsbutton1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];//点击时颜色

            

            

            jsbutton2=[[UIButton alloc]init];

            jsbutton2.frame=CGRectMake(225, 400, 60, 60);//大小

            jsbutton2.layer.masksToBounds=YES;

            jsbutton2.layer.cornerRadius=jsbutton2.frame.size.width/2;//圆形

            jsbutton2.backgroundColor=[UIColor greenColor];

            [jsbutton2 setTitle:@"暂停" forState:UIControlStateNormal];

            [jsbutton2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

            [jsbutton2 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];

            //添加事件

            [jsbutton1 addTarget:self action:@selector(startOrcancel:) forControlEvents:UIControlEventTouchUpInside];

            [jsbutton2 addTarget:self action:@selector(suspendedOrcontinue:) forControlEvents:UIControlEventTouchUpInside];

            [jsview addSubview:jsbutton1];

            [jsview addSubview:jsbutton2];

        }

            break;

            

            

        default:

            

            break;

            

    }

}

 

 

 

 

 

 

#pragma mark 世界时钟

//定时器运行

-(void)timerAction:(NSTimer *)timer{

    t+=1;

    if (t==100) {

        t=0;

    }

    //世界时钟完成

    if ([lable.text isEqualToString:@"世界时钟"]&&t==99) {

            UIView *wtview=[[UIView alloc]init];

            wtview.tag=10;

            wtview.frame=CGRectMake(0, 100, 375, 500);

            wtview.backgroundColor=[UIColor greenColor];

            [self.view addSubview:wtview];

//抓取时间的时间选择器

            UIDatePicker *wtdatePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];

            wtdatePicker.tag=11;

            wtdatePicker.backgroundColor=[UIColor redColor];//标记颜色便于观看

            wtdatePicker.datePickerMode=UIDatePickerModeDateAndTime;

            wtdatePicker.date=[NSDate date];

            [wtview addSubview:wtdatePicker];

//显示时间标签

            UILabel *wtlable=[[UILabel alloc]init];

            wtlable.frame=CGRectMake(0, 200, 375, 200);

            wtlable.tag=12;

            wtlable.backgroundColor=[UIColor blueColor];

            wtlable.textAlignment=NSTextAlignmentCenter;//设置文字居中

            NSDateFormatter *formatter=[[NSDateFormatter alloc]init];

            formatter.dateFormat=@"YYYY-MM-dd hh:mm:ss aaa";

            wtlable.text=[formatter stringFromDate:wtdatePicker.date];

            [wtview   addSubview:wtlable];

    }

    //闹钟

    else if ([lable.text isEqualToString:@"闹钟"]&&t==99){

        

        UIDatePicker *clockdatePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];

        clockdatePicker.datePickerMode=UIDatePickerModeTime;//只要时间不要日期

        clockdatePicker.date=[NSDate date];//获取现在时间

        [clockview addSubview:clockdatePicker];

        //显示时间标签

        clocklablenow=[[UILabel alloc]init];

        clocklablenow.frame=CGRectMake(0, 200, 375, 100);

        clocklablenow.textAlignment=NSTextAlignmentCenter;//设置文字居中

        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];

        formatter.dateFormat=@"HH:mm:ss";//从弹窗输入的冒号为中文式样此处也设为中文式样方便比对

        clocklablenow.text=[formatter stringFromDate:clockdatePicker.date];

        NSLog(@"%@",clocklablenow.text);

        [clockview   addSubview:clocklablenow];

        //第三层页面上部由可滚动视图占领(遮盖住了第二层的时间抓取器和时间显示标签),下部由一串按钮组成(占满视图无空余)

        clockscrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 300)];

        //获取plist文件里面已经设置好的按钮

        path=@"/Users/scjy/Desktop/cx/IOS/IOS310803/IOS310803/Clockmemory List.plist";

        clocklist=[NSMutableArray arrayWithContentsOfFile:path];

        //设置有效区域大小,有多少个lable按需求设置

        clockscrollView.contentSize=CGSizeMake(375, (62*clocklist.count)    );//有效的显示区域

        //此处

        clockscrollView.backgroundColor=[UIColor redColor];

        [clockview addSubview:clockscrollView];//将可滚动视图框添加到底部view上面

 

        //将每一条记录都通过一个一个标签添加到可滚动视图框中

        for (int i=0; i<clocklist.count; i++) {

            //获取第I个闹钟记录字符串

            NSString *alist=[clocklist objectAtIndex:i];

            

            //创建第I个lable

            UILabel *clocklable=[[UILabel alloc]initWithFrame:CGRectMake(0, 62*i, 375, 60)];

            //将闹钟记录字符串设置为标签的文本内容

            clocklable.text=alist;

            //将背景设为白色

            clocklable.backgroundColor=[UIColor whiteColor];

            //用超细的带背景色视图带色添加间隔线

            UIView *redview1 = [[UIView alloc]initWithFrame:CGRectMake(0,(60+62*i), 375, 2)];

            redview1.backgroundColor = [UIColor redColor];

            [clockscrollView addSubview:redview1];

            //将字体设为黑色

            clocklable.textAlignment=NSTextAlignmentCenter;//设置文字居中

            

            clocklable.textColor=[UIColor blackColor];

            //将闹钟记录添加到滚动视图框中

            [clockscrollView addSubview:clocklable];

            //如果闹钟时间和抓取现实时间相等,跳出弹窗,以后可以播放音乐什么的

            if ([clocklable.text isEqualToString:clocklablenow.text]) {

                UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:@"时间到,主人" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

                [alertView show];

            }

            

        }

        

        

    

    }

    //秒表完成

    else if ([lable.text isEqualToString:@"秒表"]){

        if ([mbbutton1.titleLabel.text isEqualToString:@"停止"]) {

            mus+=1;

            a=mus/6000;

            b=(mus%6000)/100;

            c=mus%100;

            mblable.text=[NSString stringWithFormat:@"%ld分%ld秒%ld",(long)a,(long)b,(long)c];

        }

    }

    //计时器完成

    else if([lable.text isEqualToString:@"计时器"]&&t==99){

        if ([jsbutton1.titleLabel.text isEqualToString:@"取消"]&&[jsbutton2.titleLabel.text isEqualToString:@"暂停"]) {

            mus=a*3600+b*60+c;

             if (mus==0) {

                jslable.text=@"时间到";

                 //时间到后,时间选择器回归到00:00:00

                 jspickview=[[UIPickerView alloc]init];

                 jspickview=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 375, 120)];

                 jspickview.dataSource=self;

                 jspickview.delegate=self;

                 jspickview.backgroundColor=[UIColor yellowColor];

                 [jsview addSubview:jspickview];

                //此处可以加弹窗

                [jsbutton1 setTitle:@"开始计时" forState:UIControlStateNormal];

                 

                isClick1=NO;

            }

            else if(mus<0){

                mus=0;

            }

            

            else{

                mus-=1;

                a=mus/3600;

                b=(mus%3600)/60;

                c=mus%60;

                NSLog(@"%ld:%ld:%ld:%ld",a,b,c,mus);

                jslable.text=[NSString stringWithFormat:@"%ld:%ld:%ld",(long)a,(long)b,(long)c];

            }

            

        }

    }

 }

 

 

#pragma mark 闹钟

/************************************************************/

//弹框(没有使用)

-(void)addClock

{

    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"设置时" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

    alertView.delegate=self;

    //添加输入框

    alertView.alertViewStyle=UIAlertViewStylePlainTextInput;

    

    aclock=[alertView textFieldAtIndex:0];

    //提示信息

    aclock.placeholder=@"00:00:00";

    [alertView show];//弹框

    

    //获取

 

}

//alertView上面的按钮点击事件(没有使用)

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {

        NSLog(@"sure");

        clocklist=[NSMutableArray arrayWithContentsOfFile:path];

        //添加

        [clocklist addObject:aclock.text];

        //写入

        [clocklist writeToFile:path atomically:YES];

       

    }

 

}

 

-(void)deleteClock{

   

    clocklist=[NSMutableArray arrayWithContentsOfFile:path];

    //初始化一个UIActionSheet

    UIActionSheet *actionSheet1=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil];

    for (int i=0; i<clocklist.count; i++) {

        [actionSheet1 addButtonWithTitle:[clocklist objectAtIndex:i]];

    }

    //添加到self.view中

    [actionSheet1 showInView:self.view];

    

}

#pragma marks - UIActionSheetDelegate

//actionSheet上面的按钮点击事件

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    //获取源文件

    clocklist=[NSMutableArray arrayWithContentsOfFile:path];

  //移除一个

    for (int i=1; i<clocklist.count+1; i++) {

        //按钮从第一个开始的而不是从零开始的

        //按钮的i对应clocklist的i-1;

        if (buttonIndex==i) {

            [clocklist removeObject:[clocklist objectAtIndex:i-1]];

            //写入

            [clocklist writeToFile:path atomically:YES];

            break;

        }

    }

    

}

 

 

 

/************************************************************/

 

 

#pragma mark 秒表

-(void)startOrstop:(UIButton *)sender{

    

    if (!isClick1) {

        [mbbutton1 setTitle:@"停止" forState:UIControlStateNormal];

        isClick1=YES;

    }

    else{

        [mbbutton1 setTitle:@"启动" forState:UIControlStateNormal];

        isClick1=NO;

    }

}

-(void)reSet:(UIButton *)sender{

    [mbbutton1 setTitle:@"启动" forState:UIControlStateNormal];

    isClick1=NO;

        a=0;

        b=0;

        c=0;

        mus=0;

        mblable.text=[NSString stringWithFormat:@"%ld:%ld:%ld",(long)a,(long)b,(long)c];

}

 

 

 

/************************************************************/

#pragma mark 计时器

//选择器PickerView

//行数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 6;

}

//列数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    if (component==0) {//第一列12行

        return 24;

    }

    else if (component==1){//第二列1行

        return 1;

    }

    else if (component==2){

        return 60;

    }

    else if (component==3){

        return 1;

    }

    else if (component==4){

        return 60;

    }

    else

    return 1;

}

//设置每一行的标题

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    if (component==0) {

       

        return [NSString stringWithFormat:@"%d",(int)row];

    }

    else if(component==1){

        return @"时";

    }

    else if (component==2){

        

        return [NSString stringWithFormat:@"%d",(int)row];

    }

    else if (component==3){

        return @"分";

    }

    else if (component==4){

        return [NSString stringWithFormat:@"%d",(int)row];

    }

    else

        return @"秒";

}

//获取单元行的内容显示在lable中

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{//主要用于第一次获取

    a=[pickerView selectedRowInComponent:0];

    b=[pickerView selectedRowInComponent:2];

    c=[pickerView selectedRowInComponent:4];

    jslable.textColor=[UIColor whiteColor];

    jslable.text=[NSString stringWithFormat:@"%ld:%ld:%ld",(long)a,(long)b,(long)c];

 

}

//按钮事件jsbutton1

//按钮自动默认为以NO开始;

-(void)startOrcancel:(UIButton *)sender{

 

    if (!isClick1) {

        NSLog(@"sd");

        [jsbutton1 setTitle:@"取消" forState:UIControlStateNormal];

        isClick1=YES;

        [jsview addSubview:jsbutton1];

    }

    else{

        NSLog(@"fs");

        //点击取消后,时间选择器回归到00:00:00

        jspickview=[[UIPickerView alloc]init];

        jspickview=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 375, 120)];

        jspickview.dataSource=self;

        jspickview.delegate=self;

        jspickview.backgroundColor=[UIColor yellowColor];

        [jsview addSubview:jspickview];

        //lable里面也回归00:00:00

        a=[jspickview selectedRowInComponent:0];

        b=[jspickview selectedRowInComponent:2];

        c=[jspickview selectedRowInComponent:4];

        jslable.text=[NSString stringWithFormat:@"%ld:%ld:%ld",(long)a,(long)b,(long)c];

        

 

        

        //改变按钮title,重新添加

        [jsbutton1 setTitle:@"开始计时" forState:UIControlStateNormal];

        isClick1=NO;

        [jsbutton2 setTitle:@"暂停" forState:UIControlStateNormal];

        isClick2=NO;

 

    }

 

}

//按钮事件jsbutton2

-(void)suspendedOrcontinue:(UIButton *)sender{

    if ([jsbutton1.titleLabel.text isEqual:@"开始计时"]) {

        

    }

    else{

    if (!isClick2) {

        [jsbutton2 setTitle:@"继续" forState:UIControlStateNormal];

        isClick2=YES;

        [jsview addSubview:jsbutton2];

    }

    else{

        [jsbutton2 setTitle:@"暂停" forState:UIControlStateNormal];

        isClick2=NO;

        [jsview addSubview:jsbutton2];

    }

    }

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

原文地址:https://www.cnblogs.com/OIMM/p/4710448.html