最近发现很多app都支持点击首页返回到屏幕顶部的功能,所以今天自己研究了一下,本着分享的心态跟大家讨论一下.
step 1 : 自定义一个UITabBarController, 给这个tabbarController加上一个属性lastSelectedIndex,系统的UITabBarController是没有这个属性的,重写父类的setSelectedIndex和didSelectItem方法中记录最后选中的位置;
step 2 : 在你的首页控制器的viewDidLoad方法中设置管理自己的tabbarController的delegate为self.遵守UITabBarControllerDelegate协议,实现- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController方法,在里面进行判断tabBarController的lastSelectedIndex的值是否和当前控制器的tabbarController的selectedIndex的值一样,如果一样就将scrollView的contentOffSet赋值为CGPointZero,如果不一样就不进行任何操作,这样就避免了从其它控制器切换回首页时也回到屏幕顶部,这样可能不是用户想要的.
一开始我使用双击手势做的,不过感觉那个效果没有这样好,就不说了