0

下面的函数应该 A. 使视图变大 B. 等待 5 秒 C. 再次缩小它。问题是 A 立即发生,而不是在两秒钟内发生。

- (void) showAndHide {
CGRect r = self.frame;
float right = r.origin.x + r.size.width, h = r.size.height, y = r.origin.y;

[UIView animateWithDuration:2
                      delay:0
                    options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     self.frame = CGRectMake(right - 400, y, 400, h);
                     [UIView animateWithDuration:2
                                           delay:5
                                         options: UIViewAnimationOptionOverrideInheritedCurve |
                      UIViewAnimationOptionCurveLinear |
                      UIViewAnimationOptionOverrideInheritedDuration
                                      animations:^{
                                          self.frame = CGRectMake(right - 40, y, 40, h);
                                      }
                                      completion:nil];

                 }
                 completion:nil];

}

这可能是什么原因造成的?提前致谢!

4

1 回答 1

0

将第二个动画放在第一个动画的完成处理程序中:

- (void) showAndHide {
CGRect r = self.frame;
float right = r.origin.x + r.size.width, h = r.size.height, y = r.origin.y;

[UIView animateWithDuration:2
                      delay:0
                    options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     self.frame = CGRectMake(right - 400, y, 400, h);
                 }
                 completion:^{
                 [UIView animateWithDuration:2
                                       delay:5
                                     options: UIViewAnimationOptionOverrideInheritedCurve | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionOverrideInheritedDuration
                                      animations:^{
                                          self.frame = CGRectMake(right - 40, y, 40, h);
                                      }
                                      completion:NULL];
                 }];
}

您的方法的问题是,两个动画同时排队。如果您想坚持使用您的代码,我认为添加UIViewAnimationOptionBeginFromCurrentState到第二个动画的选项也可以解决您的问题。不过不太确定。

于 2012-10-29T16:19:27.433 回答