網易新聞首頁的實現,可以添加,移除標籤,自動滾動圖片,無限左右切換圖片

//
//  RootViewController.h
//  Homework_163News
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
@property (nonatomic, retain) NSMutableArray *titles;
@property (nonatomic, retain) NSMutableArray *labels;
@end
//
//  RootViewController.m
//  Homework_163News
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import "RootViewController.h"
#import "NewsView.h"
@interface RootViewController ()<UIScrollViewDelegate>
{
    NSMutableArray *_images;
    NSUInteger _imageCount;
    BOOL _isOrder;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _isOrder = YES;
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changePage) userInfo:nil repeats:YES];
        self.titles = [NSMutableArray arrayWithObjects:@"頭條", @"世界盃", @"推薦", @"娛樂", @"體育", @"財經", nil];
        
        self.labels = [NSMutableArray arrayWithObjects:@"科技", @"時尚", @"汽車", @"房產", @"輕鬆一刻", @"遊戲", @"CBA", @"手機", @"數碼", @"原創", @"精選", @"家居", nil];
        _images = [NSMutableArray array];
        for (int i = 0; i < 6; i++) {
            UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"v6_guide_%d", i + 1]ofType:@"png"]];
            [_images addObject:image];
        }
        _imageCount = _images.count;
    }
    return self;
}

- (void)loadView
{
    NewsView *newsView = [[NewsView alloc] initWithFrame:[UIScreen mainScreen].bounds titles:self.titles images:_images labels:self.labels];
    newsView.backgroundColor = [UIColor whiteColor];
    self.view = newsView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIScrollView *titleScrollView = (UIScrollView *)[self.view viewWithTag:100];
    titleScrollView.delegate = self;
    
//    for (int i = 0; i < _titleCount; i++) {
//        UIButton *title = (UIButton *)[self.view viewWithTag:200 + i];
//        [title addTarget:self action:@selector(showLabel:) forControlEvents:UIControlEventTouchUpInside];
//    }
    
    NewsView *newsView = (NewsView *)self.view;
    newsView.imageView.scrollView.delegate = self;
    [newsView.imageView.pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    
    for (int i = 0; i < self.labels.count / 4; i++) {
        for (int j = 0; j < 4; j++) {
            FlagButton *label = (FlagButton *)[self.view viewWithTag:600 + 4 * i + j];
            [label addTarget:self action:@selector(addLabel:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIScrollView *imageScrollView = (UIScrollView *)[self.view viewWithTag:400];
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:500];
    int currentPage = imageScrollView.contentOffset.x / 320;
    if (currentPage == 0) {
//        [imageScrollView scrollRectToVisible:CGRectMake(320 * _imageCount, 0, 320, imageScrollView.frame.size.height) animated:NO];
        [imageScrollView setContentOffset:CGPointMake(320 * _imageCount, 0) animated:NO];
        pageControl.currentPage = _imageCount - 1;
    } else if (currentPage == _imageCount + 1) {
//        [imageScrollView scrollRectToVisible:CGRectMake(320, 0, 320, imageScrollView.frame.size.height) animated:NO];
        [imageScrollView setContentOffset:CGPointMake(320, 0) animated:NO];
        pageControl.currentPage = 0;
    } else {
        pageControl.currentPage = currentPage - 1;
    }

}

- (void)handlePageControl:(UIPageControl *)pageControl
{
    NSLog(@"%d", pageControl.currentPage);
    UIScrollView *imageScrollView = (UIScrollView *)[self.view viewWithTag:400];
//    [imageScrollView scrollRectToVisible:CGRectMake(320 * (pageControl.currentPage + 1),0,320,imageScrollView.frame.size.height) animated:NO];
    [imageScrollView setContentOffset:CGPointMake(320 * (pageControl.currentPage + 1), 0) animated:YES];
}

- (void)changePage
{
    //單序版
    /*
    NewsView *newsView = (NewsView *)self.view;
    int page = newsView.imageView.pageControl.currentPage;
    page++;
    page = page > _imageCount - 1 ? 0 : page;
    newsView.imageView.pageControl.currentPage = page;
//    [newsView.imageView.scrollView scrollRectToVisible:CGRectMake(320 * (page + 1), 0, 320, newsView.imageView.scrollView.frame.size.height) animated:NO];
    if (page) {
        [newsView.imageView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:YES];
    } else {
        [newsView.imageView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:NO];
    }
     */
    
    //雙序版
    NewsView *newsView = (NewsView *)self.view;
    int page = newsView.imageView.pageControl.currentPage;
    if (_isOrder) {
        page++;
        page = page > _imageCount - 1 ? 0 : page;
        if (!page) {
            _isOrder = NO;
            page = _imageCount - 2;
        }
    } else {
        page--;
        page = page < 0 ? 0 : page;
        if (!page) {
            _isOrder = YES;
            page = 0;
        }
    }
    newsView.imageView.pageControl.currentPage = page;
    [newsView.imageView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:YES];
}

- (void)addLabel:(FlagButton *)btn
{

    NSLog(@"%@", btn.currentTitle);
    NewsView *newsView = (NewsView *)self.view;
    [newsView.titleScrollView removeFromSuperview];
    if (btn.flag) {
        [self.titles addObject:btn.currentTitle];
        [newsView setupTitleScrollView:self.titles];
        btn.flag = NO;
    } else {
        [self.titles removeObject:btn.currentTitle];
        [newsView setupTitleScrollView:self.titles];
        btn.flag = YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if (!self.view.window && [self isViewLoaded]) {
        self.view = nil;
    }
}

- (void)dealloc
{
    self.titles = nil;
    self.labels = nil;
    [super dealloc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  NewsView.h
//  Homework_163News
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "LoopView.h"
#import "FlagButton.h"

@interface NewsView : UIView
@property (nonatomic, retain) NSMutableArray *labels;
@property (nonatomic, retain) NSMutableArray *titles;
@property (nonatomic, retain) LoopView *imageView;
@property (nonatomic, retain) UIScrollView *titleScrollView;
@property (nonatomic, retain) FlagButton *aLabel;
- (void)setupTitleScrollView:(NSMutableArray *)titles;
- (id)initWithFrame:(CGRect)frame titles:(NSMutableArray *)titles images:(NSMutableArray *)images labels:(NSMutableArray *)labels;
@end
//
//  NewsView.m
//  Homework_163News
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import "NewsView.h"
@interface NewsView ()
{
    NSMutableArray *_images;
    NSUInteger _titlesCount;
    NSUInteger _imagesCount;
    NSUInteger _labelsCount;
}
@end

@implementation NewsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setupRedView];
        [self setupTitleScrollView:self.titles];
        [self setupImageView];
        [self setupLabelView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame titles:(NSMutableArray *)titles images:(NSMutableArray *)images labels:(NSMutableArray *)labels
{
    self.titles = titles;
    _images = images;
    self.labels = labels;
    _imagesCount = images.count;
    _labelsCount = labels.count;
    [self initWithFrame:frame];
    return self;
}

- (void)setupRedView
{
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    redView.backgroundColor = [UIColor colorWithRed:1.0 green:0.1 blue:0 alpha:0.5];
    [self addSubview:redView];
    [redView release];
    
    UILabel *topical = [[UILabel alloc] initWithFrame:CGRectMake(130, 20, 70, 30)];
    topical.text = @"網易新聞";
    [redView addSubview:topical];
    [topical release];
    
    UIButton *add = [UIButton buttonWithType:UIButtonTypeSystem];
    add.frame = CGRectMake(280, 28, 25, 25);
    [add setTitle:@"+" forState:UIControlStateNormal];
    [add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    add.titleLabel.font = [UIFont systemFontOfSize:40];
    [redView addSubview:add];
    [add release];
}

- (void)setupTitleScrollView:(NSMutableArray *)titles
{
    self.titles = titles;
    _titlesCount = titles.count;
//    int width = (320 - 5 * (_titlesCount + 1)) / _titlesCount;
    int width = 47;
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 60, 320, 40)];
    _titleScrollView.backgroundColor = [UIColor colorWithRed:0.6 green:0.1 blue:0.0 alpha:0.2];
    _titleScrollView.userInteractionEnabled = YES;
    _titleScrollView.showsHorizontalScrollIndicator = NO;
    _titleScrollView.tag = 100;
    _titleScrollView.contentSize = CGSizeMake(320 + _titlesCount * width, _titleScrollView.bounds.size.height);
    _titleScrollView.pagingEnabled = YES;
    [self addSubview:_titleScrollView];
    [_titleScrollView release];
    
    int x = 5;
    for (int i = 0; i < _titlesCount; i++) {
        UIButton *title = [UIButton buttonWithType:UIButtonTypeSystem];
        title.frame = CGRectMake(x, 5, width, 30);
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, title.frame.origin.y + title.frame.size.height, width, 2)];
        title.userInteractionEnabled = YES;
        title.tag = 200 + i;
        line.tag = 300 + i;
        title.titleLabel.font = [UIFont systemFontOfSize:15];
        [title setTitle:self.titles[i] forState:UIControlStateNormal];
        [title setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        x += width + 5;
        [title addTarget:self action:@selector(showLabel:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleScrollView addSubview:title];
        [self.titleScrollView addSubview:line];
        [line release];
    }

}

    

- (void)showLabel:(UIButton *)btn
{
    UILabel *line = (UILabel *)[self viewWithTag:btn.tag + 100];
    for (int i = 0; i < self.titles.count; i++) {
        UILabel *otherLine = (UILabel *)[self viewWithTag:300 + i];
        otherLine.backgroundColor = [UIColor clearColor];
    }
    line.backgroundColor = [UIColor redColor];
}


- (void)setupImageView
{
    self.imageView = [[LoopView alloc] initWithFrame:CGRectMake(0, 100, 320, 280) images:_images];
    [self addSubview:_imageView];
    [_imageView release];
}


- (void)setupLabelView
{
    UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 380, 320, 50)];
    addLabel.backgroundColor = [UIColor colorWithRed:0.6 green:0.1 blue:0.0 alpha:0.2];
    addLabel.text = @"添加標籤";
    [self addSubview:addLabel];
    [addLabel release];
    
    for (int i = 0; i < _labelsCount / 4; i++) {
        for (int j = 0; j < 4; j++) {
            self.aLabel = [FlagButton buttonWithType:UIButtonTypeSystem];
            _aLabel.flag = YES;
            _aLabel.frame = CGRectMake(5 + j * 80, 450 + i * 40, 73, 20);
            _aLabel.backgroundColor = [UIColor lightGrayColor];
            _aLabel.tag = 600 + 4 * i + j;
            _aLabel.layer.cornerRadius = 2;
            [_aLabel setTitle:self.labels[4 * i + j] forState:UIControlStateNormal];
            [_aLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            _aLabel.titleLabel.font = [UIFont systemFontOfSize:18];
            [self addSubview:_aLabel];
        }
    }
}

- (void)dealloc
{
    self.aLabel = nil;
    self.labels = nil;
    self.titles = nil;
    self.imageView = nil;
    self.titleScrollView = nil;
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
//
//  LoopView.h
//  Test_LoopScrollView
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LoopView : UIView
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
- (id)initWithFrame:(CGRect)frame images:(NSMutableArray *)images;
@end
//
//  LoopView.m
//  Test_LoopScrollView
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import "LoopView.h"
@interface LoopView ()
{
    NSMutableArray *_images;
    NSUInteger _imagesCount;
}
@end
@implementation LoopView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setupScrollView];
        [self setupPageControl];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame images:(NSMutableArray *)images
{
    _images = images;
    _imagesCount = images.count;
    [self initWithFrame:frame];
    return self;
}

- (void)setupScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _scrollView.tag = 400;
    _scrollView.contentSize = CGSizeMake(320 * (_imagesCount + 2), [UIScreen mainScreen].bounds.size.height);
//    [_scrollView scrollRectToVisible:CGRectMake(320,0,320,[UIScreen mainScreen].bounds.size.height) animated:NO];
    [_scrollView setContentOffset:CGPointMake(320, 0) animated:NO];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    [self addSubview:_scrollView];
    [_scrollView release];
    
    for (int i = 0; i < _imagesCount + 2; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, self.frame.size.height)];
        if (i == 0) {
            imageView.image = [UIImage imageNamed:@"v6_guide_6"];
        } else if (i == _imagesCount + 1) {
            imageView.image = [UIImage imageNamed:@"v6_guide_1"];
        } else {
            imageView.image = _images[i - 1];
        }
        [_scrollView addSubview:imageView];
        [imageView release];
    }
}

- (void)setupPageControl
{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, self.frame.size.height - 40, 300, 20)];
    _pageControl.tag = 500;
    _pageControl.numberOfPages = 6;
    _pageControl.currentPage = 0;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    [self addSubview:_pageControl];
    [_pageControl release];
    
}

- (void)dealloc
{
    self.scrollView = nil;
    self.pageControl = nil;
    [super dealloc];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
//
//  FlagButton.h
//  Homework_163News
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FlagButton : UIButton
@property (nonatomic, assign) BOOL flag;
@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章