2014-03-02 50 views
0

所以我试图制作一个可以在给定视图上绘制扩展和渐变气泡的类。 (它看起来类似于dynmaic壁纸,但在我的应用程序中)。我有一个相当不错的感觉,这些问题与我的形状图层的anchorPoint或者框架有关,但是我不能让我的这段代码工作。函数newBubble应该绘制一个圆并设置它,以便它可以放大到1.0,并在4.0秒内将不透明度消除为0.0。除了相对于0,0缩放圆圈之外,一切都完美无缺。 (它,就好像有人已经把一个针放在0,0处并且从该圆的右下方延伸)。使用CAAnimationGroup进行缩放和褪色的CABasicAnimation并不集中

我将锚点设置为(0.5,0.5)以及许多其他的东西,无用的。我将框架设置为它所绘制的框架,然后将框架设置为整个视图,没有任何工作。我一直在配音几个小时,我似乎无法弄清楚这一点。所有动画发生的newBubble函数位于下方,您可以在此link处找到整个xcodeproj(只需一个每半秒触发一次的定时器和一个停止/启动按钮)。我知道这是非常简单的事情,所以在此先感谢!

- (void) newBubble { 
    int x = arc4random() % (int) (bubblesView.frame.size.width-100); 
    int y = arc4random() % (int) (bubblesView.frame.size.height-100); 

    CGRect circleFrame = CGRectMake(x, y, 100, 100); 
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:circleFrame]; 

    CAShapeLayer *bubble = [CAShapeLayer layer]; 
    bubble.path = path.CGPath; 
    bubble.anchorPoint = CGPointMake(0.5f, 0.5f); 
    bubble.frame = circleFrame; 
    bubble.fillColor = [UIColor whiteColor].CGColor; 

    CABasicAnimation *bubbleFadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    bubbleFadeIn.fromValue = [NSNumber numberWithFloat:1.0f]; 
    bubbleFadeIn.toValue = [NSNumber numberWithFloat:0.0f]; 

    CABasicAnimation *bubbleExplode = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
    bubbleExplode.fromValue = [NSNumber numberWithFloat:0.0f]; 
    bubbleExplode.toValue = [NSNumber numberWithFloat:1.0f]; 

    CAAnimationGroup *bubbleAnims = [CAAnimationGroup animation]; 
    [bubbleAnims setAnimations:[NSArray arrayWithObjects:bubbleFadeIn, bubbleExplode, nil]]; 
    [bubbleAnims setDuration:4.0]; 
    [bubbleAnims setRemovedOnCompletion:NO]; 
    [bubbleAnims setFillMode:kCAFillModeForwards]; 
    [bubble addAnimation:bubbleAnims forKey:nil]; 

    [bubblesView.layer addSublayer:bubble]; 
} 

注意:bubblesView是该类的一个属性,它由启动它的视图控制器设置为self.view。如果您仍然感到困惑,我鼓励您打开xcode文件。

回答

0

想通了......

CGRect circleFrame = CGRectMake(x, y, 100, 100); 
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:circleFrame]; 
    CGPoint circleAnchor = CGPointMake((x+50)/ bubblesView.frame.size.width, (y+50)/ bubblesView.frame.size.height); 

    CAShapeLayer *bubble = [CAShapeLayer layer]; 
    bubble.path = path.CGPath; 
    bubble.anchorPoint = circleAnchor; 
    bubble.frame = bubblesView.frame; 
    bubble.fillColor = [UIColor whiteColor].CGColor;