создавать частицы без opengl и cocos2D.применение снегопада - PullRequest
1 голос
/ 29 ноября 2011

ну, я хотел создать частицы типа (снег) без openGL или cocs2D, и я нашел этот пример кода под названием snowfall, и в этом коде есть это:

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 на документация по яблоку: / извините за мой английский я французский: /

Ответы [ 3 ]

1 голос
/ 29 ноября 2011

Вы можете использовать CAEmitterLayer для создания эффектов частиц в iOS 5. Ранее я использовал его для пламени и дыма, но он должен прекрасно работать для снега.

1 голос
/ 29 ноября 2011

Конечно, это повлияет на производительность. Вы не увидите его в симуляторе, хотя. UIImageView слишком тяжелый для создания 20 раз в секунду. Вместо этого вы можете попробовать использовать пул CALayer -s (и CAAnimation).

0 голосов
/ 11 августа 2013

Вы можете попробовать мой KHParticleView, он использует частицы cocos2d и добавить представление cocos2d сверху. Проверьте код здесь: https://github.com/lephukhanhhuy/KHParticleView

...