源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

浅析iOS多视图滑动点击切换的集成

  • 时间:2022-05-05 08:42 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:浅析iOS多视图滑动点击切换的集成
[b]前言[/b] 多视图滑动点击切换这个视图在很多App都有用到,我对这个View进行了封装,外界只需要调用一个接口,就能实现这个效果,使用方法和系统的tabbarController很相似。 [b]外界只需要调用下面这个接口即可集成.[/b]
/**
 * 添加一个子控制器
 */
- (void)addSubItemWithViewController:(UIViewController *)viewController;
[b]HYTabbarView效果图如下[/b] [img]http://files.jb51.net/file_images/article/201608/2016812102536345.gif?2016712102639[/img] HYTabbarView可灵活配置一屏宽显示多少个标题,以及标题栏的高度,具体看项目需求
#define HYTabbarViewHeight 49 //顶部标签条的高度
#define HYColumn 4  //一屏幕宽显示4个标题
[b]实现思路详解[/b]      1、界面分析:分为上下部分,顶部[code]UIScrollView[/code],底部[code]UICollectionView[/code].再实现两部分的联动即可实现 (底部视图相对复杂,占用内存大,底部用[code]UICollectionView[/code]实现会比用[code]UIScrollView[/code]性能好很多)      2、每一个标题对应一个View视图,View视图交由相应的控制器来管理,代码结构十分清晰.做到不同View上的业务逻辑高聚合.也不会产生耦合性      3、上下两部分的联动,这里是同过KVO实现的,监听当前的[code]selectedIndex[/code],底部视图滚动时,修改[code]selectedIndex[/code]的值.在KVO监听的回调方法里让标题居中.      4、其他细节相对简单,大家不看代码都知道如何处理,比如:点击顶部标题,设置按钮选中,切换到对应的[code]CollectionCell[/code]等 [b]UI结构示意图[/b] [img]http://files.jb51.net/file_images/article/201608/2016812102808077.png?2016712102943[/img] [b]代码片段:[/b] [b]1.外界传个控制器和一个标题,添加一个栏目[/b]
//外界传个控制器,添加一个栏目
- (void)addSubItemWithViewController:(UIViewController *)viewController{ 
  
 UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
 [self.tabbar addSubview:btn];
 [self setupBtn:btn withTitle:viewController.title];
 [btn addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
 [self.subViewControllers addObject:viewController];
}
[b]2.KVO监听当前选中View的序号值[/b]
//viewDidLoad中添加观察者
[self addObserver:self forKeyPath:@"selectedIndex" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew context:@"scrollToNextItem"];
 
//让标题按钮居中算法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
   
 if (context == @"scrollToNextItem") {
  self.prevSelectedIndex = [change[@"old"] integerValue];
  if (self.prevSelectedIndex == self.selectedIndex) {
   return;
  }
  //设置按钮选中
  [self itemSelectedIndex:self.selectedIndex];
  UIButton * btn = self.titles[self.selectedIndex];
  
  //让选中按钮居中
  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];
 }
} 
[b]控制器代码如下[/b] 使用方法类似系统的[code]UITabbarController[/code],外界只需直接传入控制器.
- (void)viewDidLoad {
 [super viewDidLoad];
 [self.view addSubview:self.tabbarView];
}//懒加载- (HYTabbarView *)tabbarView{ if (!_tabbarView) {
  _tabbarView = ({
   HYTabbarView * tabbar = [[HYTabbarView alloc]initWithFrame:CGRectMake(0,30,[UIScreen mainScreen].bounds.size.width,600)];   
   for (NSInteger i = 0; i< 10; i ++) {    
    UIViewController * vc = [[UIViewController alloc]init];
    vc.title = [NSString stringWithFormat:@"第%ld个",i+1];
    [tabbar addSubItemWithViewController:vc];
   }
   tabbar;
  });
 } return _tabbarView;
}
[b]总结[/b] 以上就是iOS多视图滑动点击切换的集成的全部内容,希望对大家开发IOS的时候能有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部