2011-09-29 67 views
2

我有2个子视图需要整个屏幕(除了状态栏)。我们称这个尺寸为“屏幕尺寸”。iPhone - 同时动画2个子视图

欲动画两者:

  • 从一点点比屏幕尺寸与屏幕尺寸较大,以放大所述第一,从阿尔法0到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按预期开始动画。但我无法同时运行两个动画。

我该怎么做?

回答

0

我看不出什么错在你的代码。 所以我尝试了你的代码在我的项目中,它工作得很好:它确实是你想要的。

视图1向下移动,右和淡出WHILE视图2来自左上方和淡入...

,所以我想你必须寻找一个错误别的地方...

的Ciao,

卢卡

编辑:

下面的代码:

test-switch.zip

+0

呃......你在开玩笑吗?我会尽量隔离你今晚做的代码。 :-) – Oliver

+0

啊啊......对不起,但是不......你的搜索字段比你发送的代码大一点......试试我隔离的代码(查看新的编辑答案) – meronix

2

试着看看基于块的动画。这是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"); 
       }]; 

什么:^ {}将您的视图的目标设置。上面我向你展示了如何改变位置以及阿尔法。

+0

我认为这些会更适合当你想要把它们连,或让它们依次发生(一个接一个地发生),但他希望它们发生在山姆时间。 –

+0

我得到了链接部分,但是他能不能只有两个类似的块语句彼此相邻吗?所以这些街区或多或少同时开火? – Cameron

+0

我现在明白你的观点。也许,我没有尝试过。 –

0

我想你要使用的是一个CAAnimationGroup(核心动画组)。

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

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

这里是一个usage example

+0

谢谢。你能看到我的编辑?还有一个额外的问题:在定义CAAnimationGroup时,我将不得不影响它?第一种观点,第二种观点?或者我想要动画的2个子视图的父项? – Oliver

+0

动画组将基本包含两个动画的数组,每个动画都会影响您想要动画的不同视图。 –