2017-02-08 257 views
0

我們需要按照一定的時間間隔(默認圖像處於隱藏狀態=是,並且標籤從1111到1120)順序顯示10張圖像。代碼的結果是立即顯示圖像,毫不拖延。動畫是一個獨立的功能。可能是什麼問題呢?我使用的Xcode 8.2.1動畫無延遲(UIView animateWithDuration延遲)

-(void)doski:(NSInteger)i 
    { 
    [UIView animateWithDuration:1.0 
          delay:5.0 
          options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction 
         animations:^(void) { 
         } 
         completion:^(BOOL finished) { 
          [self.view viewWithTag:(i+1110)].hidden=NO; 
          NSInteger i2=i; 
          i2++; 
          if(i2<11) 
          [self doski:i2]; 
          }]; 
    } 
    ........... 
    //function call 
    [self doski:1]; 

當您使用此選項時,情況並沒有改變:

-(void)doski:(NSInteger)i 
     { 
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut 
        animations:^(void) { 
        } 
        completion:^(BOOL finished){ 
         [UIView animateWithDuration:2.0 delay:1.0 options: 
          UIViewAnimationOptionCurveEaseIn animations:^{ 
           [self.view viewWithTag:(i+1110)].hidden=NO; 
          } completion:^ (BOOL completed) {NSInteger i2=i; 
           i2++; 
           if(i2<18) 
           [self doski:i2];}]; 
        }]; 
} 

謝謝。

+0

如果(完成)在完成塊中添加,並且您在第一個方法的動畫塊之外調用[self dosk:1]。這段代碼真的很混亂和臭,我會用UICollecitonView去做動畫。 –

回答

0

我認爲圖像立即出現的原因是你的動畫塊是空的。在一個空的動畫塊中,完成塊將立即被調用(沒有任何動畫可以完成)。

要解決你的代碼,你可以嘗試這樣的事情你的電話之前,animateWithDuration...

UIView *viewToAnimate = [self.view viewWithTag:(i+1110)] 
viewToAnimate.alpha = 0 
viewToAnimate.hidden = NO 

然後,阿爾法設置爲1動畫塊中,像這樣:

viewToAnimate.alpha = 1 

下面是一個完整的例子,說明空白動畫塊的問題。您可以複製並粘貼到操場上。儘管如此,不是Objective-C

import UIKit 
import PlaygroundSupport 

class MyView : UIView { 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.setup() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    private func setup() { 
     let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 
     view.backgroundColor = UIColor.red 
     self.addSubview(view) 

     UIView.animate(withDuration: 2, delay: 1, options: [], animations: { 
      // commenting out the following line will 
      // trigger the completion block immediately 
      view.frame = CGRect(x: 300, y: 300, width: 50, height: 50) 
     }, completion: { finished in 
      print("done") 
     }) 
    } 

} 

let view = MyView(frame: CGRect(x: 0, y: 0, width: 400, height: 400)) 
view.backgroundColor = UIColor.black 
PlaygroundPage.current.liveView = view 
0

您的代碼有多個問題。

您不能爲視圖的隱藏屬性設置動畫。隱藏的財產不具動畫性。相反,您需要將alpha值設置爲0並將其設置爲1.0。這將導致視圖淡入,這是,我認爲,你想要什麼。

您的兩個代碼塊都不會在動畫塊內執行任何操作。這是錯誤的。在動畫塊內部,您需要更改要動畫的視圖的一個或多個動畫屬性。

如果您希望視圖一次顯示一個視圖,則應該爲第一個動畫調用animateWithDuration:delay:options:animations:completion:延遲值爲0,併爲第二個動畫延遲第一個動畫的持續時間等。第一個動畫將立即開始,第二個動畫將在第一個動畫完成時開始,依此類推。

您的代碼可能是這個樣子:

NSTimeInterval duration = 0.25; 
for (index = 0; x < 10; x++) { 
    UIView *viewToAnimate = arrayOfViewsToAnimate[index]; 

    //For each view, set it's hidden property to false 
    //and it's alpha to zero 
    viewToAninimate.hidden = false; 
    viewToAnimate.alpha = 0.0; 
    [UIView animateWithDuration:duration 
    delay: duration * index 
    options: 0 
    animations:^{ 
     //inside the animation block, set the alpha to 1.0 
     viewToAnimate.alpha = 1.0; 
    } 
    completion: nil 
    ]; 
} 

該代碼假定您已經創建了包含要進行動畫處理的意見陣列arrayOfViewsToAnimate。您也可以將標籤分配給視圖,並在for循環中使用該標籤查找每個視圖。無論如何,您需要根據需要調整上述代碼,而不是將其複製/粘貼到您的程序中。