// AppDelegate.m
#import "NextViewController.h"
#import "ViewController.h"
// ViewController.m
#import "NextViewController.h"
导入头文件
<UIScrollViewDelegat>
{
UIScrollView * _scollView;
}
//=======================================
_scollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self .view.frame))];
_scollView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_scollView];
#pragma mark - 利用for循环来添加图片
for (int i = 0; i < 3; i ++) {
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*CGRectGetWidth(_scollView.frame), 0, CGRectGetWidth(_scollView.frame), CGRectGetHeight(_scollView.frame))];
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]]];
imageView.contentMode = UIViewContentModeScaleAspectFit;//设置图片视图的内容显示(这里设置为适合本身长宽比例)
imageView.clipsToBounds = YES;//把超出父视图的部分剪切掉
[_scollView addSubview:imageView];
}
#pragma mark - 配置滚动视图
[_scollView setContentSize:CGSizeMake(CGRectGetWidth(_scollView.frame)*3, CGRectGetHeight(_scollView.frame))]; //设置内容大小(能滚动的最大范围)
// //设置偏移量
// [_scollView setContentOffset:CGPointMake(1400, 50)];
//设置指示器
_scollView.showsHorizontalScrollIndicator = NO;//水平方向的指示器
_scollView.showsVerticalScrollIndicator = NO; //垂直方向的指示器
//设置按页滚动
_scollView.pagingEnabled = YES;
//设置滚动到边界的回弹效果
_scollView.bounces = YES;
_scollView.delegate = self;
#pragma mark - UIPageControl
UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(150, 650, 100, 40)];//他的大小影响用户交互的范围
// pageControl.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetWidth(self.view.bounds)+60);
pageControl.currentPage = 0;
pageControl.numberOfPages = 3;
pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];//页面控制器当前页面时的指示器的颜色
pageControl.pageIndicatorTintColor = [UIColor grayColor];
// pageControl.currentPage = //该方法去scrollViewDidEndDecelerating里面写
pageControl.tag = 101;
// pageControl.backgroundColor = [UIColor grayColor];
[pageControl addTarget:self action:@selector(pressedGR:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pageControl];
}
//=============================================
- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; //设置偏移量 [_scollView setContentOffset:CGPointMake(0, 0) animated:YES];}- (void)pressedGR:(UIPageControl *)sender{ [_scollView setContentOffset:CGPointMake(sender.currentPage * CGRectGetWidth(_scollView.frame), 0) animated:YES]; }#pragma mark -//滚动视图滚动的时候被调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//检测视图偏移量
NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
if (scrollView.contentOffset.x == 828)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
// 设置位置
btn.frame = CGRectMake(150, 600, 100, 40);
// 添加标题
[btn setTitle:@"立即进入" forState:UIControlStateNormal];
// 设置背景颜色
btn.backgroundColor = [UIColor yellowColor];
// 设置字体颜色
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// 添加按钮事件
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"将要开始拖动");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"结束拖动");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"开始减速");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int index = scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame);
UIView * view = [self.view viewWithTag:101];
if ([view isKindOfClass:[UIPageControl class]]) {
UIPageControl* pagecontrol = (UIPageControl *)view;//强转为UIPageControl
pagecontrol.currentPage = index;
}
NSLog(@"减速完毕");
}
- (void)click
{
NextViewController *next = [[NextViewController alloc]init];
[self presentViewController:next animated:YES completion:nil];
}
//// NextViewController.m
<UITableViewDelegate,UITableViewDataSource>
{
UINavigationBar *_navigationBar;
UITableView *_rightTableView;
UITableView *_leftTableView;
NSArray *_leftTableSource;
NSArray *_rightTableSource;
}
//]===============
// 设置背景颜色
self.view.backgroundColor = [UIColor lightGrayColor];
[self buildNavBar];
//先生成存放标题的数据
NSArray *array = [NSArray arrayWithObjects:@"分类",@"排行",@"歌手",@"电台",@"儿歌",@"MV",@"三" ,nil];
//初始化UISegmentedControl
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
//设置frame
segment.frame = CGRectMake(0, 64, self.view.frame.size.width, 44);
[self.view addSubview:segment];
_leftTableSource = @[@"推荐",@"心情",@"主题",@"人群",@"场景",@"曲风",@"语种",@"乐器",@"歌手"];
_rightTableSource = @[@{@"header":@"推荐",@"title":@[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff"]},
@{@"header":@"心情",@"title":@[@"gg",@"mm",@"nn",@"oo",@"pp",@"qq"]},
@{@"header":@"主题",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]},
@{@"header":@"人群",@"title":@[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff"]},
@{@"header":@"场景",@"title":@[@"gg",@"mm",@"nn",@"oo",@"pp",@"qq"]},
@{@"header":@"曲风",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]}, @{@"header":@"语种",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]}, @{@"header":@"乐器",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]}, @{@"header":@"歌手",@"title":@[@"rr",@"ss",@"jj",@"xx",@"yy",@"zz"]}];
[self setupSomeParamars];
//==============
// 创建两个tableview
- (void)setupSomeParamars
{
_rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(100, 108, self.view.frame.size.width - 100, self.view.frame.size.height-44) style:UITableViewStyleGrouped];
_rightTableView.dataSource = self;
_rightTableView.delegate = self;
[self.view addSubview:_rightTableView];
_leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 108, 100, self.view.frame.size.height-44) style:UITableViewStyleGrouped];
_leftTableView.dataSource = self;
_leftTableView.delegate = self;
[self.view addSubview:_leftTableView];
// 创建底部栏
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-44, [UIScreen mainScreen].bounds.size.width, 44)];
view.backgroundColor = [UIColor redColor];
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
img.image = [UIImage imageNamed:@"1.jpg"];
[view addSubview:img];
UILabel *l1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 5, 70, 20)];
l1.text = @"凸凹";
[view addSubview:l1];
UILabel *l2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 25, 70, 20)];
l2.text = @"崔长春";
[view addSubview:l2];
UILabel *l3 = [[UILabel alloc]initWithFrame:CGRectMake(250, 5, 40, 40)];
l3.text = @"播放";
[view addSubview:l3];
UILabel *l4 = [[UILabel alloc]initWithFrame:CGRectMake(300, 5, 70, 40)];
l4.text = @"下一首";
[view addSubview:l4];
UILabel *l5 = [[UILabel alloc]initWithFrame:CGRectMake(370, 5, 40, 40)];
l5.text = @"列表";
[view addSubview:l5];
[self.view addSubview:view];
}
// 设置cell的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifer = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifer];
}
if(tableView == _rightTableView){
cell.textLabel.text = [_rightTableSource[indexPath.section] objectForKey:@"title"][indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
cell.detailTextLabel.text = @"522.77万人在听";
}else if (tableView == _leftTableView){
cell.textLabel.text = _leftTableSource[indexPath.row];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (tableView == _rightTableView) {
return 50;
}else{
return 50;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == _rightTableView) {
return _rightTableSource.count;
}else{
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == _leftTableView) {
return _leftTableSource.count;
}else{
return [[_rightTableSource[section] objectForKey:@"title"] count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView == _rightTableView) {
return [_rightTableSource[section] objectForKey:@"header"];
}else{
return nil;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView == _rightTableView){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-100, 40)];
label.backgroundColor = [UIColor cyanColor];
label.text = [_rightTableSource[section] objectForKey:@"header"];
label.textColor = [UIColor redColor];
return label;
}else{
return nil;
}
}
// 联动效果在于这里
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if(tableView == _rightTableView){
[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
-(void)buildNavBar{
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview: self.navigationBar];
}
/**
* navigation bar
*/
-(UINavigationBar *)navigationBar{
if (!_navigationBar) {
if (self.navigationController) {
// 隐藏原有nav bar
[self.navigationController setNavigationBarHidden:true animated:true];
}
CGRect frame = self.view.bounds;
frame.size.height = 64; //可任意设置frame
UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:frame];
bar.barStyle = UIBarStyleDefault;
bar.translucent = false;
bar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
bar.barTintColor = [UIColor redColor];
bar.tintColor = [UIColor whiteColor];
bar.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor] };
// bar button items navBarButtonItemDidClick
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3"] style:UIBarButtonItemStylePlain target:self action:@selector(navBarButtonItemDidClick:)];
// cancelItem.tag = NavBarItemCancel;
UIBarButtonItem *okItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"4"] style:UIBarButtonItemStylePlain target:self action:@selector(navBarButtonItemDidClick:)];
// okItem.tag = NavBarItemOk;
//先生成存放标题的数据
NSArray *array = [NSArray arrayWithObjects:@"我的",@"乐库",@"听歌" ,nil];
//初始化UISegmentedControl
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
//设置frame
segment.frame = CGRectMake(50, 0, self.view.frame.size.width, 30);
// nav item
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.titleView = segment;
navItem.leftBarButtonItem = cancelItem;
navItem.rightBarButtonItem = okItem;
[bar pushNavigationItem:navItem animated:false];
_navigationBar = bar;
}
return _navigationBar;
}