仿网易新闻导航栏(可封装)

新建一个ViewController,并签UIScrollViewDelegate协议

-(void)addSubView:(UIViewController *)currentController andSubview:(NSMutableArray *)controller;
-(void)addImageView:(UIImageView *)imageView;
- (void)addParentController:(UIViewController *)viewController;

.m如下

#import "SCNavigation.h"
#define UIColorWithRGBA(r,g,b,a)        [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
@interface SCNavigation ()<UIScrollViewDelegate>

@property(nonatomic, strong)UIScrollView *content;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)UIScrollView *banner;
@property(assign, nonatomic)CGFloat oldSize;
@property(assign, nonatomic)CGFloat newSize;
@property(nonatomic, strong)UIView *background;
@property(retain, nonatomic)NSMutableArray *array;
@end

@implementation SCNavigation
- (id)initWithParentViewController:(UIViewController *)viewController
{
    self = [super init];
    if (self)
    {
        [self addParentController:viewController];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBarHidden = NO;

}
-(void)addView{
    self.banner = [[UIScrollView alloc]initWithFrame:CGRectMake(45, 13, 330, 40)];
    self.banner.contentSize = CGSizeMake(20+65*self.array.count, 0);
    //    banner.backgroundColor = [UIColor greenColor];
    self.banner.showsHorizontalScrollIndicator = NO;
    [self.background addSubview:self.banner];

    }
-(void)addSubView:(UIViewController *)currentController andSubview:(NSMutableArray *)controller{
    self.background = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 64)];
    //    background.backgroundColor = [UIColor blueColor];
    self.background.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.background.layer.shadowOffset = CGSizeMake(0, 4);//shadowOffset阴影偏移,x向右偏移4,y向下偏移4,默认(0, -3),这个跟shadowRadius配合使用
    self.background.layer.shadowOpacity = 0.8;//阴影透明度,默认0
    self.background.layer.shadowRadius = 3;//阴影半径,默认3
    self.background.layer.borderColor = [UIColor lightGrayColor].CGColor;
    currentController.navigationItem.titleView = self.background;
    self.array = [NSMutableArray arrayWithArray:controller];
    //    NSMutableArray *arra =[NSMutableArray arrayWithObjects:@"新闻", @"体育", @"时尚", @"最新", @"动态", @"国内", nil];
    NSMutableArray *array1 = [NSMutableArray array];
    [self addView];

    for (UIViewController *temp in controller) {
        NSString *title = temp.title;
        [array1 addObject:title];
    }
    for (int i = 0; i<array1.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(20+65*i, 0, 40, 40);
        //        button.backgroundColor = [UIColor redColor];
        [button setTitle:[array1 objectAtIndex:i] forState:UIControlStateNormal];
        button.tag = 10000+i;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAciton:) forControlEvents:UIControlEventTouchUpInside];
        [self.banner addSubview:button];
    }

    self.content = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 603)];
    self.content.contentSize = CGSizeMake(375*self.array.count, 0);
    self.content.backgroundColor =[UIColor yellowColor];
    self.content.pagingEnabled = YES;
    self.content.bounces = NO;
    self.content.delegate =self;
    self.content.showsHorizontalScrollIndicator = NO;
    for (int i = 0 ; i<self.array.count; i++) {
        UIViewController *first = [self.array objectAtIndex:i];
        first.view.frame = CGRectMake(0+375*i, 0, self.view.frame.size.width, self.view.frame.size.height);
        [self.content addSubview:first.view];
        [self addChildViewController:first];

    }
    self.automaticallyAdjustsScrollViewInsets = NO;
    //    banner.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.content];
    self.line = [[UIView alloc] initWithFrame:CGRectMake(20.0f,39, 40, 1.0)];
    self.line.backgroundColor = UIColorWithRGBA(20.0f, 80.0f, 200.0f, 0.7f);
    [self.banner addSubview:self.line];

}
- (void)addParentController:(UIViewController *)viewController
{
    // Close UIScrollView characteristic on IOS7 and later
    if ([viewController respondsToSelector:@selector(edgesForExtendedLayout)])
    {
        viewController.edgesForExtendedLayout = UIRectEdgeNone;
    }

    [viewController addChildViewController:self];
    [viewController.view addSubview:self.view];
    NSLog(@"nav= %@", self.navigationController);
}

-(void)buttonAciton:(UIButton *)button{
    NSInteger count = button.tag-10000;
    [self.content setContentOffset:CGPointMake(375*count, 0) animated:YES];
    [UIView animateWithDuration:0.2f animations:^{
        self.line.frame = CGRectMake(20+65*count, 39, 45 - 4.0f, 2);
    }];

}
-(void)addImageView:(NSString *)imageView{

    UIImageView *iamge = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageView]];
    iamge.frame = CGRectMake(5, 15, 40, 40);
    [self.background addSubview:iamge];

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.content) {
        CGFloat size = scrollView.contentOffset.x/5.76923;
        [UIView animateWithDuration:0.2f animations:^{
            self.line.frame = CGRectMake(20+size, 39, 45 - 4.0f, 2);
        }];
    }

}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    self.oldSize = scrollView.contentOffset.x;

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.newSize = scrollView.contentOffset.x;
    CGFloat size = (scrollView.contentOffset.x-375*(3)) /5.76;
    CGFloat size1 = (scrollView.contentOffset.x-375) /5.76;

    if (scrollView==self.content&&scrollView.contentOffset.x>375*(3)) {
        [self.banner setContentOffset:CGPointMake((20+size), 0) animated:YES];
    }

    if (self.newSize<self.oldSize&&scrollView.contentOffset.x<375*(2)&&scrollView.contentOffset.x>0) {
        [self.banner setContentOffset:CGPointMake((20+size1), 0) animated:YES];

    }
}

//用法如图需注意ARC与MRC问题

时间: 2025-01-03 17:43:47

仿网易新闻导航栏(可封装)的相关文章

仿网易新闻导航栏PagerSlidingTabStrip源码分析

转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!   前言 最近工作比较忙,所以现在才更新博文,对不住大家了~!言归正传,我们来说说这个PagerSlidingTabStrip,它是配合ViewPager使用的导航栏,网易新闻就是用的这个导航,我们仔细观察这个导航栏不仅他是跟着ViewPager滑动而滑动,而且指示器还会随着标题的长度而动态的变化长度. · 下载地址: Github:https://github.com/astuet

ym——Android仿网易新闻导航栏PagerSlidingTabStrip源码分析

转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 前言 最近工作比较忙,所以现在才更新博文,对不住大家了~!言归正传,我们来说说这个PagerSlidingTabStrip,它是配合ViewPager使用的导航栏,网易新闻就是用的这个导航,我们仔细观察这个导航栏不仅他是跟着ViewPager滑动而滑动,而且指示器还会随着标题的长度而动态的变化长度,还可以改变多种样式哦~! · 下载地址: Github:https://github.

PagerSlidingTabStrip实现网易新闻导航栏效果

PagerSlidingTabStrip实现网易新闻导航栏效果之前在项目当中有一个需求,实现类似网易新闻标题导航的效果,于是在在github搜索下,找了一个开源PagerSlidingTabStrip,研究了这个空间的使用和一些方法,在此与大家分享,希望能够帮到有需要的朋友,好了废话不多讲,直接上代码. package com.example.textpagerslidingtabstrip.activity; import com.example.textpagerslidingtabstri

Android应用经典主界面框架之二:仿网易新闻client、CSDN client (Fragment ViewPager)

另外一种主界面风格则是以网易新闻.凤凰新闻以及新推出的新浪博客(阅读版)为代表.使用ViewPager+Fragment,即ViewPager里适配器里放的不是一般的View.而是Fragment.所以适配器不能继承PagerAdapter,而要继承FragmentPagerAdapter,这是在android.support.v4.app.FragmentPagerAdapter包里的.有点奇葩的是,FragmentPagerAdapter仅仅在这个包里有,在android.app.*这个包以

Android应用经典主界面框架之二:仿网易新闻客户端、CSDN 客户端 (Fragment ViewPager)

第二种主界面风格则是以网易新闻.凤凰新闻以及新推出的新浪博客(阅读版)为代表,使用ViewPager+Fragment,即ViewPager里适配器里放的不是一般的View,而是Fragment.所以适配器不能继承PagerAdapter,而要继承FragmentPagerAdapter,这是在android.support.v4.app.FragmentPagerAdapter包里的.有点奇葩的是,FragmentPagerAdapter只在这个包里有,在android.app.*这个包下面么

iOS界面-仿网易新闻左侧抽屉式交互

1.介绍 用过网易新闻客户端的同学都会发现,网易新闻向左滑动时,左侧的导航栏会跟着拖动出来,新闻内容列表会拉到最右侧.像一个抽屉拉出来一样.很酷.除了网易新闻,现在好多应用都采用了这样的交互. 对手势识别不熟悉的请参考上篇: iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 这个交互效果主要用到两个手势,一个是pan拖拽,一个是tap点击.拖拽可以把抽屉拉出来,再推回去.点击可以把抽屉推回去. 效果如下:     那么这个效果如何实现呢? 2.实现思路和步骤 思路:从实现

Android仿小米商城底部导航栏之二(BottomNavigationBar、ViewPager和Fragment的联动使用)

简介 在前文<Android仿小米商城底部导航栏(基于BottomNavigationBar)>我们使用BottomNavigationBar控件模仿实现了小米商城底部导航栏效果.接下来更进一步的,我们将通过BottomNavigationBar控件和ViewPager空间的联动使用来实现主界面的滑动导航. 导航是移动应用最重要的方面之一,对用户体验是良好还是糟糕起着至关重要的作用.好的导航可以让一款应用更加易用并且让用户快速上手.相反,糟糕的应用导航很容易让人讨厌,并遭到用户的抛弃.为了打造

Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签

之前用JakeWharton的开源框架ActionBarSherlock和ViewPager实现了对网易新闻客户端Tab标签的功能,ActionBarSherlock是在3.0以下的机器支持ActionBar的功能,有兴趣的可以看看开源框架ActionBarSherlock 和 ViewPager 仿网易新闻客户端,今天用到的是JakeWharton的另一开源控件ViewPageIndicator,ViewPager想必大家都知道,Indicator指示器的意思,所以ViewPageIndica

Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻clientTab标签

之前用JakeWharton的开源框架ActionBarSherlock和ViewPager实现了对网易新闻clientTab标签的功能,ActionBarSherlock是在3.0下面的机器支持ActionBar的功能,有兴趣的能够看看开源框架ActionBarSherlock 和 ViewPager 仿网易新闻client,今天用到的是JakeWharton的还有一开源控件ViewPageIndicator.ViewPager想必大家都知道,Indicator指示器的意思,所以ViewPag