Implement Netease news homepage for iOS
This page provides the following functions: 1. click the title bar above, and the red scroll bar below follows to the bottom of the clicked title; 2. The picture in the middle is automatically played cyclically; 3, click the following labels to add the clicked labels to the top title bar. 4. When you click the label that already exists on the top, remove it from the top title bar.
// Call various methods in viewDidLoad-(void) viewDidLoad {// Do any additional setup after loading the view. self. titleArray = [NSMutableArray arrayWithObjects: @ "toutiao.com", @ "World Cup", @ "recommendation", @ "Entertainment", @ "Sports", @ "finance", nil]; [self setupTopView]; // set the top red view [self setupScrollView]; // set the title bar [self setupImageScrollView] under the red view; // set the layout of the intermediate image [self setupPageControl]; // set pageControl [self setupLabel] in the lower right corner of the image; // set "Add Tag" [self setupAllButton] in the middle. // set all the following labels [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector: @ selector (cyclePlay) userInfo: nil repeats: YES]; // use the timer to implement loop playback of images}
-(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 = @ "Netease News"; [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];}
// Set 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: uibuttonpestyystem]; 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];}
// Set the Click Event of the title bar key to ensure that the progress bar below is synchronized with the text above-(void) titleClick :( UIButton *) button {_ titleView. frame = CGRectMake (button. frame. origin. x, 27, 50, 3 );}
// Set the scrollView of the intermediate image. // although three images are played, you need to set 5 space sizes for loop playback, is how to realize the loop specific please see 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];}
// Set 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];}
// Set the pageControl Click Event-(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 ;}}
// Set to add a tag-(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 = @ "Add Tag"; label2.font = [UIFont systemFontOfSize: 15]; [_ labelView addSubview: label2]; [label2 release];}
// The buttons of the following labels are custom classes inherited from UIButton and set the flag attribute.
// Set the following Option Button-(void) setupAllButton {NSArray * labelArray = @ [@ "", @ "", @ "game", @ "original", @ "car", @ "CBA", @ "featured", @ "Real Estate", @ "Mobile Phone ", @ "home"]; 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] ;}}}
// Set the click Event (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 ;}}
// Timer playback event-(void) cyclePlay {NSInteger pageNum = _ pageControl. currentPage; pageNum ++; if (pageNum> 2) {pageNum = 0;} _ pageControl. currentPage = pageNum; [self handlePageControl: _ pageControl];}
// Memory processing-(void) dealloc {[_ titleArray release]; [_ scrollView release]; [_ titleArray release]; [_ imageScrollView release]; [_ pageControl release]; [super dealloc];}