2014-06-11 35 views
1

我试图重新创建这个效果(见下图),但是有一个圆形路径,因为我有一个圆形按钮。CAEmitterLayer - kCAEmitterLayerCircle,循环路径?

这是源代码。

https://github.com/uiue/ParticleButton/blob/master/ParticleButton/EmitterView.m

我所做的修改似乎影响了粒子动画的方法,而不是路径。

我还没有研究深度使用的技术,但我不相信这是可能的?

fireEmitter = (CAEmitterLayer *)self.layer; 
    //fireEmitter.renderMode = kCAEmitterLayerAdditive; 
    //fireEmitter.emitterShape = kCAEmitterLayerLine; 

    // CGPoint pt = [[touches anyObject] locationInView:self]; 
    float multiplier = 0.25f; 
    fireEmitter.emitterPosition = self.center; 
    fireEmitter.emitterMode = kCAEmitterLayerOutline; 
    fireEmitter.emitterShape = kCAEmitterLayerCircle; 
    fireEmitter.renderMode = kCAEmitterLayerAdditive; 
    fireEmitter.emitterSize = CGSizeMake(100 * multiplier, 0); 

enter image description here

+0

你能发表一个包含你的圆形按钮的例子(也许是一个文件)吗? – Alladinian

+0

是不是这个副本http://stackoverflow.com/questions/8296775/use-caemitterlayer-to-draw-particles-around-a-circle-or-a-cgpath – HpTerm

+0

由于CAEmitterLayer是一个CALayer子类,你是否尝试进行CAAnimation? – Francescu

回答

3

你只是设置发射器的属性有(无关紧要了,它的位置的动画)。动画创建并添加到按钮的部分在您的问题中缺失(但存在于示例项目中)。

有在drawRect:方法动画被添加如下:

-(void)drawRect:(CGRect)rect 
{ 
    CGMutablePathRef path = CGPathCreateMutable(); 

    CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); 

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
    animation.duration = 3; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"]; 
    animation.repeatCount = MAXFLOAT; 
    animation.path = path; // That's what defines the path of the animation 
    [emitterView.layer addAnimation:animation forKey:@"test"]; 
} 

基本上说,借此路径(此时与按钮的尺寸的矩形)和动画emitterView S(颗粒)沿此路径的位置3秒(并永久重复)。

所以,在animation.path到这样的事情,例如一个简单的变化可以定义一个圆形路径发射极跟随:

animation.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;

(基本上是一个带有按钮的尺寸的椭圆形 - 这可能是你想要什么,或不 - 但你的想法...)

注:我不是暗示drawRect:是创建并添加动画(它只是根据样本的最佳地方,你提供了)。只要你有一个匹配你的按钮的圆形的路径,你可以任何你喜欢的方式创建和添加动画。

+0

呵呵,当然,我不知道我是怎么错过的,谢谢你的完整解释。 :)它不会让我奖励赏金atm。 – Jules

+0

@Jules不用担心,很高兴帮助你! – Alladinian

7

enter image description here

CustomButton.m

-(void)drawRect:(CGRect)rect 
{ 
    //绘制路径 
    CGMutablePathRef path = CGPathCreateMutable(); 

    CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)); 

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
    animation.duration = 3; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"]; 
    animation.repeatCount = MAXFLOAT; 
// animation.path = path; 
    animation.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath; 
    [emitterView.layer addAnimation:animation forKey:@"test"]; 
} 

创建圆形自定义按钮。

CustomButton *button = [[CustomButton alloc] initWithFrame:CGRectMake(100, 100, 300, 300)]; 
[self.view addSubview:button]; 
button.center = self.view.center; 
button.layer.cornerRadius = button.frame.size.width/2.f; 
button.clipsToBounds = YES; 
+0

如果你希望这看起来好多了,在animationWithKeyPath中把'position'改成'emitterPosition'。 – Joe

+0

我如何更改旋转后的按钮的起点 –