1

好吧,我想在没有 openGL 或 cocs2D 的情况下创建像 (snow) 这样的粒子,我发现这个示例代码被调用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];


}

我想知道这段代码是否会损害性能或内存(可能是因为它使用了计时器)?如果这不是一个好主意,我听说我们可以在 iphone 上使用 CAReplicator 但 CAReplicatorDemo 仅适用于苹果文档上的 mac :/对不起我的英语我是法国人:/

4

3 回答 3

1

您可以在 iOS 5 上使用CAEmitterLayer来制作粒子效果。我以前用它来制作火焰和烟雾,但它应该适用于雪。

于 2011-11-29T17:44:00.373 回答
1

当然,它会影响性能。你不会在模拟器中看到它。UIImageView太重了,无法每秒创建 20 次。您可以尝试使用CALayer-s 池代替(和CAAnimation)。

于 2011-11-29T15:26:08.537 回答
0

你可以试试我的 KHParticleView,它使用 cocos2d 粒子并在顶部添加 cocos2d 视图。在此处查看代码:https ://github.com/lephukhanhhuy/KHParticleView

于 2013-08-11T11:28:25.080 回答