ios – 使用CAAnimationGroup的序列动画

前端之家收集整理的这篇文章主要介绍了ios – 使用CAAnimationGroup的序列动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图制作动画序列,我在CAAnimationGroup中找到了实现该对象的正确类.在实践中,我添加了不同的子视图,我想用弹跳效果为他们的入门动画,事实上,我想看到他们的动画发生在上一个完成之后.我知道我可以设置代表,但我认为CAAnimationGroup是正确的选择.
后来我发现组动画只能属于一层,但我需要它在屏幕上的不同层.当然,主机层不起作用.
一些建议?
- (void) didMoveToSuperview {
    [super didMoveToSuperview];
    float startTime = 0;
    NSMutableArray * animArray = @[].mutableCopy;
    for (int i = 1; i<=_score; i++) {
        NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: self.greenLeaf];
        UIImageView * greenLeafImageView = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
        greenLeafImageView.image = [UIImage imageNamed:@"greenLeaf"];
        CGPoint leafCenter =  calculatePointCoordinateWithRadiusAndRotation(63,-(M_PI/11 * i) - M_PI_2);
        greenLeafImageView.center = CGPointApplyAffineTransform(leafCenter,CGAffineTransformMakeTranslation(self.bounds.size.width/2,self.bounds.size.height));
        [self addSubview:greenLeafImageView];

        //Animation creation
        CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        greenLeafImageView.layer.transform = CATransform3DIdentity;
        bounceAnimation.values = @[
                                  [NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:1.1],[NSNumber numberWithFloat:0.8],[NSNumber numberWithFloat:1.0]
                                  ];

        bounceAnimation.duration = 2;
        bounceAnimation.beginTime = startTime;
        startTime += bounceAnimation.duration;

        [animArray addObject:bounceAnimation];
        //[greenLeafImageView.layer addAnimation:bounceAnimation forKey:nil];


    }
    // Rotation animation
    [UIView animateWithDuration:1 animations:^{
        self.arrow.transform = CGAffineTransformMakeRotation(M_PI/11 * _score);
    }];
    CAAnimationGroup * group = [CAAnimationGroup animation];
    group.animations = animArray;
    group.duration = [[ animArray valueForKeyPath:@"@sum.duration"] floatValue];
    [self.layer addAnimation:group forKey:nil];
}

解决方法

CAAnimationGroup旨在将多个CAAnimation子类堆叠在一起以形成动画,例如,一个动画可以执行缩放,另一个动画可以进行缩放,而另一个可以旋转它,而第三个动画可以旋转它,而不是用于管理多个图层,但是具有多个重叠动画.

也就是说,我认为解决问题最简单的方法是将每个CAAnimation的beginTime分配给所有之前的时间的总和,以说明:

for i in 0 ..< 20
{
    let view : UIView = // Obtain/create the view...;
    let bounce = CAKeyframeAnimation(keyPath: "transform.scale")

    bounce.duration  = 0.5;
    bounce.beginTime = CACurrentMediaTime() + bounce.duration * CFTimeInterval(i);

    // ...

    view.layer.addAnimation(bounce,forKey:"anim.bounce")
}

请注意,每个人都会获得持续时间*,而CACurrentMediaTime()在使用beginTime属性(基本上是动画中使用的“now”的高精度时间戳)时是必需的.整条线可以解释为现在持续时间* i.

必须注意的是,如果将CAAnimations添加到CAAnimationGroup,则其beginTime将变为相对于组的开始时间,因此在整个组启动后,动画的值为5.0.在这种情况下,您不使用CACurrentMediaTime()

原文链接:https://www.f2er.com/iOS/329119.html

猜你在找的iOS相关文章