第一个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编程. ...
随机推荐
- [类和对象]3 C++面向对象模型初探
? C++编译器如何完成面向对象理论到计算机程序的转化? [C++编译器是如何管理类.对象.类和对象之间的关系] 通过下面的代码,我们可以的得出:C++类对象中的成员变量和成员函数是分开存储的 成员变 ...
- android桌面悬浮窗实现
首先是一个小的悬浮窗显示的是当前使用了百分之多少的内存,点击一下小悬浮窗,就会弹出一个大的悬浮窗,可以一键加速.好,我们现在就来模拟实现一下类似的效果. ...
- HDU 3549 基础网络流EK算法 Flow Problem
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit ...
- CSS——(2)盒子模型与标准流
上篇博客<CSS--(1)基础>中简单介绍了CSS的概念和几种使用方法,现在主要是介绍其的核心内容. 盒子模型 为了理解盒子模型,我们可以先从生活中的盒子入手.盒子是用来放置物品的,内部除 ...
- 【bzoj1070】[SCOI2007]修车 最小费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6798411.html 题目描述 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的 ...
- CLion 的 Debug 模式是怎么回事
我对这个问题产生兴趣的起因是 Codeoforces Avito Challenges 2018 的 E 题,我想到了正解,但写得太慢,最后一刻才提交.有个地方写错,结果是 Runtime error ...
- POJ A Simple Problem with Integers | 线段树基础练习
#include<cstdio> #include<algorithm> #include<cstring> typedef long long ll; #defi ...
- UVA 11478(差分约束 + 二分)
题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点和一个整数的,把所有以v为终点的边的权值减去d, 把所有以v为起点的边的权值加上d 最后要让所有边的权的最小值非负且尽量大 代码 #i ...
- 2016华中农业大学预赛 B 数学
Problem B: Handing Out Candies Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 272 Solved: 20[Submit ...
- HDU 1867 A + B for you again ----KMP
题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...