为什么“+ (void)setAnimationStartDate:(NSDate *)startDate;”不行

Why does "+ (void)setAnimationStartDate:(NSDate *)startDate;" not work

我正在研究UIView Animation,我在使用“+ (void)setAnimationStartDate:(NSDate *)startDate;”时发现在 beginAnimations:context:commitAnimations 方法之间,它不起作用

[UIView beginAnimations:@"demo" context:nil];
[UIView setAnimationDuration:3.0f];
[UIView setAnimationStartDate:[NSDate date]];
// do something
[UIView commitAnimations];

因为方法默认是现在(NSDate日期),我什至尝试“[NSDate dateWithTimeIntervalSinceNow:5]”,它也不起作用。

如果我不使用该方法,它会像往常一样工作。谢谢。

您正在调用 setAnimationStartDate 这意味着您正在为此动画提供开始时间。所以,在那个时间到来之前,它不会开始。

Apple Documentation所述,

Call this method between the beginAnimations:context: and commitAnimations methods to specify the start time for that set of animations. And call this method prior to changing the animatable properties of your views. (Do not call this method in conjunction with a block-based animation.) If you do not call this method, the start time is set to the value returned by the CFAbsoluteTimeGetCurrent function, which begins the animations as soon as possible.

在 iOS 4.0 及更高版本中不鼓励使用此方法。您应该使用基于块的动画方法来指定您的动画。

有关详细信息,请参阅文档。

希望这会有所帮助:)

看看that就知道了。我认为您只需要使用 class UIView.

的方法 animateWithDuration:delay:options:animations:completion: 重构您的代码

您可以使用 setAnimationDelay: 实现相同的功能。有效!!

[UIView setAnimationDelay:5];

Apple Documentation 上查看更多详细信息。不确定为什么 setAnimationStartDate: 不起作用,它应该起作用,因为上面的文档中提到了这一点。