2012-12-14 28 views
7

我遇到了transitionWithViewanimateWithDuration問題。我的animateWithDuration塊之一不會轉換,這是一個突然的更改,並且transitionWithView不會暫時禁用用戶交互。我檢查了文檔,並相信我正在做的一切正常,但顯然有些問題。這裏是兩個代碼塊:transitionWithView和animateWithDuration的問題

這是在我的主視圖控制器ViewController其中有三個容器視圖/子視圖控制器。此塊移動其中一個容器視圖,但不會在轉換髮生時阻止用戶與ViewController中的其他交互。

[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^ { 
     CGRect frame = _containerView.frame; 
     frame.origin.y = self.view.frame.size.height - _containerView.frame.size.height; 
     _containerView.frame = frame; 
}completion:^(BOOL finished) { 
    // do something 
}]; 

這是在我的一個容器視圖控制器中。動畫似乎沒有效果,因爲productTitleLabelproductDescriptionTextView的文本突然變化,就好像動畫塊不存在一樣。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    [self.viewController toggleFlavoredOliveOilsTableView]; 

    if (indexPath.row > 0) { 
     NSDictionary *selectedCellDict = [[_flavoredOliveOilsDict objectForKey:@"Unflavored Olive Oils"] objectAtIndex:indexPath.row - 1]; 

     [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^ { 
      self.viewController.productTitleLabel.text = [_flavoredOliveOilsTableView cellForRowAtIndexPath:indexPath].textLabel.text; 
      self.viewController.productDescriptionTextView.text = [selectedCellDict objectForKey:@"Description"]; 
     }completion:nil]; 

     if (indexPath.row == 1) { 
      [self.viewController setProductDescriptionTextViewFrameForInformationTab]; 
     } 
     else { 
      [self.viewController setProductDescriptionTextViewFrameForNonInformationTab]; 

      //self.viewController.productImageView.image = [UIImage imageNamed:[selectedCellDict objectForKey:@"Image"]]; 
     } 
    } 
} 

我想,我的大部分動畫和過渡塊的預期不完全的工作問題有點關係。謝謝你的幫助。

編輯

我試圖做到的是移動的容器視圖中ViewController並設置標籤,文本視圖和圖像視圖的文本和圖像性能;所有這些都是主要觀點。這些屬性的細節通過子視圖控制器發送。 transitionWithView採用名爲toggleFlavoredOiveOilsTableView的方法,該方法在didSelectRowAtIndexPath中調用。我認爲問題在於我試圖同時調用兩個不同的動畫/轉換塊。

回答

13

如果一個動畫是在另一個正在進行動畫的視圖的子視圖上完成的,則您可以體驗這兩個動畫互相干擾的行爲。因此,如果您執行transitionWithView:self.view(即在主視圖上)就像您的代碼片段所示,則可能會出現問題。如果您對不同的子視圖執行這兩個動畫,問題可能會消失。在我的下面原來的答覆,我:

  • 上有兩個UILabel控制子視圖執行transitionWithView;

  • 把我的視圖控制器包含子視圖在主視圖的子視圖內,然後transitionFromViewController被限制爲該子視圖。

當我將兩個動畫部分放在不同的子視圖上時,動畫可以同時發生而不會發生事件。


如果你想動畫的兩個文本標籤內容的變化,您可以:

[UIView transitionWithView:self.viewController.textLabelsContainerView 
        duration:0.5 
        options:UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ 
        self.viewController.productTitleLabel.text = [_flavoredOliveOilsTableView cellForRowAtIndexPath:indexPath].textLabel.text; 
        self.viewController.productDescriptionTextView.text = [selectedCellDict objectForKey:@"Description"]; 
       } 
       completion:nil]; 

一般來說,我會用animateWithDuration,但文本屬性不是動畫的屬性,所以這就是爲什麼我使用transitionWithView,確保我有這些文本字段的容器。但我試過使用animateWithDuration對其他控件進行動畫製作,同時對兒童控制器的更改進行動畫製作,並且工作正常。

對於過渡子視圖控制器,我使用transitionFromViewController來動態交換容器子控制器的視圖(它是Managing Child View Controllers in a Custom Container方法系列的一部分)。爲了促進這個過程,我在我的主視圖控制器視圖中放置了一個容器視圖,並將該子控制器的視圖添加爲該容器的子視圖。這樣,當我爲孩子的過渡設置動畫時,動畫很好地限制在那個容器視圖中。

所以,這裏是一些示例代碼到一個視圖控制器添加到我的容器視圖,然後代碼我用兩個子視圖控制器之間的過渡採用分段按鈕:

- (void)addFirstChild 
{ 
    UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"Stooge"]; 
    [self addChildViewController:child]; 

    child.view.frame = self.bottomContainerView.bounds; 
    [self.bottomContainerView addSubview:child.view]; 

    [child didMoveToParentViewController:self]; 
} 

- (void)changedChild 
{ 
    UIViewController *oldController = [self.childViewControllers lastObject]; 
    UIViewController *newController; 

    if (self.segmentedControl.selectedSegmentIndex == 0) 
     newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Stooge"]; 
    else 
     newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Marx"]; 

    [oldController willMoveToParentViewController:nil]; 
    [self addChildViewController:newController]; 

    // start off screen below 

    CGRect startFrame = self.bottomContainerView.bounds; 
    startFrame.origin.y += startFrame.size.height; 

    // end up where it's supposed to be, right in the container 

    newController.view.frame = startFrame; 

    [self transitionFromViewController:oldController 
         toViewController:newController 
           duration:0.5 
           options:0 
          animations:^{ 
           newController.view.frame = self.bottomContainerView.bounds; 
          } 
          completion:^(BOOL finished) { 
           [oldController removeFromParentViewController]; 
           [newController didMoveToParentViewController:self]; 
          }]; 

} 

底線,我使容器子控件和父控制器中的字段動畫都沒有問題。也許我不瞭解你描述的問題。或者,我們在動畫製作方面的差異可能有些微妙。如果你想要,我已經把this above code放在github上,如果你想看看。

+0

如果要同時爲兩個標籤設置動畫,請將這兩個標籤放在自己的標籤容器視圖上,然後將其用作「transitionWithView」的第一個參數。 – Rob

+0

我現在有一部分工作正常。將animateWithDuration和UIViewAnimationOptionTransitions結合起來還是將transitionWithView和UIViewAnimationOptions結合起來是不可能的?看起來,當我有這些他們不工作,但如果我使用transitionWithView轉換選項它確實工作,同樣與動畫。我已經閱讀了文檔,似乎可以將它們全部結合在一起,但它並沒有爲我工作。 –

+0

請參閱修訂後的問題。謝謝 –

相關問題