UIViewAnimation 立即播放,延迟被忽略

Posted

技术标签:

【中文标题】UIViewAnimation 立即播放,延迟被忽略【英文标题】:UIViewAnimation plays instantly, delay is ignored 【发布时间】:2012-11-15 15:35:59 【问题描述】:

我有一个简单的 UIViewAnimation,一旦我的视图被加载,它就会被调用。 但是,无论我设置什么作为延迟值,它都会被忽略并且动画会立即播放。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:100.0];
[UIView setAnimationDelay:2.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(faceRight:finished:context:)];
_chooseLabelContainer.center = CGPointMake(originalCenter.x, 0 - _chooseLabelContainer.frame.size.height);
[UIView commitAnimations];

更新:作为一项测试,我已经处理了这种情况,并且在非延迟动画下方会立即发生,但是调度队列中的动画会按预期超时! UIView* objectToAnimate = self.hudController->_chooseLabelContainer;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_current_queue(), ^
    [UIView animateWithDuration:5.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^
        NSLog(@"Start Dispatch %@", NSStringFromCGPoint( objectToAnimate.center ) );
        objectToAnimate.center = CGPointMake(objectToAnimate.center.x, objectToAnimate.center.y+90);
    completion:^(BOOL done)
        NSLog(@"Done Dispatch");
    ];


);

[UIView animateWithDuration:5.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^
    NSLog(@"Start %@", NSStringFromCGPoint( objectToAnimate.center ) );
    objectToAnimate.center = CGPointMake( objectToAnimate.center.x, objectToAnimate.center.y+30);
completion:^(BOOL done)
    NSLog(@"Done");
];

【问题讨论】:

【参考方案1】:

这可能无法解决您的问题,但从 ios 4 开始,Apple 建议您使用 UIView 动画块:

[UIView animateWithDuration:100.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^
    _chooseLabelContainer.frame = CGPointMake(originalCenter.x, 0 - _chooseLabelContainer.frame.size.height);
completion:^(BOOL done)
    [self faceRight:finished:context]; //will of course generate errors since I don't know what arguments to pass.
];

【讨论】:

出现同样的问题 :( - 我本来就有这个问题,但在调试过程中只是切换到“旧方式”。 这个视图控制器转换后,在viewDidAppear函数上 另外需要注意的是,动画没有动画,它会立即将对象放置到位。为了调试,我还将上面的代码放在触摸处理程序中 - 每当我点击时,指定的视图就会跳转到新位置 @1dayitwillmake 最初您的代码包含_chooseLabelContainer.frame,现在我看到您已将其编辑为_chooseLabelContainer.center。如果您仍在代码中使用 .frame,则向 CGRect 发送 CGPoint 可能会导致此问题。 是的,这是我粘贴之前 cmd+z 的错字。如果重要的话,我有一个 GLView 与这个视图的背后同时存在。

以上是关于UIViewAnimation 立即播放,延迟被忽略的主要内容,如果未能解决你的问题,请参考以下文章

NAudio - 如何在结束后立即播放音频而不会有任何延迟?

AVPlayer replaceCurrentItemWithPlayerItem 打破 UIViewAnimation

延迟 Youtube 视频自动播放

AVPlayer 不会在 iOS 10 上立即播放视频,而仅播放音频

dispatch_after 和 [UIView animation: duration] 立即发生(但不应该)

在 MPMoviePlayerController 上调用 play 后,电影播放有一些延迟