先放效果图:
这是我第一次尝试在Mac平台下编程,之前学过几天IOS开发,一直在等MJ老师更新。闲下来不编程不舒服,给自己,也给老婆编了这个以提醒自己好好学习。v2版加入了各种倒计时。改进了界面。把倒计时计算抽成了一个类,提高程序的复用性。
-概览
首先放上xib界面图
-改界面名字
建项目的过程中一开始不小心取了个没有远见的名字“考研倒计时”,后来改成了倒计时。怎么改呢?我是这样改的:(比如程序的window,上面的menuitem类同)
顺手把Resize的钩钩去掉
把Full Screen改成Primary Window蛮有意思的。(试试把Resize勾上再全屏)
-拖入控件
这里我一开始只拖进去这5句话(NSTextField),双击控件先把文字改个样子做个效果。在右边顺便把字体改了。
后来如图又加入了一个NSImageView 以作背景
在Attribute Inspector中改成我事先找好的图,并把Scaling改成AxesIndependently 这样图片就可以适当的放缩了。
-代码&优化
一开始我的代码是这样的,后来又加了纪念日、软考等,代码是直接“拷贝”复用的。
#pragma mark - 设置 -
NSDateComponents *components=[[NSDateComponents alloc]init];//创建组件
#pragma mark 考研
[components setMonth:1];
[components setDay:4];
[components setYear:2015];
[components setHour:7];
NSDate *targetDate = [[NSCalendar currentCalendar] dateFromComponents:components];
NSTimeInterval targetInteval = [targetDate timeIntervalSinceNow];
// NSLog(@"%f",KYinteval);
NSLog(@"%d",(int)targetInteval/60/60/24);
[_KYLabel setStringValue:[NSString stringWithFormat:@"距离考研还有%d天!",(int)targetInteval/60/60/24]];
v2版把倒计时计算抽成了一个类,提高程序的复用性。
用法如下:(使用了Delegate思想和静态实例化思想)
ZZYDateCounter*dc=[ZZYDateCounter CounterWithTargetYear:2015mounth:1day:4hour:7andSentence:@"距离考研还有%d天!"];
dc.textDelegate=_KYLabel;
[dc say];
ZZYDateCounter源代码如下:(提供了使用NSDate初始化和直接使用年月日小时直接初始化的两个接口,为以后的扩展预留接口)
//
// ZZYDateCounter.h
// 考研倒计时
//
// Created by 张泽阳 on 3/14/14.
// Copyright (c) 2014 张泽阳. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ZZYDateCounter : NSObject
@property NSTextField* textDelegate;
-(void)say;
+(ZZYDateCounter*)CounterWithTargetDate:(NSDate*)targetDate andTargetSentence:(NSString*)targetSentence;
+(ZZYDateCounter*)CounterWithTargetYear:(int)y mounth:(int)m day:(int)d hour:(int)h andSentence:(NSString*)targetSentence;
@end
//
// ZZYDateCounter.m
// 考研倒计时
//
// Created by 张泽阳 on 3/14/14.
// Copyright (c) 2014 张泽阳. All rights reserved.
//
#import "ZZYDateCounter.h"
@implementation ZZYDateCounter{
NSDateComponents *components;
NSString* counterSentence;
NSTimeInterval targetInteval;
NSDate *targetDate ;
}
-(id)init{
if (self=[super init]) {
components=[[NSDateComponentsalloc]init];//创建组件
}
returnself;
}
-(id)initWithDate:(NSDate*)date andSentence:(NSString*)sentence{
if ((self=self.init)) {
// form http://blog.sina.com.cn/s/blog_8732f19301010fxd.html
// NSDate 相互转换
//
// NSDateComponents *comps = [[NSDateComponents alloc] init];
//
// [comps setDay:6];
//
// [comps setMonth:5];
//
// [comps setYear:2004];
//
// NSCalendar *gregorian = [[NSCalendar alloc]
//
// initWithCalendarIdentifier:NSGregorianCalendar];
//
// NSDate *date = [gregorian dateFromComponents:comps];
//
// [comps release];
//
// NSDateComponents *weekdayComponents =
//
// [gregorian components:NSWeekdayCalendarUnit fromDate:date];
//
// int weekday = [weekdayComponents weekday];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
components=[gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnitfromDate:date];
counterSentence=[NSString stringWithString:sentence];
} ;
returnself;
}
-(void)say{
targetDate = [[NSCalendarcurrentCalendar] dateFromComponents:components];
targetInteval = [targetDatetimeIntervalSinceNow];
if (targetInteval<0) {
targetInteval=-targetInteval;
}
[_textDelegatesetStringValue:[NSStringstringWithFormat:counterSentence,(int)targetInteval/60/60/24]];
}
-(id)initWithYear:(int)y mounth:(int)m day:(int)d hour:(int)hour andSentence:(NSString*)sentence{
if ((self=self.init)) {
[components setMonth:m];
[components setDay:d];
[components setYear:y];
[components setHour:hour];
counterSentence=[NSString stringWithString:sentence];
}
returnself;
}
+(ZZYDateCounter *)CounterWithTargetDate:(NSDate *)targetDate andTargetSentence:(NSString *)targetSentence{
ZZYDateCounter* counter = [[ZZYDateCounteralloc] init];
return [counter initWithDate:targetDate andSentence:targetSentence];
}
+(ZZYDateCounter*)CounterWithTargetYear:(int)y mounth:(int)m day:(int)d hour:(int)h andSentence:(NSString *)targetSentence{
ZZYDateCounter* counter = [[ZZYDateCounteralloc] init];
return [counter initWithYear:y mounth:m day:d hour:h andSentence:targetSentence];
}
@end
在AppDelegate中添加下面两个方法:
//关闭Window后退出程序
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
NSLog(@"ttt");
returnYES;
}
//当然少不了彩蛋了!
- (IBAction)onAboutClicked:(NSMenuItem *)sender {
NSAlert *alert = [NSAlertalertWithMessageText:@"关于这个软件"defaultButton:@"我也爱你" alternateButton:nilotherButton:nilinformativeTextWithFormat:@"为我家亲爱的编写,祝我们都能考上好研究生,一辈子在一起!\n永远爱我家文儿!"];
[alert beginSheetModalForWindow:_windowcompletionHandler:nil];
}
最后,别忘了
设置为开机自启:)
心得体会:
Mac下开发果然特别舒服,和IOS一脉相承。了解了NSImageView和NSTextView和NSWindow 等控件。
计划以后继续深入了解如:
改变部分字体颜色和文字大小、
改进NSWindow的外观、
使用快捷键进出全屏
等。
还有,感觉排版很渣。。。MarsEdit还不是很熟。。继续努力!