【问题标题】:iPhone - Animate 2 subviews at the same timeiPhone - 同时动画 2 个子视图
【发布时间】:2011-09-29 22:10:08
【问题描述】:

我有 2 个占据整个屏幕的子视图(状态栏除外)。我们称这个尺寸为“屏幕尺寸”。

我想同时动画:

  • 第一个从比屏幕尺寸大一点的屏幕尺寸缩放到屏幕尺寸,从 alpha 0 到 alpha 1。

  • 秒从屏幕尺寸到比屏幕尺寸小一点,从alpha 1到alpha 0。

第二个视图在开始时可见并在屏幕上。

这是我写的:

- (void) switchViews
{
    if (self.view2Controller == nil) {
        self.view2Controller = [[View2Controller alloc] initWithNibName:@"View2XIB" bundle:nil];
        self.view2Controller.view.hidden = YES;
        [self.view addSubview:self.view2Controller.view];
    }

    CGRect bigFrame = CGRectInset(self.view.frame, -50, -50);
    CGRect normalFrame = self.view.frame;
    CGRect smallFrame = CGRectInset(self.view.frame, 50, 50);

    self.view2Controller.view.frame = bigFrame;
    self.view2Controller.view.alpha = 0.0;

    [UIView beginAnimations:@"Anim1" context:nil];
    [UIView setAnimationDuration:5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    self.view2Controller.view.hidden = NO;
    self.view2Controller.view.frame = normalFrame;
    self.view2Controller.view.alpha = 1.0;

    [UIView commitAnimations];

    // ------------------------------

    [UIView beginAnimations:@"Anim2" context:nil];
    [UIView setAnimationDuration:5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    self.view1Controller.view.frame = smallFrame;
    self.view1Controller.view.alpha = 0.0;

    [UIView commitAnimations];
}

当然,我首先尝试将两个动画都放入一个独特的动画中。这不会改变任何事情,这就是我试图将它们分开的原因。

启动时,view1 立即变为黑色,然后 view2 开始按预期设置动画。但我无法同时运行两个动画。

我该怎么做?

【问题讨论】:

    标签: iphone ios cocoa-touch animation uiview


    【解决方案1】:

    尝试查看基于块的动画。这是 iOS 4.0+ 推荐的方法。看看这里的答案:What are block-based animation methods in iPhone OS 4.0?

    编辑 试试这样的

    //You can do the same thing with a frame
    CGPoint newCenter = CGPointMake(100, 100);
    [UIView animateWithDuration:2.0
                     animations:^{ 
                         firstView.center = newCenter;
                         secondView.center = newCenter;
                         firstView.alpha = 0.2;
                     } 
                     completion:^(BOOL finished){
                         NSLog(@"All done animating");
                     }];
    

    您放入动画中的任何内容:^{ } 将是您视图的目标设置。上面我向您展示了如何更改位置以及 alpha。

    【讨论】:

    • 我认为这些更适合您想要将它们链接起来,或者让它们按顺序出现(一个接一个),但他希望它们同时出现。跨度>
    • 我得到了链接部分,但他不能只有两个相似的块语句相邻吗?那么这些方块或多或少会同时开火?
    • 你为什么认为我在为 iOS 4 编写代码?
    • 这里无需对抗。您提出了一个问题,其中大约 94% 的设备都支持其中一个提议的解决方案。公平地说,如果您想要对 3.x 的旧版支持,那很好,但您没有在问题中说明这一点。
    • @Cameron :请编辑您的答案以解释为什么这可以解决问题,所以我可以删除反对票。
    【解决方案2】:

    我看不出你的代码有什么问题。 所以我在我的项目中尝试了你的代码,它运行良好:它完全符合你的要求。

    View1 向右下方移动并淡出,而 view2 来自左上角并淡入...

    所以我猜你必须在其他地方寻找错误......

    知乎,

    卢卡

    编辑:

    代码如下:

    test-switch.zip

    【讨论】:

    • 呃……不,你在开玩笑吗?我会像你今晚所做的那样尝试隔离代码。 :-)
    • 啊啊...抱歉,但没有...您的搜索字段比您发送的代码大一点...试试我的隔离代码(请参阅新编辑的答案)
    【解决方案3】:

    我认为您要使用的是CAAnimationGroup(核心动画组)。

    CAAnimationGroup 允许对多个动画进行分组并同时运行。分组动画在 CAAnimationGroup 实例指定的时间空间内运行。

    它有一个animations 属性,您可以将其设置为您希望它与setAnimations: 一起执行的动画数组。

    这是usage example

    【讨论】:

    • 谢谢。你能看到我的编辑吗?还有一个额外的问题:当定义 CAAnimationGroup 时,我必须影响哪个视图?第一种观点,第二种观点?还是我想要制作动画的 2 个子视图的父级?
    • 动画组将基本上包含两个动画的数组,每个动画都会影响您要制作动画的不同视图。
    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    相关资源
    最近更新 更多