下面这个视图(多视图滑动点击切换)在很多App都有用到.使用封装过的view,外部调用一个API就可以实现,十分的方便。
git代码:https://github.com/HelloYeah/HYTabbarView.
//添加一个子控制器
代码如下 | 复制代码 |
-(void)addSubItemWithViewController:(UIViewController *)viewController; |
1.外部传一个控制器
代码如下 | 复制代码 |
-(void)addSubItemWithViewController:(UIViewController *)viewController{ UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom]; [self.tabbar addSubview:btn]; [self setupBtn:btnwithTitle:viewController.title]; [btn addTarget:self action:@selector(itemSelected:)forControlEvents:UIControlEventTouchUpInside]; [self.subViewControllers addObject:viewController]; } |
2.KVO监听当前选中View的序号值
代码如下 | 复制代码 |
NSInteger min = HYColumn / 2 ; if (_selectedIndex = self.titles.count - min) { UIButton * tempBtn = self.titles[self.titles.count - min - 1]; CGFloat btnX = (HYColumn % 2 ) ? tempBtn.center.x : (tempBtn.center.x + btn.frame.size.width * 0.5) ; CGFloat offsetX = _tabbar.center.x - btnX; [UIView animateWithDuration:0.25 animations:^{ _tabbar.contentOffset = CGPointMake(- offsetX, 0); }]; }else if (_selectedIndex > min && _selectedIndex < self.titles.count - min && self.titles.count > HYColumn ) { CGFloat btnX = (HYColumn % 2 ) ? btn.center.x : (btn.center.x - btn.frame.size.width * 0.5) ; CGFloat offsetX = _tabbar.center.x - btnX; [UIView animateWithDuration:0.25 animations:^{ _tabbar.contentOffset = CGPointMake( - offsetX, 0); }]; } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } |
时间: 2024-09-29 15:08:01