2011-11-29 51 views
1

好,我想沒有OpenGL或cocs2D創建像(雪)的粒子,我發現此示例代碼中調用snowfall並在此代碼中有這樣的:創建沒有opengl和cocos2D的粒子。降雪應用

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

// start a timet that will fire 20 times per second 
[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 
} 



// Timer event is called whenever the timer fires 

- (void)onTimer 
{ 


// build a view from our 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, 5.0 * scale, 5.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, 5.0 * scale, 5.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 { 

UIImageView *flakeView = context; 
[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]; 


} 

我想知道如果這個代碼可以(可能是因爲它使用計時器)?如果我不知道CAReplicator可以用於iPhone,但CAReplicatorDemo僅適用於蘋果文檔中的mac:/對不起,米法語:/

+0

對NSLog()的調用是錯誤的(消除了-stringWithFormat:調用)並且使用'retainCount'沒有意義。 – bbum

回答

1

當然,這會影響性能。儘管你不會在模擬器中看到它。 UIImageView太重,無法每秒創建20次。您可以嘗試使用CALayer -s池(和CAAnimation)。

+0

你能解釋更多的CALayer和CAAnimation的使用是否有可能取代我的代碼與CALayers和CAAnimation? –

+0

它看起來像你想創造一些視覺的東西(雪?)。你用這個'UI'類。即「用戶界面」。你的雪不是用戶界面,它不需要處理用戶事件(觸摸等)。是的,可以通過'CA'完成。這更合乎邏輯。 – debleek63

+0

但是如何有任何代碼? –

1

你可以使用CAEmitterLayer在iOS 5上做粒子效果。我以前用它來做火焰和煙霧,但它應該適用於雪。