2013-09-22 40 views
2

您好我有一塊其中只有一個行不工作的代碼......animateWithDuration SETFRAME不工作

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
    [label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working 
    [self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working 
    [self.currentLabel setAlpha:0.0f]; //working 
} completion:^(BOOL finished) { 
    [self.currentLabel removeFromSuperview]; 
    self.currentLabel = label; 
}]; 

我跑出來的想法有什麼錯......

+0

我有同樣的問題。 – chenyi1976

+0

我假設故事板或xib正確嗎? – Logan

+0

如果是這樣,請轉到您的xib或該視圖的故事板,並取消選擇「useAutolayout」。我有照片在某處,我必須找到它。 – Logan

回答

0

幀必須一個CGRect與4個參數:(xPosition,yPosition,寬度,高度)。使用CGRectMake用於設定框架

self.currentLabel.frame = CGRectMake(100, 100, self.currentLabel.frame.size.width, self.currentLabel.frame.size.height) 

您可以設置0.3秒的動畫時間,當它的盡頭,你從視圖中刪除。這是太短了。設置示例2的持續時間,並且您將看到您的標籤正在移動。

+0

我做了,它不工作 –

+0

self.currentLabel.frame = CGRectMake(100,100,self.currentLabel.frame.size.width, self.currentLabel.frame.size.height) – incmiko

+0

還沒有,我試過 –

0

有時候,你應該在完成塊的動畫視圖還動畫後,例如設置框架,嘗試類似的東西

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
    [label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working 
    [self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working 
    [self.currentLabel setAlpha:0.0f]; //working 
} completion:^(BOOL finished) { 
    self.frame = currentLabelFrame; 
    label.frame = label.frame; 
    [self.currentLabel removeFromSuperview]; 
    self.currentLabel = label; 
}]; 
2

我發現的是:如果我關掉「使用自動佈局」,然後它神奇地工作,所以這個問題與自動佈局有關。

搜索後,我發現這一點:http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was

添加約束出口映射視圖,然後而是採用SETFRAME,更新的約束會做的伎倆!

下面是我最終的實現(_topConstraint是表視圖頂部的垂直空間的限制):

- (IBAction)switchButtonTouched:(id)sender 
{ 
    if (_topConstraint.constant <= 0) 
     _topConstraint.constant = _buttonView.frame.size.height; 
    else 
     _topConstraint.constant = 0; 

    [_tableView setNeedsUpdateConstraints]; 
    [UIView animateWithDuration:0.5f animations:^(void){ 
     [_tableView layoutIfNeeded]; 
    } completion:^(BOOL finished){ 
    }]; 
} 
-1

如果您的問題既不是自動佈局有關,或此處列出的其他人,也有另一種可能的問題原來是我的問題。我同時在視圖上運行UIView轉換(transitionFromView:toView :),佔用相同的屏幕空間(不同的超級視圖,不相關,除了定位在全局視圖控制器的超級視圖中)。

我的代碼是這樣的:

[UIView transitionFromView:self.someView 
        toView:self.someOtherView 
        duration:0.6f 
        options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionShowHideTransitionViews 
       completion:NULL]; 

[UIView animateWithDuration:0.3 
         delay:0.0 options:UIViewAnimationOptionCurveEaseOut 
        animations:^{ 
         [self.someThirdView setFrame:someFrame]; //not working 
         [self.someThirdView setAlpha:1.0f]; //working 
       } completion:nil]; 

框架改變,但它彈出新的框架,而不是動畫。我猜這是一個內部的iOS問題。只要我刪除「transitionFromView:...」調用,框架動畫工作正常。

我現在的解決方案是將第二個動畫移動到transitionFromView的完成塊。這並不完美,因爲這兩個動畫應該排在一起,但現在它是一個可以通過的解決方案。