倒计时demo
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong,nonatomic) UILabel *titleLable;
@property (strong,nonatomic) UILabel *showTimeLable;
@property (strong,nonatomic) UIView *bottomView;
@property (strong,nonatomic) NSString *setTime;
@property (strong,nonatomic) NSArray *timeArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(88, 40, 200, 140)];
self.titleLable.backgroundColor = [UIColor clearColor];
self.titleLable.numberOfLines = 0;
self.titleLable.textColor = [UIColor purpleColor];
self.titleLable.textAlignment = NSTextAlignmentCenter;
self.titleLable.font = [UIFont systemFontOfSize:30];
[self.view addSubview:self.titleLable];
self.showTimeLable = [[UILabel alloc] initWithFrame:CGRectMake(88, 190, 200, 200)];
self.showTimeLable.backgroundColor = [UIColor grayColor];
self.showTimeLable.textAlignment = NSTextAlignmentCenter;
self.showTimeLable.font = [UIFont systemFontOfSize:30];
self.showTimeLable.numberOfLines = 0;
self.showTimeLable.textColor = [UIColor yellowColor];
self.showTimeLable.layer.cornerRadius = self.showTimeLable.frame.size.width / 2;
self.showTimeLable.clipsToBounds = YES;
[self.view addSubview:self.showTimeLable];
self.setTime = @"2016-6-1";
self.timeArr = [self.setTime componentsSeparatedByString:@"-"];
[self refreshTime];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];
}
-(void)refreshTime
{
NSCalendar *laterCalender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *latercomponents = [[NSDateComponents alloc] init];
latercomponents.year = [self.timeArr[0] integerValue];
latercomponents.month = [self.timeArr[1] integerValue];
latercomponents.day = [self.timeArr [2] integerValue];
NSDate *laterDate = [laterCalender dateFromComponents:latercomponents];
NSDateComponents *betweenDate = [laterCalender components:NSCalendarUnitSecond fromDate:[NSDate date] toDate:laterDate options:0];
if (betweenDate.second < 0) {
self.showTimeLable.text = [self DayHourMunintSecond:-betweenDate.second];
self.titleLable.text = [NSString stringWithFormat:@"距离\n%@\n已经过时",self.setTime];
}
else{
self.showTimeLable.text = [self DayHourMunintSecond:betweenDate.second];
self.titleLable.text = [NSString stringWithFormat:@"距离\n%@\n结课还有",self.setTime];
}
}
-(NSString *)DayHourMunintSecond:(NSInteger)time
{
NSString *timeString;
timeString = [NSString stringWithFormat:@"%ld秒",time % 60];
time /= 60;
if (time > 0) {
timeString = [NSString stringWithFormat:@"%ld分\n%@",time % 60,timeString];
}
time /= 60;
if (time > 0) {
timeString = [NSString stringWithFormat:@"%ld时\n%@",time % 24,timeString];
}
time /= 24;
if (time > 0) {
timeString = [NSString stringWithFormat:@"%ld天\n%@",time,timeString];
}
return timeString;
}
最新文章
- *HDU1848 博弈
- 按要求编写Java应用程序。 (1)创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。 编写两个构造方法:一个没有形参,在方法中将车牌号设置“XX1234”,速 度设置为100,载重量设置为100;另一个能为对象的所有属性赋值; (2)创建主类: 在主类中创建两个机动车对象。 创建第
- Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. An example is the root-to-leaf path1->;2->;3which represents the number123. Find the total sum of a
- 短地址TinyURL的API使用
- Generic Data Access Layer泛型的数据访问层
- 兼容IE,Firefox,Opera等浏览器的添加到收藏夹js代码实现
- printf那点事
- vijos1080题解
- RabbitMQ 消息队列 入门 第一章
- 那些年我们一起踩过的Dubbo";坑";
- 洛谷P1144最短路计数题解
- [转] node.js下mongoose简单操作实例
- 对浏览器攻击:MS10-002
- LAMP搭建个人网站
- 贪吃蛇GamePanel Java实现(二)
- Linux 中 awk命令应用
- 认识和使用Task
- mysql主备部署[高可用]
- SUSE Linux Enterprise Server设置IP地址、网关、DNS(转载)
- PHP与Imagemagick