Dan*_*son 96 iphone animation cocoa-touch uitableview ios
有没有办法既可以指定UITableView行动画的持续时间,也可以在动画完成时获取回调?
我想做的是在动画完成后闪烁滚动指示器.之前做闪光灯不会做任何事情.到目前为止,我的解决方法是延迟半秒(这似乎是默认的动画持续时间),即:
[self.tableView insertRowsAtIndexPaths:newRows
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView performSelector:@selector(flashScrollIndicators)
withObject:nil
afterDelay:0.5];
Run Code Online (Sandbox Code Playgroud)
kar*_*wag 201
刚碰到这个.这是怎么做的:
Objective-C的
[CATransaction begin];
[tableView beginUpdates];
[CATransaction setCompletionBlock: ^{
// Code to be executed upon completion
}];
[tableView insertRowsAtIndexPaths: indexPaths
withRowAnimation: UITableViewRowAnimationAutomatic];
[tableView endUpdates];
[CATransaction commit];
Run Code Online (Sandbox Code Playgroud)
迅速
CATransaction.begin()
tableView.beginUpdates()
CATransaction.setCompletionBlock {
// Code to be executed upon completion
}
tableView.insertRowsAtIndexPaths(indexArray, withRowAnimation: .Top)
tableView.endUpdates()
CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)
小智 38
扩展karwag的精彩答案,请注意,在iOS 7上,使用UIView Animation围绕CATransaction提供对表动画持续时间的控制.
[UIView beginAnimations:@"myAnimationId" context:nil];
[UIView setAnimationDuration:10.0]; // Set duration here
[CATransaction begin];
[CATransaction setCompletionBlock:^{
NSLog(@"Complete!");
}];
[myTable beginUpdates];
// my table changes
[myTable endUpdates];
[CATransaction commit];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
UIView动画的持续时间对iOS 6没有影响.也许iOS 7表动画在UIView级别实现不同.
Fré*_*dda 26
这是一个有用的伎俩!我写了一个UITableView扩展,以避免一直编写CATransaction的东西.
import UIKit
extension UITableView {
/// Perform a series of method calls that insert, delete, or select rows and sections of the table view.
/// This is equivalent to a beginUpdates() / endUpdates() sequence,
/// with a completion closure when the animation is finished.
/// Parameter update: the update operation to perform on the tableView.
/// Parameter completion: the completion closure to be executed when the animation is completed.
func performUpdate(_ update: ()->Void, completion: (()->Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
// Table View update on row / section
beginUpdates()
update()
endUpdates()
CATransaction.commit()
}
}
Run Code Online (Sandbox Code Playgroud)
这样使用:
// Insert in the tableView the section we just added in sections
self.tableView.performUpdate({
self.tableView.insertSections([newSectionIndex], with: UITableViewRowAnimation.top)
}, completion: {
// Scroll to next section
let nextSectionIndexPath = IndexPath(row: 0, section: newSectionIndex)
self.tableView.scrollToRow(at: nextSectionIndexPath, at: .top, animated: true)
})
Run Code Online (Sandbox Code Playgroud)
小智 25
缩短布伦特的好答案,对于至少iOS 7,你可以将这一切简洁地包装在[UIView animateWithDuration:delay:options:animations:completion:]调用中:
[UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
} completion:^(BOOL finished) {
// completion code
}];
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法从EaseInOut以外的任何东西覆盖默认动画曲线.
pri*_*ris 23
这是karwag答案的Swift版本
CATransaction.begin()
tableView.beginUpdates()
CATransaction.setCompletionBlock { () -> Void in
// your code here
}
tableView.insertRowsAtIndexPaths(indexArray, withRowAnimation: .Top)
tableView.endUpdates()
CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)
对我来说,我需要一个collectionView.我做了一个简单的扩展来解决这个问题:
extension UICollectionView {
func reloadSections(sections: NSIndexSet, completion: () -> Void){
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.reloadSections(sections)
CATransaction.commit()
}
}
Run Code Online (Sandbox Code Playgroud)
- (void)performBatchUpdates:(void (^)(void))updates
completion:(void (^)(BOOL finished))completion;
Run Code Online (Sandbox Code Playgroud)
在更新闭包中,您放置与 beginUpdates()/endUpdates 部分中相同的代码。并且完成是在所有动画之后执行的。