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);}
//設定中間圖片的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]; }
//下面的各標籤的button是自訂的一個繼承於UIButton的類,設定屬性flag
//設定下面的選項按鈕- (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];}