首页 > 代码库 > iOS实现网易新闻首页

iOS实现网易新闻首页


主要就是这个页面,实现的主要功能有:1.点击上面的标题栏,下方的红色滚动条跟着到所点击的标题的下方;2中间的图片进行循环自动播放;3,点击下面的各项标签将所点击的标签添加到上面的标题栏,4当点击下面的标签在上面已经存在的时候将其从上面的标题栏移除

//viewDidLoad里面调用各种方法
- (void)viewDidLoad
{
    // Do any additional setup after loading the view.
    self.titleArray = [NSMutableArray arrayWithObjects:@"头条",@"世界杯",@"推荐",@"娱乐",@"体育",@"财经", nil];
    [self setupTopView];//设置最上面的红色视图
    [self setupScrollView];//设置红色视图下面的标题栏
    [self setupImageScrollView];//设置中间图片的布局
    [self setupPageControl];//设置图片右下角的pageControl
    [self setupLabel];//设置中间的"添加标签"
    [self setupAllButton];//设置最下面的所有标签
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(cyclePlay) userInfo:nil repeats:YES];//使用定时器实现图片的循环播放

}

- (void)setupTopView
{
    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    [redView release];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(120, 20, 80, 20)];
    label.text = @"网易新闻";
    [redView addSubview:label];
    [label release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(280, 30, 30, 20);
    [button setTitle:@"+" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:40];
    [redView addSubview:button];
}


//设置上面标题栏的scrollView
- (void)setupScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 320, 30)];
    _scrollView.contentSize = CGSizeMake(640 , 30);
    [self.view addSubview:_scrollView];
        _scrollView.showsVerticalScrollIndicator = NO;
    for (int i = 0; i < [_titleArray count]; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(50 * i, 0, 50, 30);
        [button setTitle:_titleArray[i] forState:UIControlStateNormal];
        [button setTintColor:[UIColor blackColor]];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 110 + i;
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [_scrollView addSubview:button];
    }
    _scrollView.bounces = NO;
    _scrollView.tag = 300;
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(10, 27, 50, 3)];
    _titleView.backgroundColor = [UIColor redColor];
    [_scrollView addSubview:_titleView];
    [_titleView release];
}
//设置标题栏键的点击事件,保证下边的进度条和上面的文字同步
- (void)titleClick:(UIButton *)button
{
    _titleView.frame = CGRectMake(button.frame.origin.x, 27, 50, 3);
}

</pre><pre name="code" class="objc">//设置中间图片的scrollView
//虽然播放的是3张图片但是为了进行循环播放,要设置5个空间大小,是怎么实现循环的具体请看http://blog.csdn.net/canyeyj/article/details/38963435
- (void)setupImageScrollView
{
    self.imageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 90, 320, 240)];
    _imageScrollView.contentSize = CGSizeMake(320 * 5, 240);
    _imageScrollView.contentOffset = CGPointMake(320, 0);
    for (int i = 0; i < 5; i++) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320 * i, 0, 320, 240)];
        if (0 == i) {
            _imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"v6_guide_3"] ofType:@"png"]];
        } else if (i >= 1 && i <= 3) {
            _imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"v6_guide_%d", i] ofType:@"png"]];
        } else if (4 == i) {
            _imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"v6_guide_1"] ofType:@"png"]];
        }
        //        _imageView.tag = 300 + i;
        [_imageScrollView addSubview:_imageView];
        [_imageView release];
    }
    _imageScrollView.tag = 150;
    _imageScrollView.showsHorizontalScrollIndicator = NO;
    _imageScrollView.showsVerticalScrollIndicator = NO;
    _imageScrollView.delegate = self;
    [self.view addSubview:_imageScrollView];
    _imageScrollView.pagingEnabled = YES;
    [_imageScrollView release];
}

//设置pageControl
- (void)setupPageControl
{
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(250, 290, 30, 20)];
    _pageControl.numberOfPages = 3;
    _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    _pageControl.pageIndicatorTintColor = [UIColor blackColor];
    _pageControl.tag = 160;
    [_pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    [_pageControl release];
}

//设置pageControl的点击事件
- (void)handlePageControl:(UIPageControl *)pageControl
{
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:150];
   [scrollView setContentOffset:CGPointMake (320 * (pageControl.currentPage + 1), 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIPageControl *pageControl = (UIPageControl *) [self.view viewWithTag:160];
    pageControl.currentPage = (scrollView.contentOffset.x  - 320 )/ 320;
    int i = (scrollView.contentOffset.x  - 320 ) / 320 + 1;
    if (0 == i) {
        scrollView.contentOffset = CGPointMake(320 * 3, 0);
        pageControl.currentPage = 2;
        
    } else if (4 == i){
        scrollView.contentOffset = CGPointMake(320, 0);
        pageControl.currentPage = 0;
    }
}

//设置添加标签拦
- (void)setupLabel
{
    _labelView = [[UIView alloc]initWithFrame:CGRectMake(0, 330, 320, 80)];
    _labelView.backgroundColor = [UIColor colorWithRed: 255 / 225.0  green:182 / 225.0 blue: 193 / 225.0 alpha:1.0];
    [self.view addSubview:_labelView];
    [_labelView release];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 40)];
    label2.text = @"添加标签";
    label2.font = [UIFont systemFontOfSize:15];
    [_labelView addSubview:label2];
    [label2 release];
    
}

</pre>//下面的各标签的button是自定义的一个继承于UIButton的类,设置属性flag<pre name="code" class="objc">//设置下面的选项按钮
- (void)setupAllButton
{
    NSArray *labelArray = @[@"科技",@"轻松一刻",@"数码",@"时尚",@"游戏",@"原创",@"汽车",@"CBA",@"精选",@"房产",@"手机",@"家居"];
     int n = 0;
    for (int i = 0 ; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            _button = [YJButton buttonWithType:UIButtonTypeSystem];
            _button.frame = CGRectMake(10 + i * 75, 430 + j * 40, 65, 30);
            _button.backgroundColor = [UIColor lightGrayColor];
            [_button setTintColor:[UIColor blackColor]];
            _button.tag = 116 + n;
            _button.flag = YES;
            [_button setTitle:labelArray[n++] forState:UIControlStateNormal];
            [self.view addSubview:_button];
            [_button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

//设置下面各标签的点击事件
- (void)click:(YJButton *)button
{
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:300];
    [scrollView removeFromSuperview];
   if (button.flag) {
        [self.titleArray addObject:button.currentTitle];
        [self setupScrollView];
        button.flag = NO;
    } else {
        [self.titleArray removeObject:button.currentTitle];
        [self setupScrollView];
        button.flag = YES;
    }
}

//定时器的播放事件
- (void)cyclePlay
{
    NSInteger pageNum =  _pageControl.currentPage;
    pageNum++;
    if (pageNum > 2) {
        pageNum = 0;
    }
    _pageControl.currentPage = pageNum;
    [self handlePageControl:_pageControl];
}

//内存处理
- (void)dealloc
{
    [_titleArray release];
    [_scrollView release];
    [_titleArray release];
    [_imageScrollView release];
    [_pageControl release];
    [super dealloc];
}











iOS实现网易新闻首页