iOS仿网易新闻滚动导航条效果

 更新时间:2018年05月21日 15:45:25   作者:vbirdbest  
这篇文章主要为大家详细介绍了iOS仿网易新闻滚动导航条效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS滚动导航条效果展示的具体代码,供大家参考,具体内容如下

实现效果

效果:选择不同的栏目,下面出现不同的视图,栏目条可以滚动;下面的视图也可以滚动,滚动时上面对应的栏目要选中颜色为红色;

滚动的导航条包括两部分:标题滚动视图(UIScrollView),内容滚动视图(UIScrollView)

实现代码

1.首先实现Main.storyboard

2.创建多个子控制器:头条、科技、汽车、体育、视频、图片、热点

// 头条ViewController, 其它控制器和这个控制器都一样,只是背景颜色不同而已
#import <UIKit/UIKit.h>
@interface TopLineViewController : UIViewController

@end
//----------------------------------------------------------------
#import "TopLineViewController.h"
@interface TopLineViewController ()

@end

@implementation TopLineViewController
- (void)viewDidLoad {
 [super viewDidLoad];

 self.view.backgroundColor = [UIColor blackColor];
}
@end

实现Main.storyboard对应的视图控制器ViewController

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController

@end
//----------------------------------------------------------------
#import "ViewController.h"
#import "TopLineViewController.h"
#import "TechnologyViewController.h"
#import "CarViewController.h"
#import "SportsViewController.h"
#import "VideoViewController.h"
#import "ImageViewController.h"
#import "HotViewController.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;

@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

@property (strong, nonatomic) NSMutableArray *buttons;
@property (strong, nonatomic) UIButton *selectedButton;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 self.navigationItem.title = @"网易新闻";
 // 1. 初始化标题滚动视图上的按钮
 [self initButtonsForButtonScrollView];


}


- (void) initButtonsForButtonScrollView {
 // 初始化子控制器
 [self initChildViewControllers];
 CGFloat buttonWidth = 100;
 CGFloat buttonHeight = 40;
 NSInteger childViewControllerCount = self.childViewControllers.count;
 for (NSInteger i = 0; i < childViewControllerCount; i++) {
  UIViewController *childViewController = self.childViewControllers[i];
  UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
  titleButton.tag = i;
  CGFloat x = i * buttonWidth;
  titleButton.frame = CGRectMake(x, 0, buttonWidth, buttonHeight);
  [titleButton setTitle:childViewController.title forState:UIControlStateNormal];
  [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [titleButton addTarget:self action:@selector(titleButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
  [self.titleScrollView addSubview:titleButton];

  [self.buttons addObject:titleButton];
 }

 self.titleScrollView.contentSize = CGSizeMake(buttonWidth * childViewControllerCount, 0);
 self.titleScrollView.showsHorizontalScrollIndicator = NO;
 self.titleScrollView.bounces = NO;

 self.contentScrollView.contentSize = CGSizeMake(ScreenWidth * childViewControllerCount, 0);
 self.contentScrollView.showsHorizontalScrollIndicator = NO;
 self.contentScrollView.pagingEnabled = YES;
 self.contentScrollView.delegate = self;

 // 禁止额外滚动区域
 self.automaticallyAdjustsScrollViewInsets = NO;

 // 初始化时默认选中第一个
 [self titleButtonOnClick:self.buttons[0]];
}


- (void)titleButtonOnClick:(UIButton *)button {
 // 1. 选中按钮
 [self selectingButton:button];

 // 2. 显示子视图
 [self addViewToContentScrollView:button];
}

- (void)selectingButton:(UIButton *)button {
 [_selectedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 _selectedButton.transform = CGAffineTransformMakeScale(1.0, 1.0);
 [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
 button.transform = CGAffineTransformMakeScale(1.3, 1.3); // 选中字体变大,按钮变大,字体也跟着变大
 _selectedButton = button;

 // 选中按钮时要让选中的按钮居中
 CGFloat offsetX = button.frame.origin.x - ScreenWidth * 0.5;
 CGFloat maxOffsetX = self.titleScrollView.contentSize.width - ScreenWidth;

 if (offsetX < 0) {
  offsetX = 0;
 } else if (offsetX > maxOffsetX) {
  offsetX = maxOffsetX;
 }

 [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

- (void)addViewToContentScrollView:(UIButton *)button {
 NSInteger i = button.tag;
 UIViewController *childViewController = self.childViewControllers[i];
 CGFloat x = i * ScreenWidth;

 // 防止添加多次
 if (childViewController.view.subviews != nil) {
  childViewController.view.frame = CGRectMake(x, 0, ScreenWidth, ScreenHeight);
  [self.contentScrollView addSubview:childViewController.view];
 }
 self.contentScrollView.contentOffset = CGPointMake(x, 0);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {


}

// 滚动结束时,将对应的视图控制器的视图添加到内容滚动视图中
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
 NSInteger i = self.contentScrollView.contentOffset.x / ScreenWidth;
 [self addViewToContentScrollView:_buttons[i]];

 // 内容滚动视图结束后选中对应的标题按钮
 [self selectingButton:_buttons[i]];
}

- (void)initChildViewControllers {
 // 0.头条
 TopLineViewController * topViewController = [[TopLineViewController alloc] init];
 topViewController.title = @"头条";
 [self addChildViewController:topViewController];

 // 1.科技
 TechnologyViewController * technologyViewController = [[TechnologyViewController alloc] init];
 technologyViewController.title = @"科技";
 [self addChildViewController:technologyViewController];

 // 2.汽车
 CarViewController * carViewController = [[CarViewController alloc] init];
 carViewController.title = @"汽车";
 [self addChildViewController:carViewController];

 // 3.体育
 SportsViewController * sportsViewController = [[SportsViewController alloc] init];
 sportsViewController.title = @"体育";
 [self addChildViewController:sportsViewController];

 // 4.视频
 VideoViewController * videoViewController = [[VideoViewController alloc] init];
 videoViewController.title = @"视频";
 [self addChildViewController:videoViewController];

 // 5.图片
 ImageViewController * imageViewController = [[ImageViewController alloc] init];
 imageViewController.title = @"图片";
 [self addChildViewController:imageViewController];

 // 6.热点
 HotViewController * hotViewController = [[HotViewController alloc] init];
 hotViewController.title = @"热点";
 [self addChildViewController:hotViewController];
}

- (NSMutableArray *)buttons {
 if (_buttons == nil) {
  _buttons = [NSMutableArray array];
 }

 return _buttons;
}
@end

以上代码即可实现网易新闻 滚动的导航条, 因该功能可能在其它地方使用,所以最好可以将该功能抽出来,便于其它控制器集成,暂时还没做。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • IOS10.11 无法访问http的问题解决办法

    IOS10.11 无法访问http的问题解决办法

    这篇文章主要介绍了IOS10.11 无法访问http的问题解决办法的相关资料,需要的朋友可以参考下
    2016-12-12
  • iOS 隐私权限和通过openURL实现跳转实例

    iOS 隐私权限和通过openURL实现跳转实例

    这篇文章主要介绍了iOS 隐私权限和通过openURL实现跳转实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • iOS点击推送消息跳到应用指定页面方法

    iOS点击推送消息跳到应用指定页面方法

    现在的推送用的越来越频繁,几乎每个应用都开始用到了。这篇文章主要介绍了iOS点击推送消息跳到应用指定页面方法,有需要的可以了解一下。
    2016-11-11
  • iOS开发微信支付的方法

    iOS开发微信支付的方法

    这篇文章主要为大家详细介绍了iOS开发微信支付的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • iOS-GCD使用详解及实例解析

    iOS-GCD使用详解及实例解析

    这篇文章主要介绍了iOS-GCD使用详解及实例解析的相关资料,需要的朋友可以参考下
    2016-11-11
  • iOS中修改UITextField占位符字体颜色的方法总结

    iOS中修改UITextField占位符字体颜色的方法总结

    这篇文章给大家分享了iOS中修改UITextField占位符字体颜色的三个方法,分别是使用attributedPlaceholder属性、重写drawPlaceholderInRect方法和修改UITextField内部placeholderLaber的颜色,下面我们一起来看看详细的方法介绍。
    2016-09-09
  • 详解iOS多线程GCD问题

    详解iOS多线程GCD问题

    本篇文章主要介绍了iOS多线程GCD问题,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • iOS开发之1行代码实现缓存计算及清除缓存

    iOS开发之1行代码实现缓存计算及清除缓存

    这篇文章主要给大家介绍了关于iOS开发之1行代码实现缓存计算及清除缓存的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • iOS中SQLite的操作方法

    iOS中SQLite的操作方法

    这篇文章主要为大家详细介绍了iOS中SQLite的操作方法,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • iOS画出精美的图表方法示例

    iOS画出精美的图表方法示例

    这篇文章主要给大家介绍了关于iOS如何画出精美的图表的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08

最新评论