The implementation of Netease news homepage allows you to add, remove tags, automatically scroll images, and switch between left and right images infinitely
//// RootViewController. h // Homework_163News /// Created by lanouhn on 14-8-30. // Copyright (c) 2014 vaercly@163.com Chen conglei. all rights reserved. // # import
@ 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 vaercly@163.com Chen conglei. all rights reserved. // # import "RootViewController. h "# import" NewsView. h "@ interface RootViewController ()
{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 Priority: 1 target: self selector: @ selector (changePage) userInfo: n Il repeats: YES]; self. titles = [NSMutableArray arrayWithObjects: @ "toutiao.com", @ "World Cup", @ "recommendation", @ "Entertainment", @ "Sports", @ "finance", nil]; self. labels = [NSMutableArray arrayWithObjects: @ "technology", @ "fashion", @ "car", @ "Real Estate", @ "easy moment", @ "game ", @ "CBA", @ "Mobile Phone", @ "digital", @ "original", @ "featured", @ "home", nil]; _ images = [NSMutableArray array]; for (int I = 0; I <6; I ++) {UIImage * image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForRes Ource: [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 v IewDidLoad]; // 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 * news View = (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) preview :( 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 setContentOffs Et: 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 {// single-Order Version/* 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 * (pa Ge + 1), 0) animated: NO];} * // dual-Order Version 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 didreceivemorywarning]; // 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 vaercly@163.com Chen conglei. all rights reserved. // # import
# 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 vaercly@163.com Chen conglei. all rights reserved. // # import "NewsView. h "@ interface NewsView () {variables * _ 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 = @ "Netease News"; [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 = @ "Add Tag"; [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 vaercly@163.com Chen conglei. all rights reserved. // # import
@ 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 vaercly@163.com Chen conglei. 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 vaercly@163.com Chen conglei. all rights reserved. // # import
@ Interface FlagButton: UIButton @ property (nonatomic, assign) BOOL flag; @ end