使用NSTimer实现倒计时,Iphone幻灯片效果+背景音乐,

1.使用NSTimer实现倒计时

今天在CocoaChina上面看到有人在问倒计时怎么做,记得以前在看Iphone31天的时候做过一个,今天翻出来运行不了了,原因是我的IphoneSDK升级到3.1了,以前使用的是2.2.1,在2.2.1里面是可以使用NSCalendarDate的,但是在3.1里面不能够使用,怎么办,只好用NSTimer了,最后还是给实现了。代码也比较简单,开始运行viewDidLoad的时候加载 [NSTimerscheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(timerFireMethod:) userInfo:nilrepeats:YES];//使用timer定时,每秒触发一次
,然后就是写selector了。
 
-(void)timerFireMethod:(NSTimer*)theTimer
{
 //NSDateFormatter *dateformatter =[[[NSDateFormatter alloc]init]autorelease];//定义NSDateFormatter用来显示格式
 //[dateformatter setDateFormat:@"yyyy MM dd hh mmss"];//设定格式
 NSCalendar *cal = [NSCalendarcurrentCalendar];//定义一个NSCalendar对象
 NSDateComponents *shibo = [[NSDateComponentsalloc] init];//初始化目标时间(好像是世博会的日期)
 [shibo setYear:2010];
 [shibo setMonth:5];
 [shibo setDay:1];
 [shibo setHour:8];
 [shibo setMinute:0];
 [shibo setSecond:0];
 
 NSDate *todate = [caldateFromComponents:shibo];//把目标时间装载入date
 [shibo release];
// NSString *ssss = [dateformatterstringFromDate:dd];
// NSLog([NSString stringWithFormat:@"shiboshi:%@",ssss]);
 
 NSDate *today = [NSDate date];//得到当前时间
// NSString *sss = [dateformatterstringFromDate:today];
// NSLog([NSString stringWithFormat:@"xianzaishi:%@",sss]);
 //用来得到具体的时差
 unsigned int unitFlags = NSYearCalendarUnit |NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *d = [cal components:unitFlagsfromDate:today toDate:todate options:0];
 lab.text = [NSStringstringWithFormat:@"%d年%d月%d日%d时%d分%d秒",[d year],[d month], [d day],[d hour], [d minute], [d second]];
}
这样就实现了倒计时的功能。

2.Iphone幻灯片效果+背景音乐
今天弄了几张好看的图片,我就摸索着实现了图片的幻灯片效果,这个以前也实现过了,也算是温故知新吧,另外就是使用SoundEngine类实现背景音乐的播放。SoundEngine类可以从[url=read.php?tid-1215.html]http://www.cocoachina.com/bbs/read.php?tid-1215.html[/url]下载到。

代码很简单贴出来,以备不时只需:
-(void)viewDidLoad
{
 
array = [[NSMutableArray alloc] init];
 
int i = 1;
 
for(i;i<=30;i++)
 
{
 
 [array addObject:[UIImageimageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
 
}
 
pictures.animationImages = array;
 
pictures.animationDuration = 300;//时间间隔
 
pictures.animationRepeatCount = 0;//循环播放
 
[pictures startAnimating];//开始播放

//播放背景音乐,利用SoundEngine类进行播放
 
SoundEngine_SetListenerPosition(0.0, 0.0,1.0);
 
SoundEngine_Initialize(44100);
 
SoundEngine_LoadBackgroundMusicTrack([[[NSBundlemainBundle] pathForResource:@"win" ofType:@"caf"] UTF8String],true, true);
 
SoundEngine_StartBackgroundMusic();
}


用这种方法播放好像挺占用资源的,比较卡,以后再研究研究其它的方法。

3.NSTimer的用法

iPhone为我们提供了一个很强大得时间定时器 NSTimer,它可以完成任何定时功能:
我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理delegate和事件处理方法@selector();

就可以用
1 +(NSTimer *)scheduledTimerWithTimeIn
2 terval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; 
[/pre]来初始化一个 时间定时器
下面我写了一个很简单得例子:
-(void)initTimer
 {
//时间间隔4 NSTimeInterval timeInterval =1.0;
 //定时器6 NSTimer   showTimer =[NSTimer scheduledTimerWithTimeInterval:maxShowTime 
target:self
selector:@selector(handleMaxShowTimer:)
userInfo:nil
 repeats:NO];
}
//触发事件13 -(void)handleMaxShowTimer:(NSTimer *)theTimer
 {
NSDateFormatter dateFormator =[[NSDateFormatter alloc] init];
 dateFormator.dateFormat =@"yyyy-MM-dd  HH:mm:ss";
 NSString *date =[dateformater stringFromDate:[NSDate date]];
 if([date isEqualToString:@"2010-11-09 23:59:59"])
 {
 UIAlertView *alert =[[UIAlertView alloc] initWithTitle:TITLE_NAME
message:@"现在马上就有新的一天了!"22 delegate:self
 cancelButtonTitle:nil
 otherButtonTitles:CONFIRM_TITLE, nil];
 [alert show];
[alert release];
}
[data release];
 [dateFormator release];
 }

posted @ 2013-01-07 16:18  Kristen  阅读(372)  评论(0编辑  收藏  举报