先放效果图:

这是我第一次尝试在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]];

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/white-space: pre;/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

v2版把倒计时计算抽成了一个类,提高程序的复用性。

用法如下:(使用了Delegate思想和静态实例化思想)

ZZYDateCounter*dc=[ZZYDateCounter CounterWithTargetYear:2015mounth:1day:4hour:7andSentence:@"距离考研还有%d天!"];
    dc.textDelegate=_KYLabel;
    [dc say];

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/white-space: pre;/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

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

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/white-space: pre;/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

 

在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];
 
}

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/white-space: pre;/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

最后,别忘了

设置为开机自启:)

 

 

心得体会:

Mac下开发果然特别舒服,和IOS一脉相承。了解了NSImageView和NSTextView和NSWindow 等控件。

计划以后继续深入了解如:

改变部分字体颜色和文字大小、

改进NSWindow的外观、

使用快捷键进出全屏

等。

还有,感觉排版很渣。。。MarsEdit还不是很熟。。继续努力!

第一个Mac程序——倒计时v1&v2的更多相关文章

  1. 第一个 mac 程序 Create-JSON-Model

    第一个 mac 程序 Create-JSON-Model 效果图 数据 {"ID":null,"name":"Doe","firs ...

  2. 从安装Mac OS X虚拟机到第一个IOS程序

    对于纯粹地抄这种行为是比较抵触的,别人已经写得挺好的东西没必要又去写一遍,但如果不写经验来看下次再做时自己又要重复百度筛选一遍,所以还是要记一记. 之前要获取IOS静态库的版本,但一直以来没有Mac没 ...

  3. Mac下将C程序创建为动态链接库再由另一个C程序调用

    写C的时候需要调用之前的一个C程序,想用动态链接库的方式.Mac下的动态链接库是dylib,与Linux下的.os或Windows下的.dll不同.由于之前没有接触过,所以翻了大量的博客,然而在编译过 ...

  4. 第一个Spark程序

    1.Java下Spark开发环境搭建(from http://www.cnblogs.com/eczhou/p/5216918.html) 1.1.jdk安装 安装oracle下的jdk,我安装的是j ...

  5. 第一个go程序和基本语法

    目录 第一个go程序和基本语法 一. 第一个go程序 二. 基础语法 1. 命名 2. 变量 3 常量与枚举 4. 数据类型 5. fmt包的使用 6. 类型别名 7. 类型转换 8. 运算符 第一个 ...

  6. 如何发布一个Mac应用并使其成为全球付费榜第一

    Readdle公司如何发布第一个 Mac App,并使之成为Mac App Store 全球付费排名第一的 Easy注:自从发布了<程序员如何优雅的挣零花钱?>后,就不断有同学询问怎么做A ...

  7. 我曾经的第一个OC程序

    一. OC简介 C语言的基础上,增加了一层最小的面向对象语法 完全兼容C语言 可以在OC代码中混入C语言代码,甚至是C++代码 可以使用OC开发Mac OS X平台和iOS平台的应用程序 二. OC语 ...

  8. 【C语言】03-第一个C程序代码分析

    前面我们已经创建了一个C程序,接下来分析一下里面的代码. 项目结构如下: 一.代码分析 打开项目中的main.c文件(C程序的源文件拓展名为.c),可以发现它是第一个C程序中的唯一一个源文件,代码如下 ...

  9. 使用Playground编写第一个Swift程序

    从控制台输出“HelloWorld”是我学习C语言的第一步,也是我人生中非常重要的一步.多年后的今天,我仍希望以HelloWorld作为第一步,与大家共同开启一个神奇.瑰丽的世界——Swift编程. ...

随机推荐

  1. 课时46:魔法方法:描述符(property的原理)

    目录: 一.描述符(property的原理) 二.课时46课后习题及答案 ********************************** 一.描述符(property的原理) ********* ...

  2. 一个画ROC曲线的封装包

    Draw_ROC_Curves This is a python file which is used for drawing ROC curves -f : assign file name -t ...

  3. 博客挪窝了 http://my.oschina.net/jrrx/blog

    1. 博客园很不错: 2. 由于各种原因,挪到了 http://my.oschina.net/jrrx/blog

  4. Python编码报错

    Python默认编码错误SyntaxError: Non-ASCII character '\xe5'之解决方法在编写Python时,当使用中文输出或注释时运行脚本,会提示错误信息:SyntaxErr ...

  5. BZOJ[NOI2004]郁闷的出纳员 | Splay板子题

    题目: 洛谷也能评测....还有我wa了10多次的记录233 题解: 不要想得太复杂,搞一个全局变量记录一下工资的改变量Delta,这样可以等询问的时候就输出val+Delta,然后插入的时候插入x- ...

  6. POJ 1523 SPF 求割点的好(板子)题!

    题意: 给个无向图,问有多少个割点,对于每个割点求删除这个点之后会产生多少新的点双联通分量 题还是很果的 怎么求割点请参考tarjan无向图 关于能产生几个新的双联通分量,对于每个节点u来说,我们判断 ...

  7. 2017 多校4 Matching In Multiplication(二分图)

    Matching In Multiplication 题解: 首先如果一个点的度数为1,那么它的匹配方案是固定的,继而我们可以去掉这一对点.通过拓扑我们可以不断去掉所有度数为1的点. 那么剩下的图中左 ...

  8. webapp的favicon应该怎样组织代码

    处理过那么多index 页面了那么关于特别针对于此页的favicon是时候详细的总结一下了 它是网站的头像,它出现在浏览器的收藏夹中(标题的旁边) ,浏览器标签页的左上角,微信公众号的logo,保存网 ...

  9. ios手机弹出层上表单的操作,收起键盘焦点错乱的问题

    今天遇到了ios手机下 弹出层上form表单 当收起键盘后,焦点错乱,无法再操作的问题 解决办法 function device() { const u = navigator.userAgent; ...

  10. [C++对象模型][8]多重继承与虚函数表

    转载: [C++对象模型][8]多重继承与虚函数表 一 多重继承 1) 代码: Code #include <iostream> using namespace std; class B1 ...