IPhone编程31天,第6天关于Utility Application的使用

昨天看了 http://blog.sina.com.cn/s/blog_5fae23350100dyg4.html上面的三十一天,Iphone app编程入门第六天,我做了一下,效果出来了,但是有些地方还不是很明白,列出来,等待以后来解决!
1.创建Iphone Application的时候一共有6项供选择,Navigation-based Application,OpenGL ES APplication,Tab Bar Application,Utility Application,View-Based Application,Window-Based Application各代表什么东西,创建出来的东西有什么区别?每一项对应创建什么样的项目?
2.@property (nonatomic, copy) NSDate *startDate;这个代表什么意思?怎么用?
3.在MainView.h中调用@synthesize startDate;代表的是什么意思?为什么要这样做?
4.- (void)alertView:(UIAlertView *)alertView didDismissWithButtonInde x:(NSInteger)buttonIndex这个函数代表的是什么?入口和出口个是什么?
5.double noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000;这一句表示的是什么意思?
下面我也大致的把这个制作过程自己再叙述一遍,做一个熟练:
1.File-New Project-Utility Application.随便起个名字(当然不能和关键字冲突了),我起的项目的名字是IP_UtilityGame。
2.在MainView.h中填写如下代码:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>//需要添加的,引入Foundation
@interface MainView : UIView {
  NSDate *startDate;//需要添加的,定义一个NSDate类型的变量
  IBOutlet UIImageView *stopLight;//需要添加的,定义一个UIImageView类型,控制红绿灯的显示
}
@property (nonatomic, copy) NSDate *startDate;//需要添加的,但是还不知道为什么???
-(IBAction)gasPedalPressed;//需要添加的,点击Button触发事件
@end
6. 在MainView.m中添加如下代码:
首先引入@synthesize startDate;//????
然后定义一个全局变量,用来表示是否显示为绿灯 int greelightOn= 0;当然也可以用Bool类型了。
然后添加-
-(void)awakeFromNib//系统运行的时候给出一个提示
{
  UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"当绿灯亮时以最快的速度按下脚踏板!" delegate:self cancelButtonTitle:@"游戏开始" otherButtonTitles:nil];
  [alert show];
}
继续添加
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonInde x:(NSInteger)buttonIndex//这个函数不清楚是做什么用的。有待研究。呵呵
{
  stopLight.image = [UIImage imageNamed:@"yellowLightSmall.png"];//设置为黄灯
  greelightOn = 0;//标示位设置
  [NSTimer scheduledTimerWithTimeIn terval:(3.0) target:self selector:@selector(onYellowLightTimer) userInfo:nil repeats:NO];//定义一个Timer用来计时黄灯的显示时间
}
接下来添加
-(void)onYellowLightTimer//黄灯亮3秒后触发的事件
{
  stopLight.image = [UIImage imageNamed:@"redLightSmall.png"];//设置位红灯
  int delay = ((int)(random()%7+1));//随机得到一个时间
  [NSTimer scheduledTimerWithTimeIn terval:(3.0 + delay) target:self selector:@selector(onRedLightTimer) userInfo:nil repeats:NO]; 
}
继续添加
-(void)onRedLightTimer
{
  stopLight.image = [UIImage imageNamed:@"greenLightSmall.png"];//设置黄灯
  greelightOn = 1;//证明绿灯已经亮起
  self.startDate = [NSDate date];//开始计时
}
接下来添加Button触发事件
-(IBAction)gasPedalPressed
{
  double noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000;//得到响应时间
  NSString *seactionTime = [[NSString alloc] initWithFormat:@"你的相应速度时%1.0f毫秒!",noSeconds];
  if(greelightOn == 0)//如果标示位为0证明不是绿灯,给出提示
    seactionTime = @"请等待绿灯亮了再点!";
  UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"提示" message:seactionTime delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
  [alert show];
}
7. 添加所用的图片,图片可以在上面的Blod中下载到。呵呵(偷懒了)
在项目文件上点击右键-Add-Existing Files,选中要添加的图片文件添加进去就OK了。
8. 选择Resources-MainView.xib,用IB打开,打开MainView。拖入一个UIImageView,选择Road作为背景。layout选择Send BackWard。
再拖入一个UIButton,type选择Custom,Image选择一个。
在拖入一个UIImageView,选择一个灯
接下来就是关联了。
在Main View里面点右键,会出来一个黑色框把
Outlets连接到UIImageView(路灯上)
Receives Action连接到UIButton上
8. 点击Build and go运行看结果。
 

你可能感兴趣的:(编程,移动开发,application,iPhone,utility)