码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——实用技术OC片&点击状态栏回到顶部

时间:2015-09-02 07:03:41      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

点击状态栏回到顶部

 

 经常我们在使用一个App的时候,比如QQ,微信等流行App都会有一个很常见的功能,就是当我们刷新了很多节目的时候,先立刻回到顶部只需要轻轻点一下状态栏就可以(当然这种方法不是谁都知道的,因为app没有提示),也有的会在屏幕的右下角或者某个位置放置一个按钮实现点击按钮一样可以回到顶部,那样实现虽然可以,也不是很麻烦,但是其实系统已经为我们提供了一个很好的自带的功能,我们为什么不用呢?
 
但是最近在自己视线这个功能的时候遇到了一个小小的问题,那就拿出来分享一下,当然前面也有说到类似的问题:http://www.cnblogs.com/iCocos/p/4733430.html
 

@property(nonatomic) BOOL  scrollsToTop 这个属性在起作用 默认是YES

  • // When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
  • // On iPhone, we execute this gesture only if there‘s one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
 
只有当一个主控制器有一个scrollview 并把这个属性设置为yes,其他的scrollview.scrollsToTop = NO 这样才会响应这个事件,原理很简单,如果有2个scrollview,系统根本不知道你需要哪个滚动到最上面 切记!
 
 
比如说:有时候我们会在一个UIView里面放多个UITableView或者ScrollView。

如 果我们在IB里面加入这些UITableView,通常我们也会把delegate和datasource也关联到同一个ViewController 里面。这就是问题所在了。把UITableView里面的scrollToTop方法设为YES(默认是YES)就能使得该UITableView在点击 顶部状态栏的时候把table滚到顶部。但是多个table同时关联到controller的时候,这个方法就失效了。

 
解决的方法很简单,不要让这些UITableView同时visible,一个时间内只让其中一个visible就行了。可以设置
  • talbeView1.scrollsToTop = NO;
  • tableView2.scrollerToTop = YES;
 
或者
  • talbeView1.hidden = NO;
  • tableView2.hidden = YES;
 
------------------或者你还可以这样实现--------------------
 
在一个scrollview中横向有多个tableview,点击状态栏,tableview要返回到顶部
要将所有scrollview 以及tableview 的scrollToTop设置为NO。显示的tableview的scrollToTop设置为TRUE。
 

@property(nonatomic,strong) NSMutableArray *tdoclist;

//设置一个数组,存储tableview

 1 self.tdoclist=[NSMutableArray arrayWithCapacity:10];
 2 
 3     for (int i=0; i<[self.category count]+1; i++) {
 4 
 5         [[[CommenData alloc]init] setCategoryIndex:[self getCategoryIndex:i]];
 6 
 7         if (i==0) {
 8 
 9             self.doctable=[self.storyboard instantiateViewControllerWithIdentifier:@"docview"];
10 
11             self.doctable.view.frame=CGRectMake(r.size.width*i, 0.0, r.size.width, r.size.height-40);
12 
13             [self addChildViewController:self.doctable];
14 
15             self.doctable.tableView.scrollsToTop=YES;
16 
17             [self.tdoclist addObject:self.doctable];
18 
19             [self.tableScroll addSubview:self.doctable.view];
20 
21         }else{
22 
23             self.tdoctable=[self.storyboard instantiateViewControllerWithIdentifier:@"tdocview"];
24 
25             self.tdoctable.view.frame=CGRectMake(r.size.width*i, 0.0, r.size.width, r.size.height-40);
26 
27             [self addChildViewController:self.tdoctable];
28 
29             self.tdoctable.tableView.scrollsToTop=NO;
30 
31             [self.tdoclist addObject:self.tdoctable];
32 
33             [self.tableScroll addSubview:self.tdoctable.view];
34 
35         }
36 
37     }

 

//左右滑动页面切换tableview时切换 tableview的scrollToTop属性

 1 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
 2 
 3     //加载之后的一页
 4 
 5     if (scrollView==self.tableScroll) {
 6 
 7                 CGFloat pageWidth=scrollView.frame.size.width;
 8 
 9         int page=floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
10 
11         if (page!=indexPage) {
12 
13             //可以继续滚动
14 
15             if (indexPage<[self.category count]+1) {
16 
17                 indexPage=page;
18 
19                 [self.scrollnavi setSelectedIndex:indexPage];
20 
21                 for (int i=0; i<self.tdoclist.count; i++) {
22 
23                     TDocTableViewController *tdocs = self.tdoclist[i] ;
24 
25                     if (i==indexPage) {
26 
27                         tdocs.tableView.scrollsToTop=YES;
28 
29                     }else{
30 
31                         tdocs.tableView.scrollsToTop=NO;
32 
33                     }
34 
35                 }
36 
37                 //若直接用self.tdoc 只会刷新最后一个页面
38 
39                  TDocTableViewController *tdoc = self.tdoclist[indexPage] ;
40 
41                 [tdoc refreshData];
42 
43             }
44 
45         }
46 
47     }
48 
49 }

 

iOS开发——实用技术OC片&点击状态栏回到顶部

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4777497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!