ios – 单个UIView上的多个同时UIViewAnimations

ios – 单个UIView上的多个同时UIViewAnimations,第1张

概述目标:在单个UIView上同时播放多个动画. 问题:据我所知,这是不可能的.每个动画必须按顺序发生,不能重叠. 本质上,我想要一个UIView垂直动画,同时水平动画.因为我正在使用UIViewAnimationOptionRepeat,所以我无法在onCompletion中嵌套任何其他动画:这是我想要的工作,但不是: [UIView animateWithDuration:duration 目标:在单个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.height);                             } completion:^(BOol finished) {        }];                    } completion:^(BOol finished) {}];
解决方法 答案不是使用UIVIEwAnimation块而是使用CABasicAnimations.使用它们,您可以分别 *** 纵中心坐标(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    }

它非常甜蜜和容易.希望这有助于任何有类似需求的人.干杯!

总结

以上是内存溢出为你收集整理的ios – 单个UIView上的多个同时UIViewAnimations全部内容,希望文章能够帮你解决ios – 单个UIView上的多个同时UIViewAnimations所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1054792.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存