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 = [[logoviewconalloc] initWithNibName:@"logoviewcon"bundle:nil];
self.logo = lo;
[lo release];
[windowaddSubview:self.logo.view];
//初始化timmer
NSTimer*timer = [NSTimerscheduledTimerWithTimeInterval: 1.5target: selfselector: @selector(logo:) userInfo: nilrepeats: YES];
注意,初始化的代码中有这么一段:@selector(logo:),其中的方法就是当这个1.5秒时间过去之后自动调用的方法。
-(void) logo:(NSTimer*)timer{
[logo.view removeFromSuperview];
[timer invalidate];//这句代码用来终止timmer,否则,每过1.5秒,就会执行该方法一次,我们是要在开始的时候执行一次就够了。
}
iphone开发笔记2相关推荐
- iphone开发笔记和技巧总结
在iphone程序中实现截屏的一种方法: //导入头文件 #importQuartzCore/QuartzCore.h //将整个self.view大小的图层形式创建一张图片imageUIGrap ...
- ios学习--iphone开发笔记和技巧总结(原址持续更新)
ios学习--iphone开发笔记和技巧总结(原址持续更新) 分类: ios Object-C2012-04-18 10:16 2716人阅读 评论(1) 收藏 举报 uiviewiphonelist ...
- iphone 开发笔记
iphone 开发笔记 退回输入键盘 - (BOOL)textFieldShouldReturn:(id)textField{ [textField resignFirstResponde ...
- [每日100问][2011-10-11]iphone开发笔记,今天你肿了么
[url=http://www.buildapp.net/iphone/show.asp?id=24500]为啥gamecenter在沙箱里好使,在正式环境中分数和成就都提交不了呢????[/url] ...
- [每日100问][2011-9-06]iphone开发笔记,今天你肿了么
[url=http://www.buildapp.net/iphone/show.asp?id=5700]怎么让view保持不动,实现层次布局[/url] [url=http://www.builda ...
- [每日100问][2011-9-30]iphone开发笔记,今天你肿了么
[url=http://www.buildapp.net/iphone/show.asp?id=18700]有apple打款的时候正巧要更换银行信息的么?[/url] [url=http://www. ...
- [每日100问][2011-9-08]iphone开发笔记,今天你肿了么
[url=http://www.buildapp.net/iphone/show.asp?id=6700]请问,我想做iphone的真机调试,我的同一程序有办法对应多个固件版本吗?[/url] [ur ...
- [每日100问][2011-10-09]iphone开发笔记,今天你肿了么 编辑
[url=http://www.buildapp.net/iphone/show.asp?id=23300]<font color=#0000FF>中秋不只赏月趣! 在香港和法國生活类获选 ...
- [每日100问][2011-10-07]iphone开发笔记,今天你肿了么
[url=http://www.buildapp.net/iphone/show.asp?id=22500]<font color=#008000>Re:ipad五子棋中国风1.2新版上线 ...
最新文章
- 浅析精准网络推广的基础内容
- python要自学多长时间-怎么自学python,大概要多久?
- 爱是相互的,这样才是平衡
- POJ 1932 XYZZY (差分约束+传递闭包)
- Unity Package Manager Error的解决方案
- lettuce配置_skywalking与lettuce哨兵模式
- Python取出SQL表单中的字段名
- Struts2报错异常Method setUser failed for object com.mikey.action.ConverterAction@dd34285
- 帛书《要》篇“夫子老而好易”章
- 杰控组态自定义串口通讯的经验
- 深入理解GBDT回归算法
- 14.STC15W408AS单片机IIC驱动OLED
- user declined directory sharing Creating xxxx
- IndentationError: unindent does not match any outer indentation level 错误解决
- Java实现坦克大战小游戏(源码+注释)
- js原生css修改,原生js获取、添加、修改_非行间css样式
- 数字图像处理:线性和非线性滤波的平滑空间滤波器(Smoothing Spatial Filters)
- Zend studio 调整优化
- Ubuntu 16.04安装32bit支持
- win10周期性卡顿解决