2017-02-08 264 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循环中使用该标签查找每个视图。无论如何,您需要根据需要调整上述代码,而不是将其复制/粘贴到您的程序中。