viewConter.h
#import <UIKit/UIKit.h>
@interface ViewController : UITabBarController
@end
viewConter.m
#import "ViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
OneViewController * one = [[OneViewController alloc]init];
one.title = @"首页";
one.tabBarItem.image = [UIImage imageNamed:@"icon_16"];
UINavigationController * T1 = [[UINavigationController alloc]initWithRootViewController:one];
TwoViewController * two = [[TwoViewController alloc]init];
two.title = @"发现";
two.tabBarItem.image = [UIImage imageNamed:@"icon_17"];
UINavigationController * T2 = [[UINavigationController alloc]initWithRootViewController:two];
ThreeViewController * three = [[ThreeViewController alloc]init];
three.title = @"广场";
three.tabBarItem.image = [UIImage imageNamed:@"icon_18"];
UINavigationController * T3 = [[UINavigationController alloc]initWithRootViewController:three];
FourViewController * four = [[FourViewController alloc]init];
four.title = @"我的";
four.tabBarItem.image = [UIImage imageNamed:@"icon_19"];
UINavigationController * T4 = [[UINavigationController alloc]initWithRootViewController:four];
self.viewControllers = @[T1,T2,T3,T4];
}
One.m
#import "OneViewController.h"
#import "oneTableViewCell.h"
#import "Modee.h"
#define TEST_URL @"http://v.juhe.cn/toutiao/index?type=top&key=6dde6de11cc04328c65963b2951dc8b5"
//#import <UIImageView+WebCache.h>
@interface OneViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
UITableView * _table;
NSDictionary * dic;
}
@property (nonatomic, retain)NSTimer* rotateTimer;
@property (nonatomic, retain)UIPageControl *myPageControl;
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiClick:) name:@"www" object:nil];
// 单例类赋值
[[Modee sharddata]shuju];
//初始化scrollView 大小为屏幕大小
UIScrollView * rotateScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
//设置滚动范围
rotateScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3, CGRectGetHeight(self.view.frame));
//设置分页效果
rotateScrollView.pagingEnabled = YES;
//水平滚动条隐藏
rotateScrollView.showsHorizontalScrollIndicator = NO;
//添加三个子视图 UILabel类型
for (int i = 0; i< 3; i++)
{
UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
subLabel.tag = 1000+i;
subLabel.text = [NSString stringWithFormat:@"我是第%d个视图",i];
[subLabel setFont:[UIFont systemFontOfSize:80]];
subLabel.adjustsFontSizeToFitWidth = YES;
[subLabel setBackgroundColor:[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]];
[rotateScrollView addSubview:subLabel];
}
UILabel *tempLabel = [rotateScrollView viewWithTag:1000];
//为滚动视图的右边添加一个视图,使得它和第一个视图一模一样。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*3, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
label.backgroundColor = tempLabel.backgroundColor;
label.text = tempLabel.text;
label.font = tempLabel.font;
label.adjustsFontSizeToFitWidth = YES;
[rotateScrollView addSubview:label];
[self.view addSubview:rotateScrollView];
rotateScrollView.tag = 1000;
self.myPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-50, CGRectGetWidth(self.view.frame), 50)];
self.myPageControl.numberOfPages = 3;
self.myPageControl.currentPage = 0;
[self.view addSubview:self.myPageControl];
//启动定时器
self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeView) userInfo:nil repeats:YES];
//为滚动视图指定代理
rotateScrollView.delegate = self;
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
_table.dataSource =self;
_table.delegate =self;
[self.view addSubview:_table];
//添加头视图
_table.tableHeaderView = rotateScrollView;
}
-(void)notiClick:(NSNotification *)shuju
{
dic =[shuju.object copy];
// NSLog(@"1234=%@",_dic);
// 刷新表格
[_table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [[[dic objectForKey:@"e"] objectForKey:@"data"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString * cellID = @"cell";
oneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell = [[oneTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
// [cell.iamgea sd_setImageWithURL:[[[[dic objectForKey:@"result"] objectForKey:@"data"] objectAtIndex:indexPath.row] objectForKey:@"thumbnail_pic_s"]];
//
cell.label.text = [[[[dic objectForKey:@"data"] objectForKey:@"list"] objectAtIndex:indexPath.row] objectForKey:@"title"];
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 300;
}
////----------------------------------头视图方法-----------------
//开始拖拽的代理方法,在此方法中暂停定时器。
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"正在拖拽视图,所以需要将自动播放暂停掉");
//setFireDate:设置定时器在什么时间启动
//[NSDate distantFuture]:将来的某一时刻
[self.rotateTimer setFireDate:[NSDate distantFuture]];
}
//视图静止时(没有人在拖拽),开启定时器,让自动轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//视图静止之后,过1.5秒在开启定时器
//[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]] 返回值为从现在时刻开始 再过1.5秒的时刻。
NSLog(@"开启定时器");
[self.rotateTimer setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]];
}
//定时器的回调方法 切换界面
- (void)changeView{
//得到scrollView
UIScrollView *scrollView = [self.view viewWithTag:1000];
//通过改变contentOffset来切换滚动视图的子界面
float offset_X = scrollView.contentOffset.x;
//每次切换一个屏幕
offset_X += CGRectGetWidth(self.view.frame);
//说明要从最右边的多余视图开始滚动了,最右边的多余视图实际上就是第一个视图。所以偏移量需要更改为第一个视图的偏移量。
if (offset_X > CGRectGetWidth(self.view.frame)*3) {
scrollView.contentOffset = CGPointMake(0, 0);
}
//说明正在显示的就是最右边的多余视图,最右边的多余视图实际上就是第一个视图。所以pageControl的小白点需要在第一个视图的位置。
if (offset_X == CGRectGetWidth(self.view.frame)*3) {
self.myPageControl.currentPage = 0;
}else{
self.myPageControl.currentPage = offset_X/CGRectGetWidth(self.view.frame);
}
//得到最终的偏移量
CGPoint resultPoint = CGPointMake(offset_X, 0);
//切换视图时带动画效果
//最右边的多余视图实际上就是第一个视图,现在是要从第一个视图向第二个视图偏移,所以偏移量为一个屏幕宽度
if (offset_X >CGRectGetWidth(self.view.frame)*3) {
self.myPageControl.currentPage = 1;
[scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.view.frame), 0) animated:YES];
}else{
[scrollView setContentOffset:resultPoint animated:YES];
}
}
@end
Mode.m
#import "Modee.h"
static Modee * data = nil;
#define TEST_URL @"http://hop.haodou.com/hop/router/rest.json?uuid=e9c30e86e3b5705e6c3e0e7e14ba3ce2&vc=122&_HOP_=%7B%22version%22%3A%221.0%22%2C%22action%22%3A%22front.get_index%22%2C%22secret_id%22%3A%225722f877e4b0d4512e3fd872%22%2C%22current_time%22%3A1510359257%2C%22sign%22%3A%22ed0b6c097b18405bd88e3ad6a9e87128%22%7D&uid=32056801&offset=0&limit=20&appid=4&brand=Apple&channel=appstore&from=iPhone%206&hduid=32056801&idfa=8a40e5398b53b36c415a3ba265f96cf4&isBreak=0&model=iPhone%206&netType=WIFI&osName=iPhone%20OS&osVersion=9.2.1&uid=32056801&uuid=e9c30e86e3b5705e6c3e0e7e14ba3ce2&vc=122&vn=v6.1.35"
@implementation Modee
{
NSDictionary * dic;
}
+(instancetype)sharddata
{
static dispatch_once_t once;
dispatch_once(&once, ^{
data = [[Modee alloc]init];
});
return data;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if (data == nil)
{
data =[super allocWithZone:zone];
}
return data;
}
-(id)copy
{
return self;
}
-(id)mutableCopy
{
return self;
}
-(void)shuju
{
NSURL * url =[NSURL URLWithString:TEST_URL];
NSURLSession * session = [NSURLSession sharedSession];
NSURLSessionDataTask * task =[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dic =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"www" object:dic];
});
}];
[task resume];
}
Modee.h
+(instancetype)sharddata;
-(void)shuju;
oneTableViewCell.h 重写表格
@property(nonatomic,strong)UIImageView * iamgea;
@property(nonatomic,strong)UILabel * label;
oneTableViewCell.m
-(UIImageView *)iamgea
{
if (!_iamgea)
{
_iamgea = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 350, 200)];
[self addSubview:_iamgea];
}
return _iamgea;
}
-(UILabel *)label
{
if (!_label)
{
_label = [[UILabel alloc]initWithFrame:CGRectMake(10, 225, 350, 40)];
[self addSubview:_label];
}
return _label;
}
网友评论