先放效果图:

这是我第一次尝试在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. 网络--NAT技术

    一.概述 1.简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task For ...

  2. 波动数列 神奇的dp

    问题描述 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a或者减 ...

  3. 基于linux操作系统安装、使用memcached详解

    1.memcached的应用背景及作用 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态. ...

  4. c# 对XML 解析 和 序列化

    /// <summary> /// 解析XML 数据 /// </summary> /// <param name="re"></para ...

  5. BZOJ 1731: [Usaco2005 dec]Layout 排队布局

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  6. 《c程序设计语言》读书笔记-递归实现快速排序算法

    #include <stdio.h> void swap(int v[],int i,int j) { int temp; temp = v[i]; v[i] = v[j]; v[j] = ...

  7. svn merge详解

    svn merge详解 [OK] http://blog.163.com/lgh_2002/blog/static/4401752620106202710487/ Subversion的分支通常用于在 ...

  8. Mysql 乱码配置

    转自: http://www.ha97.com/5359.html 一.登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下: +--------- ...

  9. Java Web 路径问题

    可能在做文件上传或者 图片加载,资源加载 时候用到文件相对服务器地址 System.out.println(request.getRemoteUser()); //客户端用户System.out.pr ...

  10. linux--lsof

    lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控制协议 ...