当我的用户在屏幕上拖动手指时,我想产生一些粒子。我有所有的拖动代码,但我的问题是我根本看不到任何粒子绘制!
通过一些谷歌搜索,我提出了以下类,我已经创建了 UIView 的子类:
#import "PrettyTouch.h"
#import <QuartzCore/QuartzCore.h>
@implementation PrettyTouch
{
CAEmitterLayer* touchEmitter;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
touchEmitter = (CAEmitterLayer*) self.layer;
touchEmitter.emitterPosition= CGPointMake(0, 0);
touchEmitter.emitterSize = CGSizeMake(5,5);
CAEmitterCell* touch = [CAEmitterCell emitterCell];
touch.birthRate = 0;
touch.lifetime = 0.6;
touch.color = [[UIColor colorWithRed:0.2 green:0.3 blue:1.0 alpha:0.06] CGColor];
touch.contents = (id)[[UIImage imageNamed:@"part.png"]CGImage];
touch.velocity = 0;
touch.velocityRange = 50;
touch.emissionRange = 2*M_PI;
touch.scale = 1.0;
touch.scaleSpeed = -0.1;
touchEmitter.renderMode = kCAEmitterLayerAdditive;
touchEmitter.emitterCells = [NSArray arrayWithObject:touch];
}
return self;
}
+ (Class) layerClass
{
//tell UIView to use the CAEmitterLayer root class
return [CAEmitterLayer class];
}
- (void) setEmitterPosition:(CGPoint)pos
{
touchEmitter.emitterPosition = pos;
}
- (void) toggleOn:(bool)on
{
touchEmitter.birthRate = on? 300 : 0;
}
@end
然后在我的游戏视图控制器类中,在 viewDidLoad 中,我这样做了:
@implementation PlayViewController
{
//...
PrettyTouch* touch;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//...
touch = [[PrettyTouch alloc]initWithFrame:self.view.frame];
touch.hidden = NO;
[self.view addSubview:touch];
//...
然后在我的 UIPanGestureRecogniser 函数中,我会[touch toggleOn:YES];
在手势开始[touch setEmitterPosition:[gest locationInView:self.view]];
时和函数被调用时调用。(gest 是 UIPanGestureRecognizer*)。
我可能会丢失什么或者我需要做什么才能获得粒子图?
谢谢