iphone开发笔记2

1.截取屏幕图片

//创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)
UIGraphicsBeginImageContext(CGSizeMake(200,400));

//renderInContext 呈现接受者及其子范围到指定的上下文

[self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];
  
 //返回一个基于当前图形上下文的图片
 UIImage *aImage =UIGraphicsGetImageFromCurrentImageContext();
 
 //移除栈顶的基于当前位图的图形上下文
UIGraphicsEndImageContext();

//以png格式返回指定图片的数据

imageData = UIImagePNGRepresentation(aImage);


2.使用NSTimer与iphone的简单动画,实现飘雪效果
使用NSTimer与iphone的简单动画,实现飘雪效果,这理原理比较简单,就是定时生成一定的雪花图片,然后使用动画的方式向下漂落(我在其它论坛,看到使用path的方式实现的一个云漂来漂去的效果,实际也可以用那种方式实现,这实际就是前面说的动画效果的两种应用)。所以,我们可以在 viewDidLoad事件中,增加一个图片及定时器并启动,这里的pic请在头文件中定义。
-(void)viewDidLoad{
 [super viewDidLoad];
 self.pic = [UIImage imageNamed:@"snow.png"];//初始化图片
 //启动定时器,实现飘雪效果
 [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(ontime) userInfo:nil repeats:YES];
}
然后再实现定时器定时调用的ontime方法:
-(void)ontime{
 UIImageView *view = [[UIImageView alloc] initWithImage:pic];//声明一个UIImageView对象,用来添加图片
 view.alpha = 0.5;//设置该view的alpha为0.5,半透明的
 int x = round(random()%320);//随机得到该图片的x坐标
 int y = round(random()%320);//这个是该图片移动的最后坐标x轴的
 int s = round(random()%15)+10;//这个是定义雪花图片的大小
 int sp = 1/round(random()%100)+1;//这个是速度
 view.frame = CGRectMake(x, -50, s, s);//雪花开始的大小和位置
 [self.view addSubview:view];//添加该view
 [UIView beginAnimations:nil context:view];//开始动画
 [UIView setAnimationDuration:10*sp];//设定速度
 view.frame = CGRectMake(y, 500, s, s);//设定该雪花最后的消失坐标
 [UIView setAnimationDelegate:self];
 [UIView commitAnimations];
}


3.使用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]];
}
这样就实现了倒计时的功能。


4.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();
}


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


5.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];
 }


6.iphone开发之 - 启动页面设置
         不管是开发个人项目还是公司项目,大家通常都有一个需求,就是,在app启动的时候,指定一定的时间来显示自己的或者公司的logo,那么,我就将刚刚写好的启动加载页面设置代码贡献出来。(不对指出请留言,好的话也给我留个言吧,鼓励下我!呵呵)
        这里我需要用到NSTimer这个东西,相关的内容可以查看API,有比较详细的解释。
          新建一个项目,随便是什么项目,我建立的是“view based application”,然后,命名为“Logo”,然后确定。
          直接编辑“Resources"目录下的"LogoViewController.xib”。将背景颜色改称绿色,主要是为了当从logo页跳转过来的时候能有感觉到变化。
          然后新建一个NSTimer.

logoviewcon*lo = [[logoviewconallocinitWithNibName:@"logoviewcon"bundle:nil];
self.logo = lo;
[lo release];
[windowaddSubview:self.logo.view];
//初始化timmer
NSTimer*timer =  [NSTimerscheduledTimerWithTimeInterval1.5targetselfselector@selector(logo:) userInfonilrepeatsYES];
注意,初始化的代码有这么一段:@selector(logo:),其的方法就是当这个1.5秒时间过去之后自动调用的方法


-(void) logo:(NSTimer*)timer{
[logo.view removeFromSuperview];
[timer invalidate];//这句代码用来终止timmer,否则,每过1.5秒,就会执行该方法一次,我们是要在开始的时候执行一次就够了。
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值