网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片

阅读: 评论:0

网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片

网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片

//
//  RootViewController.h
//  Homework_163News
//
//  Created by lanouhn on 14-8-30.
//  Copyright (c) 2014年 vaercly@163 陈聪雷. 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 陈聪雷. 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 = _unt;}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 < unt / 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 = tOffset.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 陈聪雷. 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 陈聪雷. 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 = unt;_labelsCount = unt;[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)]; = @"网易新闻";[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 = unt;
//    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;_tSize = 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, igin.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 < unt; 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]; = @"添加标签";[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;_Radius = 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 陈聪雷. 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 陈聪雷. 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 = unt;[self initWithFrame:frame];return self;
}- (void)setupScrollView
{self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];_scrollView.tag = 400;_tSize = 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 陈聪雷. All rights reserved.
//#import <UIKit/UIKit.h>@interface FlagButton : UIButton
@property (nonatomic, assign) BOOL flag;
@end

本文发布于:2024-02-02 13:00:18,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170685001943965.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:图片   首页   移除   网易新闻   标签
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23