简体   繁体   English

动画完成后执行任务(在iOS中)

[英]Perform a task (in iOS) after animation has finished

So I have this line of code: 所以我有这行代码:

[tableView setContentOffset:point animated:YES];

and I want to run another piece of code after the animation ends. 我想在动画结束后再运行一段代码。 My attempt was to throw the animating code (setContentOffset) in a separate method and calling it using: 我的尝试是将动画代码(setContentOffset)放在单独的方法中,并使用以下方法进行调用:

[self performSelectorOnMainThread:@selector(scrollMethod:) withObject:sender waitUntilDone:YES];

The problem is that the method returns immediately, not after the animation is finished, even though waitUntilDone is YES, but apparently that is how animation works. 问题在于,即使waitUntilDone为YES,该方法也将立即返回,而不是在动画完成之后返回,但是显然这是动画的工作方式。

I know that I can use thread waiting but it is not clean, so I will only use it as a last resort. 我知道我可以使用线程等待,但它不是干净的,因此我只会将其用作最后的选择。 (Maybe I would use this if I know the time it takes the scrolling animation to happen.) (如果我知道滚动动画发生的时间,也许可以使用它。)

Any ideas on how to go about this are welcome. 任何关于如何做到这一点的想法都值得欢迎。

(PS The scenario is this: I am showing a popover, which is displayed perfectly when there is no keyboard, however, if the keyboard is visible, the popover's height shrinks which sometimes reduces it to almost border only. So just before showing the popover, I want to scroll the view upwards so that the popover never pops in the keyboard.) (PS的情况是这样的:我正在显示一个弹出窗口,当没有键盘时,它会完美显示,但是,如果可见键盘,则弹出窗口的高度会缩小,有时会使其几乎变成边框。因此,在显示弹出窗口之前,我想向上滚动视图,以使弹出窗口永远不会在键盘上弹出。)

UITableView inherits setContentOffset:animated: from its superclass, UIScrollView. UITableView从其超类UIScrollView继承setContentOffset:animated:。 Check out the scrollViewDidEndScrollingAnimation: method of UIScrollViewDelegate. 签出UIScrollViewDelegate的scrollViewDidEndScrollingAnimation:方法。

Try to use this method: 尝试使用此方法:

[UIView animateWithDuration:0.6f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     // Do your animations here.
                 }
                 completion:^(BOOL finished){
                     if (finished) {
                         // Do your method here after your animation.
                     }
                 }];

For a scrollView, tableView or collectionView if you do something like this: 对于scrollView,tableView或collectionView,如果执行以下操作:

[self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x+260.0,
                                                      self.collectionView.contentOffset.y)
                                 animated:YES];

then you'll get back a: 然后您会得到一个:

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

when the scroll finishes. 滚动完成时。

You do NOT get this callback if the user moves the view. 如果用户移动视图,则不会收到此回调。

You can create animations and tell them directly to perform a block after they're done. 您可以创建动画,并告诉它们直接在完成后执行块。

Here's an alternative that may play nicer with the UITableView's animations. 这是一个可以与UITableView的动画更好玩的替代方法。

[UIView beginAnimations:nil context:sender];
[UIView setDelegate:self];
[UIView setDidStopSelector:@selector(scrollMethod:)];
[tableView setContentOffset:point];
[UIView commitAnimations];

And make sure to implement your scrollMethod: with this signature: 并确保使用以下签名实现您的scrollMethod: ::

- (void)scrollMethod:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

You can use the context to know which sender you have. 您可以使用上下文了解您拥有哪个sender Read the UIView docs for more on UIView animations. 阅读UIView文档,以获取有关UIView动画的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM