Ios 在单个UIView上同时显示多个UIView动画

Ios 在单个UIView上同时显示多个UIView动画,ios,objective-c,uiviewanimation,Ios,Objective C,Uiviewanimation,目标:在一个UIView上同时播放多个动画 问题:据我所知,这是不可能的。每个动画必须按顺序出现,并且不能重叠 本质上,我希望单个UIView在水平方向上同时设置动画,同时设置垂直方向的动画。由于我使用的是UIViewAnimationOptionRepeat,因此我无法在onCompletion中嵌套任何其他动画:以下是我想要工作的内容,但没有: [UIView animateWithDuration:duration delay:kANIMATIO

目标
在一个UIView上同时播放多个动画
问题
据我所知,这是不可能的。每个动画必须按顺序出现,并且不能重叠

本质上,我希望单个UIView在水平方向上同时设置动画,同时设置垂直方向的动画。由于我使用的是UIViewAnimationOptionRepeat,因此我无法在onCompletion中嵌套任何其他动画:以下是我想要工作的内容,但没有:

[UIView animateWithDuration:duration
                      delay:kANIMATION_DELAY
                    options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{

                        object.frame = CGRectMake(object.frame.origin.x + 40,
                                           object.frame.origin.y,
                                           object.frame.size.width,
                                           object.frame.size.height);

        [UIView animateWithDuration:duration
                              delay:kANIMATION_DELAY
                            options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{

                                object.frame = CGRectMake(object.frame.origin.x,
                                                   object.frame.origin.y - 40,
                                                   object.frame.size.width,
                                                   object.frame.size.height);

                             } completion:^(BOOL finished) {
        }];

                    } completion:^(BOOL finished) {
}];

答案不是使用UIViewAnimation块,而是使用Cabasicanimation。使用它们可以分别操纵中心坐标(x,y)。下面是我的代码现在的样子:

    UIView *view = views[i];

    // Add the horizontal animation
    CABasicAnimation *horizontal = [CABasicAnimation animationWithKeyPath:@"position.x"];

    horizontal.delegate = self;
    horizontal.fromValue = [NSNumber numberWithFloat:25.0];
    horizontal.toValue = [NSNumber numberWithFloat:50.0];
    horizontal.repeatCount = INFINITY;
    horizontal.duration = 6;
    horizontal.autoreverses = YES;
    horizontal.beginTime = 0; // ignore delay time for now
    horizontal.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [view.layer addAnimation:horizontal forKey:@"horizontal_animation"];

    // Add the vertical animation
    CABasicAnimation *vertical = [CABasicAnimation animationWithKeyPath:@"position.y"];

    vertical.delegate = self;
    vertical.fromValue = [NSNumber numberWithFloat:100.0];
    vertical.toValue = [NSNumber numberWithFloat:400.0];
    vertical.repeatCount = INFINITY;
    vertical.duration = 2;
    vertical.autoreverses = YES;
    vertical.beginTime = 0; // ignore delay time for now
    vertical.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    [view.layer addAnimation:vertical forKey:@"vertical_animation"];
这允许我在两个单独的动画中处理重复、持续时间等。然后,当我的动画结束时,我可以调用follow方法来删除动画。或者

    [view.layer removeAllAnimations];
或者,如果我想删除更具体的动画

    [view.layer removeAnimationForKey:@"vertical_animation"];
然后,如果希望对动画的开始/停止时间进行更多自定义控制,只需添加以下委托方法:

    -(void)animationDidStart:(CAAnimation *)anim {
        // custom code here
    }

    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
        // custom code here
    }

它很甜很简单。希望这能帮助任何有类似需要的人。干杯

您在for键[view.layer addAnimation:horizontal forKey:@“horizontal_animation”]中遗漏了addAnimation;