动画 [tableView reloadData]
Animate [tableView reloadData]
我有一个 UITableViewCell
可以在点击时展开并添加标签。
Logic:
I added a label in cellForRowAtIndexPath
to the selected cell, and
in didSelectRow...
I reloaded the tableView. In heightForRow...
the
selected cell expands. Hope you get the picture.
在 didSelectRow...
中我必须重新加载数据,而不是 begin/end 更新。我想要做的是 reloadData 并让它动画化。我可以这样做:
[UIView transitionWithView: tableView
duration: 0.40f
options: UIViewAnimationOptionTransitionCrossDissolve
animations: ^(void)
{
[self.tableView reloadData];
}
completion: nil
}];
这样就可以交叉溶解正在变化的细胞。我想要的是 UITableViewRowAnimationTop
,但显然这在过渡选项中是不可能的。有没有办法 reloadData
并使用 UITableViewRowAnimationTop
?
[UITablewView reloadData:]
不可设置动画。但是,您可能想尝试通过重新加载带有动画的所有部分来重新加载 tableView。
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfSections])];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic];
好吧,我撒谎了。它可能是可动画的,但它需要很多关于 QuartzCore Framework 如何在 UI 组件上运行的知识。
@import QuartzCore;
[self.tableView reloadData];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.fillMode = kCAFillModeForwards;
transition.duration = 0.6;
transition.subtype = kCATransitionFromTop;
[self.tableView.layer addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"];
不需要 CoreAnimation 的更简单的解决方案。
Swift 3:
UIView.transition(with: self.view,
duration: 0.15,
options: [.curveEaseInOut, .transitionCrossDissolve],
animations: {
self.tableView.reloadRows(at: self.tableView.indexPathsForVisibleRows!, with: .none)
}, completion: nil)
或者这样:
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [self numberOfSectionsInTableView: self.tableView])];
[self.tableView reloadSections: sections withRowAnimation: UITableViewRowAnimationTop];
就个人而言,我认为 UITableViewRowAnimationFade 看起来好多了。
此 UITableView 扩展适用于 Swift 4:
extension UITableView {
func reloadDataWithAnimation(duration: TimeInterval, options: UIViewAnimationOptions) {
UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 0 }, completion: nil)
self.reloadData()
UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 1 }, completion: nil)
}
}
用法:
tableView.reloadDataWithAnimation(duration: 1.0, options: .curveLinear)
Swift 4 解决方案 QuartzCore
import QuartzCore
tableView.reloadData()
let transition = CATransition()
transition.type = kCATransitionPush
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.fillMode = kCAFillModeForwards
transition.duration = 0.6
transition.subtype = kCATransitionFromBottom
tableView.layer.add(transition, forKey: "UITableViewReloadDataAnimationKey")
Swift 5
tableView.reloadData()
let transition = CATransition()
transition.type = .push
transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
transition.fillMode = .forwards
transition.duration = 0.6
transition.subtype = .fromBottom
我有一个 UITableViewCell
可以在点击时展开并添加标签。
Logic:
I added a label in
cellForRowAtIndexPath
to the selected cell, and indidSelectRow...
I reloaded the tableView. InheightForRow...
the selected cell expands. Hope you get the picture.
在 didSelectRow...
中我必须重新加载数据,而不是 begin/end 更新。我想要做的是 reloadData 并让它动画化。我可以这样做:
[UIView transitionWithView: tableView
duration: 0.40f
options: UIViewAnimationOptionTransitionCrossDissolve
animations: ^(void)
{
[self.tableView reloadData];
}
completion: nil
}];
这样就可以交叉溶解正在变化的细胞。我想要的是 UITableViewRowAnimationTop
,但显然这在过渡选项中是不可能的。有没有办法 reloadData
并使用 UITableViewRowAnimationTop
?
[UITablewView reloadData:]
不可设置动画。但是,您可能想尝试通过重新加载带有动画的所有部分来重新加载 tableView。
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfSections])];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic];
好吧,我撒谎了。它可能是可动画的,但它需要很多关于 QuartzCore Framework 如何在 UI 组件上运行的知识。
@import QuartzCore;
[self.tableView reloadData];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.fillMode = kCAFillModeForwards;
transition.duration = 0.6;
transition.subtype = kCATransitionFromTop;
[self.tableView.layer addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"];
不需要 CoreAnimation 的更简单的解决方案。
Swift 3:
UIView.transition(with: self.view,
duration: 0.15,
options: [.curveEaseInOut, .transitionCrossDissolve],
animations: {
self.tableView.reloadRows(at: self.tableView.indexPathsForVisibleRows!, with: .none)
}, completion: nil)
或者这样:
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [self numberOfSectionsInTableView: self.tableView])];
[self.tableView reloadSections: sections withRowAnimation: UITableViewRowAnimationTop];
就个人而言,我认为 UITableViewRowAnimationFade 看起来好多了。
此 UITableView 扩展适用于 Swift 4:
extension UITableView {
func reloadDataWithAnimation(duration: TimeInterval, options: UIViewAnimationOptions) {
UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 0 }, completion: nil)
self.reloadData()
UIView.animate(withDuration: duration, delay: 0.0, options: options, animations: { self.alpha = 1 }, completion: nil)
}
}
用法:
tableView.reloadDataWithAnimation(duration: 1.0, options: .curveLinear)
Swift 4 解决方案 QuartzCore
import QuartzCore
tableView.reloadData()
let transition = CATransition()
transition.type = kCATransitionPush
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.fillMode = kCAFillModeForwards
transition.duration = 0.6
transition.subtype = kCATransitionFromBottom
tableView.layer.add(transition, forKey: "UITableViewReloadDataAnimationKey")
Swift 5
tableView.reloadData()
let transition = CATransition()
transition.type = .push
transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
transition.fillMode = .forwards
transition.duration = 0.6
transition.subtype = .fromBottom