使用 UIView animateWithDuration:延迟:Xcode

Posted

技术标签:

【中文标题】使用 UIView animateWithDuration:延迟:Xcode【英文标题】:using UIView animateWithDuration: delay: Xcode 【发布时间】:2014-02-04 08:20:43 【问题描述】:

我正在尝试使用

[UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionCurveEaseIn animations:^
 completion:^ (BOOL completed)          ];

但是,无论我为延迟输入多少数字,都没有延迟。 当触摸另一个按钮时,我试图让一系列按钮一个接一个地淡入。所以我使用了一系列具有不同延迟长度的 UIView 动画,但无论我输入什么时间延迟,它们都会同时显示。有人有什么建议吗?

【问题讨论】:

您能在上下文中向我们展示更多您的代码吗?上面的 sn-p 有一个空的动画块... 【参考方案1】:

首先animateWithDurationdelay的值是float值,应该是1.02.0

意见:如果你的代码在ViewDidLoad方法中,试着把它移到一个单独的函数中

其次,如果您无法在此上下文中执行此操作,请跳到另一个,或四处走走,例如使用

performSelector:withObject:afterDelay

另一种方法是将delay设置为0,并将后续UIView动画块放在前面animateWithDuration语句的completion块中

 [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^(void) 
        //Leave it empty
                 
                 completion:^(BOOL finished)

// Your code goes here
   [UIView animateWithDuration:1.0 delay:0.0 options:
   UIViewAnimationOptionCurveEaseIn animations:^
         completion:^ (BOOL completed) ];
    ];

PS:某些属性不支持动画,例如在动画块中设置文本,因此延迟结束后,动画开始立即更改文本,因为该更改不可动画.

在文本动画的情况下,做你想做的,改变完成块中的文本如下:

[UIView animateWithDuration:1.0
                      delay:2.0
                    options: UIViewAnimationOptionTransitionCrossDissolve
                 animations:nil
                 completion:^
                     lbl.text =  @"My text" ;
];

另外,backgroundColorUIView不是动画。而是使用视图的layerbackgroundColor 属性:

[[controller layer] setBackgroundColor:[[UIColor anyColor] CGColor]];

【讨论】:

非常感谢您的帮助【参考方案2】:

或者您可以在viewWillAppear 编码。

- (void)viewWillAppear:(BOOL)animated 
    [UIView animateWithDuration:1.0
                          delay:2.0
                        options:UIViewAnimationOptionTransitionCrossDissolve
                     animations:^
                         //yourAnimation
                      completion:^(BOOL finished)
                         NSLog(@"Animation is finished");
                     ];

【讨论】:

惊人的答案!立即接受!【参考方案3】:

在您实施之前,请阅读它 持续时间:动画的总持续时间,以秒为单位。如果您指定负值或 0,则进行更改时不会对其进行动画处理。 delay :开始动画之前等待的时间量(以秒为单位)。指定值 0 以立即开始动画。 选项

-对上述两个参数使用浮点值 - 如果你在单独的例程中定义你的动画操作,那就太棒了

将其称为适当的位置,例如 ViewDidAppear/viewWillapear。

Use function like this & set your UI Element properties like this:
 [UIView animateWithDuration:0.6
                          delay:0.3
                        options: UIViewAnimationOptionCurveEaseInOut
                     animations:^
                         [self.view layoutIfNeeded];
                         [yourviews setTintColor:[UIColor whiteColor]];
                         self.view.backgroundColor = [UIColor greenColor];
                     
                     completion:^(BOOL finished)

                         // You next action to perform after completion;
                     ];

编程愉快,希望对你有所帮助。

【讨论】:

以上是关于使用 UIView animateWithDuration:延迟:Xcode的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 中使用 UIView 动画显示 UIView,如 UIAction 表?

如何使用自动布局将 UIView 定位在其他两个 UIView 的中心?

UIView/Drawer 使用平移/滑动手势

使用 UIPanGestureRecognizer 移动和缩放 UIView

使用带有修复底部 UIView 的自动布局调整 UIView 的大小

如何移动 UIView 并使用 Swift 以编程方式放置新的 UIView?