【问题标题】:CABasicAnimation using CAAnimationGroup to scale and fade is not centering使用 CAAnimationGroup 缩放和淡化的 CABasicAnimation 不居中
【发布时间】:2014-03-02 21:33:38
【问题描述】:

所以我正在尝试创建一个可以在给定视图上绘制扩展和消退气泡的类。 (它看起来类似于动态壁纸,但在我的应用程序中)。我有一种很好的感觉,这些问题与我的形状层的锚点或框架有关,但我一辈子都无法让这段代码正常工作。函数 newBubble 应该绘制一个圆圈并对其进行设置,使其在 4.0 秒内放大到 1.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 文件。

【问题讨论】:

    标签: ios cabasicanimation


    【解决方案1】:

    想通了……

        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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多