网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片
2014-08-31 22:17
806 查看
// // RootViewController.h // Homework_163News // // Created by lanouhn on 14-8-30. // Copyright (c) 2014年 vaercly@163.com 陈聪雷. 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年 vaercly@163.com 陈聪雷. 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年 vaercly@163.com 陈聪雷. 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年 vaercly@163.com 陈聪雷. 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年 vaercly@163.com 陈聪雷. 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年 vaercly@163.com 陈聪雷. 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 陈聪雷. All rights reserved. // #import <UIKit/UIKit.h> @interface FlagButton : UIButton @property (nonatomic, assign) BOOL flag; @end
相关文章推荐
- 滚动视图(UIScrollView)无限无缝左右切换图片,自动顺序加逆序循环播放图片
- 使用viewpager嵌套实现上下左右滑动切换图片(IOS双向滚动翻页效果相同)
- javascript图片自动左右滑动滚动切换
- android 图片滚动可以自动切换
- [Android UI]可自动切换、无限滑动的图片广告展示栏的实现分享(续1)
- 带左右箭头切换的自动滚动图片JS特效
- jQuery实现图片与文字描述左右滑动自动切换的方法
- android自定义Gallery实现手动和自动循环滚动切换图片
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动 - 郭晓
- 带左右箭头切换的自动滚动图片JS特效
- OCUI界面设计:滚动视图与分页控件结合NSTimer实现图片自动循环与无限滚动展示
- javascript实现首页图片自动切换【HTML/JSP】
- Jquery实现图片左右滚动(自动)
- Android开发之实现图片自动滚动显示标签的ViewPager
- Android实行图片滚动空间,带页标签功能,实现自动播发功能
- 使用viewpager嵌套实现上下左右滑动切换图片(IOS双向滚动翻页效果相同)
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- jquery实现图片左右间隔滚动特效(可自动播放)
- android自定义Gallery实现手动和自动循环滚动切换图片
- [javascript]首页图片自动切换的一种实现方式