萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> 蘋果iOS實例編程入門教程

蘋果iOS實例編程入門教程

想給你的iPhone制作一場雪景嗎?今天小編為大家整理了蘋果iOS實例編程入門教程,一起來看看吧!

綱要:

- 在程序顯示前運行代碼 -

- UIImageView 的運用 -

- 關於iPhone的“Utility Application” 運用 -

- onTimer 代碼運用 -

- onAnimation 代碼運用 -

首先運行以安裝好的 xCode

選擇: File->New Project.

從 "New Project" 窗口

選擇 : iPhone OS ->Applications-> Utility Application

命名 : 我這裡命名為 “SnowFall”

(1)  在xCode打開 MainView.h 文件,加入下面代碼

#import

@interface MainViewController : UIViewController {

UIImage* flakeImage;

}

@property (nonatomic, retain) UIImage* flakeImage;

- (void)onTimer;

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

@end

(2)  在xCode打開 MainView.m 文件,加入下面代碼

#import "MainViewController.h"

#import "MainView.h"

@implementation MainViewController

@synthesize flakeImage;

- (void)viewDidLoad {

[super viewDidLoad];

// 把背景顏色設置為冷色

self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];

// 把雪花圖片文件不停導出

flakeImage = [UIImage imageNamed:@"flake.png"];

// 在onTimer設置開始時間每秒二十次

[NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

// Timer event is called whenever the timer fires

- (void)onTimer

{

//建立一個雪花圖片 flake image

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

//use the random() function to randomize up our flake attributes

int startX = round(random() % 320);

int endX = round(random() % 320);

double scale = 1 / round(random() % 100) + 1.0;

double speed = 1 / round(random() % 100) + 1.0;

// set the flake start position

flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);

flakeView.alpha = 0.25;

// put the flake in our main view

[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];

// set up how fast the flake will fall

[UIView setAnimationDuration:5 * speed];

// set the postion where flake will move to

flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);

// set a stop callback so we can cleanup the flake when it reaches the

// end of its animation

[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];

[UIView setAnimationDelegate:self];

[UIView commitAnimations];

}

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

flakeView = nil;

[flakeView removeFromSuperview];

// open the debug log and you will see that all flakes have a retain count

// of 1 at this point so we know the release below will keep our memory

// usage in check

NSLog([NSString stringWithFormat:@"[flakeView retainCount] = %d", [flakeView retainCount]]);

[flakeView release];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview

// Release anything that's not essential, such as cached data

}

- (void)dealloc {

[flakeImage release];

[super dealloc];

}

@end

(3) 導入下面圖片文件

下載下面圖片,放入 SnowFall 文件夾內並命名為下面名稱

flake.png

在xCode下右鍵點擊 SnowFall->Add->Existing Files; 在 SnowFall 文件夾內,選擇下載好的圖片,按 Add,最後在 xCode 選擇 Build->Build and Go; Save All.

以上就是小編為大家整理的蘋果iOS實例編程入門教程,希望對大家有所幫助。

copyright © 萬盛學電腦網 all rights reserved