使用CAAnimationGroup进行缩放和淡入淡出的CABasicAnimation不是居中的

时间:2014-03-02 21:33:38

标签: ios cabasicanimation

所以我试图创建一个可以在给定视图上绘制扩展和渐弱气泡的类。 (它看起来类似于dynmaic壁纸,但在我的应用程序中)。我非常感觉这些问题与我的形状层的anchorPoint或框架有关,但我不能为我的生活让这个代码工作。函数newBubble应绘制一个圆并进行设置,以便它可以扩展到1.0并在4.0秒内将不透明度淡化为0.0。一切都很完美除了圆圈相对于0,0缩放。 (好像有人把针放在0,0并从圆圈的右下角拉伸。)

我将锚设置为(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文件。

1 个答案:

答案 0 :(得分: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;
相关问题