zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS开发- 点击通知栏回到顶部的动画效果

ios动画开发 效果 点击 通知 顶部 回到
2023-09-11 14:21:22 时间

这里写图片描述
你可能经常会用到点击通知栏回到顶部的功能,那这么动画效果怎么实现呢?一开始博主也被误导了,用基础动画来写,麻烦不说,效果还不好,后来才知道原来系统提供了相应的方法,网上给出了好几种用法,还有种错误的用法,博主这里再总结下:

1.滚动到顶部

[_tableView setContentOffset:CGPointMake(0,0) animated:YES];

2.滚动到底部

CGPoint offset = CGPointMake(0,_tableView.contentSize.height - _tableView.frame.size.height);
[_tableView setContentOffset:offset animated:YES];

3.利用scrollToRowAtIndexPath:
可以滚动到tableView的任何位置


NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:6];
[_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];

这里注意,不能使用:

    NSIndexPath *path = [NSIndexPath indexPathWithIndex:0];  //0,1...都不行

即使你没有分组,一点就崩,用1报错如下:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: section (1) beyond bounds (1).'

看着是因为没那么多分组的原因,可是当你在多分组中用了,会报这样的错:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView.  Index paths passed to table view must contain exactly two indices specifying the section and row.  Please use the category on NSIndexPath in UITableView.h if possible.'

说是需要包含section and row,由此我们可以摸清楚它的用法,请看第三条开始的代码。