2013-04-25 66 views
6

如果你創建一個全新的單一視圖的應用程序,並把這個代碼後面的按鈕:如何添加翻轉動畫的子視圖?

UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; 
blah.backgroundColor = [UIColor grayColor]; 
[UIView transitionWithView:blah duration:1 
        options:UIViewAnimationOptionTransitionFlipFromRight 
       animations:^{ 
        [self.view addSubview:blah]; 
       } 
       completion:^(BOOL finished){ 

       }]; 

子視圖與沒有動畫立即加入。如果你先添加子視圖,然後嘗試對它進行動畫處理...你會遇到同樣的問題。

UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; 
    [self.view addSubview:blah]; 
    [UIView transitionWithView:blah duration:1 
         options:UIViewAnimationOptionTransitionFlipFromRight 
        animations:^{ 
         blah.backgroundColor = [UIColor grayColor]; 
        } 
        completion:^(BOOL finished){ 

        }]; 

在地球上,您如何在添加子圖的同時或之後爲子圖添加動畫?

+0

當您使用容器視圖,而不是胡說發生了什麼[UIView transitionWithView:containerView ...] – guenis 2013-04-25 21:26:07

+0

如果我使用self.view作爲轉換視圖,那麼它翻轉整個屏幕,當它完成時,我的子視圖顯示。當我展示它時,我只想翻轉子視圖。 – 2013-04-25 21:29:52

回答

12

您通常需要有約束動畫是在已經到位的容器:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CGRect frame = CGRectMake(0, 0, 100, 100); 

    _container = [[UIView alloc] initWithFrame:frame]; 
    _container.backgroundColor = [UIColor lightGrayColor]; 
    [self.view addSubview:_container]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    UIView *subview = [[UIView alloc] initWithFrame:_container.bounds]; 
    subview.backgroundColor = [UIColor darkGrayColor]; 

    [UIView transitionWithView:_container 
         duration:1.0 
         options:UIViewAnimationOptionTransitionFlipFromRight 
        animations:^{ 
         [_container addSubview:subview]; 
        } 
        completion:NULL]; 
} 
+1

是的,那工作。只是做一個預先加載的子視圖容器很好用!謝謝,羅伯! – 2013-04-25 21:53:09

+0

你可以添加Container並在一個回調中進行轉換嗎? – 2014-03-01 11:37:59

1

這是值得一試:

UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; 
blah.backgroundColor = [UIColor grayColor]; 
[self.view addSubview:blah]; 
blah.alpha = 0.0; //Or with blah.hidden = TRUE and then FALSE inside the animation block 
[UIView transitionWithView:blah 
        duration:1 
        options:UIViewAnimationOptionTransitionFlipFromRight 
       animations:^{ 
        blah.alpha = 1.0; 
       } 
       completion:^(BOOL finished){ 

       }]; 
+0

大聲笑,我也嘗試過,但沒有翻轉,但是,子視圖淡入。我真的需要翻轉。 ;( – 2013-04-25 21:34:49

+0

非常感謝guenis 阿爾法仍然使魔術.. :) – Abo3atef 2014-06-25 02:08:20