苹果iOS实例编程入门教程.docx
《苹果iOS实例编程入门教程.docx》由会员分享,可在线阅读,更多相关《苹果iOS实例编程入门教程.docx(5页珍藏版)》请在冰豆网上搜索。
苹果iOS实例编程入门教程
苹果iOS实例编程入门教程
想给你的iPhone制作一场雪景吗?
今天小编为大家整理了苹果iOS实例编程入门教程,一起来看看吧!
纲要:
-在程序显示前运行代码-
-UIImageView的运用-
-关于iPhone的“UtilityApplication”运用-
-onTimer代码运用-
-onAnimation代码运用-
首先运行以安装好的xCode
选择:
File->NewProject.
从"NewProject"窗口
选择:
iPhoneOS->Applications->UtilityApplication
命名:
我这里命名为“SnowFall”
(1) 在xCode打开MainView.h文件,加入下面代码
#import
@interfaceMainViewController:
UIViewController{
UIImage*flakeImage;
}
@property(nonatomic,retain)UIImage*flakeImage;
-(void)onTimer;
-(void)onAnimationComplete:
(NSString*)animationIDfinished:
(NSNumber*)finishedcontext:
(void*)context;
@end
(2) 在xCode打开MainView.m文件,加入下面代码
#import"MainViewController.h"
#import"MainView.h"
@implementationMainViewController
@synthesizeflakeImage;
-(void)viewDidLoad{
[superviewDidLoad];
//把背景颜色设置为冷色
self.view.backgroundColor=[UIColorcolorWithRed:
0.5green:
0.5blue:
1.0alpha:
1.0];
//把雪花图片文件不停导出
flakeImage=[UIImageimageNamed:
@"flake.png"];
//在onTimer设置开始时间每秒二十次
[NSTimerscheduledTimerWithTimeInterval:
(0.05)target:
selfselector:
@selector(onTimer)userInfo:
nilrepeats:
YES];
}
//Timereventiscalledwheneverthetimerfires
-(void)onTimer
{
//建立一个雪花图片flakeimage
UIImageView*flakeView=[[UIImageViewalloc]initWithImage:
flakeImage];
//usetherandom()functiontorandomizeupourflakeattributes
intstartX=round(random()%320);
intendX=round(random()%320);
doublescale=1/round(random()%100)+1.0;
doublespeed=1/round(random()%100)+1.0;
//settheflakestartposition
flakeView.frame=CGRectMake(startX,-100.0,25.0*scale,25.0*scale);
flakeView.alpha=0.25;
//puttheflakeinourmainview
[self.viewaddSubview:
flakeView];
[UIViewbeginAnimations:
nilcontext:
flakeView];
//setuphowfasttheflakewillfall
[UIViewsetAnimationDuration:
5*speed];
//setthepostionwhereflakewillmoveto
flakeView.frame=CGRectMake(endX,500.0,25.0*scale,25.0*scale);
//setastopcallbacksowecancleanuptheflakewhenitreachesthe
//endofitsanimation
[UIViewsetAnimationDidStopSelector:
@selector(onAnimationComplete:
finished:
context:
)];
[UIViewsetAnimationDelegate:
self];
[UIViewcommitAnimations];
}
-(void)onAnimationComplete:
(NSString*)animationIDfinished:
(NSNumber*)finishedcontext:
(void*)context{
flakeView=nil;
[flakeViewremoveFromSuperview];
//openthedebuglogandyouwillseethatallflakeshavearetaincount
//of1atthispointsoweknowthereleasebelowwillkeepourmemory
//usageincheck
NSLog([NSStringstringWithFormat:
@"[flakeViewretainCount]=%d",[flakeViewretainCount]]);
[flakeViewrelease];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation{
//ReturnYESforsupportedorientations
return(interfaceOrientation==UIInterfaceOrientationPortrait);
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];//Releasestheviewifitdoesn’thaveasuperview
//Releaseanythingthat’snotessential,suchascacheddata
}
-(void)dealloc{
[flakeImagerelease];
[superdealloc];
}
@end
(3)导入下面图片文件
下载下面图片,放入SnowFall文件夹内并命名为下面名称
flake.png
在xCode下右键点击SnowFall->Add->ExistingFiles;在SnowFall文件夹内,选择下载好的图片,按Add,最后在xCode选择Build->BuildandGo;SaveAll.
以上就是小编为大家整理的苹果iOS实例编程入门教程,希望对大家有所帮助。