第一个Mac程序——倒计时v1&v2
先放效果图:

这是我第一次尝试在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的更多相关文章
- 第一个 mac 程序 Create-JSON-Model
第一个 mac 程序 Create-JSON-Model 效果图 数据 {"ID":null,"name":"Doe","firs ...
- 从安装Mac OS X虚拟机到第一个IOS程序
对于纯粹地抄这种行为是比较抵触的,别人已经写得挺好的东西没必要又去写一遍,但如果不写经验来看下次再做时自己又要重复百度筛选一遍,所以还是要记一记. 之前要获取IOS静态库的版本,但一直以来没有Mac没 ...
- Mac下将C程序创建为动态链接库再由另一个C程序调用
写C的时候需要调用之前的一个C程序,想用动态链接库的方式.Mac下的动态链接库是dylib,与Linux下的.os或Windows下的.dll不同.由于之前没有接触过,所以翻了大量的博客,然而在编译过 ...
- 第一个Spark程序
1.Java下Spark开发环境搭建(from http://www.cnblogs.com/eczhou/p/5216918.html) 1.1.jdk安装 安装oracle下的jdk,我安装的是j ...
- 第一个go程序和基本语法
目录 第一个go程序和基本语法 一. 第一个go程序 二. 基础语法 1. 命名 2. 变量 3 常量与枚举 4. 数据类型 5. fmt包的使用 6. 类型别名 7. 类型转换 8. 运算符 第一个 ...
- 如何发布一个Mac应用并使其成为全球付费榜第一
Readdle公司如何发布第一个 Mac App,并使之成为Mac App Store 全球付费排名第一的 Easy注:自从发布了<程序员如何优雅的挣零花钱?>后,就不断有同学询问怎么做A ...
- 我曾经的第一个OC程序
一. OC简介 C语言的基础上,增加了一层最小的面向对象语法 完全兼容C语言 可以在OC代码中混入C语言代码,甚至是C++代码 可以使用OC开发Mac OS X平台和iOS平台的应用程序 二. OC语 ...
- 【C语言】03-第一个C程序代码分析
前面我们已经创建了一个C程序,接下来分析一下里面的代码. 项目结构如下: 一.代码分析 打开项目中的main.c文件(C程序的源文件拓展名为.c),可以发现它是第一个C程序中的唯一一个源文件,代码如下 ...
- 使用Playground编写第一个Swift程序
从控制台输出“HelloWorld”是我学习C语言的第一步,也是我人生中非常重要的一步.多年后的今天,我仍希望以HelloWorld作为第一步,与大家共同开启一个神奇.瑰丽的世界——Swift编程. ...
随机推荐
- CentOS修改IP地址
一.CentOS 修改IP地址修改对应网卡的IP地址的配置文件 # vi /etc/sysconfig/network-scripts/ifcfg-eth0 电信 # vi /etc/syscon ...
- 【JAVA进阶】——myEclipse连接mysql启动数据库服务
背景: DRP项目要求使用Oracle数据库,但目前由于种种原因,暂时还装不了Oracle.但也不能闲着啊,就拿mysql来试试.安装完mysql以后,使用myEclipse连接数据库,就一直报错,报 ...
- ASP.NET——实现两个下拉框动态联动
引入: 在网页中,我们经常会遇到下图中的情况.首先在下拉框中选择所在的省,选择之后,第二个下拉框会自动加载出该省中的市.这样设计极大的方便了用户的查找.那这是如何实现的呢? 1.建立数据库 " ...
- hexo 添加标签
--- title: title #文章標題 date: 2016-06-01 23:47:44 #文章生成時間 categories: "Hexo教程" #文章分類目錄 可以省略 ...
- Apache实现一个ip(如:127.0.0.1)和多个域名(虚拟主机)绑定
今天在学习PHP时,有这样的一个需求:一个ip(如:127.0.0.1)和多个域名(虚拟主机)绑定,以下是我的解决方案:对Apache进行相关的配置 解决方案一:通过端口来区分不同的虚拟主机 ①按照绑 ...
- MYSQL 简单的建库操作代码
一.查询所有数据库 代码:show databases; 成功后如下图: 二.建立一个数据库 代码:create database test3: 成功后如下图: 三.连接数据库 代码:use test ...
- 洛谷 P3747 [六省联考2017]相逢是问候 解题报告
P3747 [六省联考2017]相逢是问候 题目描述 \(\text {Informatik verbindet dich und mich.}\) 信息将你我连结. \(B\) 君希望以维护一个长度 ...
- 将windows文本格式转换为UNIX格式
将windows文本格式转换为UNIX格式 1.使用sed命令来进行转换,如下: sed -e ’s,^M,,g’ textfile 其中^M的输入方法是Ctrl+V, Ctrl+M 对于批量文件的处 ...
- 3.6 Lucene基本检索+关键词高亮+分页
3.2节我们已经运行了一个Lucene实现检索的小程序,这一节我们将以这个小程序为例,讲一下Lucene检索的基本步骤,同时介绍关键词高亮显示和分页返回结果这两个有用的技巧. 一.Lucene检索的基 ...
- Topcoder SRM 600 div1题解
日常TC计划正式启动! Easy(250pts): 题目大意:给你一个集合,里面一堆数,初始数为0,给你一个目标数,你可以选择集合中若干个数进行OR操作来得到目标数.问至少删去多少个数,使得你永远无法 ...