动画无延迟(UIView animateWithDuration延迟)

时间:2017-02-08 20:42:38

标签: ios objective-c uiview xcode8 uiviewanimation

我们需要以一定的时间间隔顺序显示10个图像(默认图像隐藏=是,并且标记为1111到1120)。代码的结果是一次性出现图像,没有延迟。动画是一个单独的功能。可能是什么问题呢?我正在使用xcode 8.2.1

-(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];}];
                     }];
}

谢谢。

2 个答案:

答案 0 :(得分:0)

我认为图像立即出现的原因是你的动画块是空的。使用空动画块,将立即调用完成块(没有任何动画,所以我们也可以这样做。)

要修复您的代码,您可以在致电animateWithDuration...

之前尝试使用此类代码
UIView *viewToAnimate = [self.view viewWithTag:(i+1110)]
viewToAnimate.alpha = 0
viewToAnimate.hidden = NO

然后将动画块内的alpha设置为1,如下所示:

viewToAnimate.alpha = 1

这是一个完整的例子,用于说明空动画块的问题。您可以将其复制并粘贴到游乐场中。它虽然是Swift,但不是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

答案 1 :(得分:0)

您的代码存在多个问题。

您无法为视图的隐藏属性设置动画。隐藏属性不可动画。相反,您需要将alpha值设置为0并将其设置为1.0。这将导致视图淡入,我认为,这就是你想要的。

您的两个代码块都不会在动画块中执行任何操作。那是错的。在动画块中,您需要更改要设置动画的视图的一个或多个可动画属性。

如果您希望视图一次显示一个,那么您应该为第一个动画调用延迟值为0的animateWithDuration:delay:options:animations:completion:,并为第二个动画调用第一个动画的持续时间延迟,这样第一个动画将立即开始,第二个动画将在第一个动画结束时开始,依此类推。

您的代码可能如下所示:

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

相关问题