//? AppDelegate.m
#import "NextViewController.h"
#import "ViewController.h"
//? ViewController.m
#import "NextViewController.h"
導(dǎo)入頭文件
<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循環(huán)來添加圖片
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;//設(shè)置圖片視圖的內(nèi)容顯示(這里設(shè)置為適合本身長寬比例)
imageView.clipsToBounds = YES;//把超出父視圖的部分剪切掉
[_scollView addSubview:imageView];
}
#pragma mark - 配置滾動視圖
[_scollView setContentSize:CGSizeMake(CGRectGetWidth(_scollView.frame)*3, CGRectGetHeight(_scollView.frame))]; //設(shè)置內(nèi)容大小(能滾動的最大范圍)
//? ? //設(shè)置偏移量
//? ? [_scollView setContentOffset:CGPointMake(1400, 50)];
//設(shè)置指示器
_scollView.showsHorizontalScrollIndicator = NO;//水平方向的指示器
_scollView.showsVerticalScrollIndicator = NO; //垂直方向的指示器
//設(shè)置按頁滾動
_scollView.pagingEnabled = YES;
//設(shè)置滾動到邊界的回彈效果
_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];//頁面控制器當(dāng)前頁面時的指示器的顏色
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];? ? //設(shè)置偏移量? ? [_scollView setContentOffset:CGPointMake(0, 0) animated:YES];}- (void)pressedGR:(UIPageControl *)sender{? ? [_scollView setContentOffset:CGPointMake(sender.currentPage * CGRectGetWidth(_scollView.frame), 0) animated:YES];? ? }#pragma mark -//滾動視圖滾動的時候被調(diào)用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//檢測視圖偏移量
NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
if (scrollView.contentOffset.x == 828)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
// 設(shè)置位置
btn.frame = CGRectMake(150, 600, 100, 40);
// 添加標(biāo)題
[btn setTitle:@"立即進入" forState:UIControlStateNormal];
// 設(shè)置背景顏色
btn.backgroundColor = [UIColor yellowColor];
// 設(shè)置字體顏色
[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(@"結(jié)束拖動");
}
- (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;//強轉(zhuǎn)為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;
}
//]===============
// 設(shè)置背景顏色
self.view.backgroundColor = [UIColor lightGrayColor];
[self buildNavBar];
//先生成存放標(biāo)題的數(shù)據(jù)
NSArray *array = [NSArray arrayWithObjects:@"分類",@"排行",@"歌手",@"電臺",@"兒歌",@"MV",@"三" ,nil];
//初始化UISegmentedControl
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
//設(shè)置frame
segment.frame = CGRectMake(0, 64, self.view.frame.size.width, 44);
[self.view addSubview:segment];
_leftTableSource = @[@"推薦",@"心情",@"主題",@"人群",@"場景",@"曲風(fēng)",@"語種",@"樂器",@"歌手"];
_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":@"曲風(fēng)",@"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];
//==============
// 創(chuàng)建兩個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];
// 創(chuàng)建底部欄
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];
}
// 設(shè)置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;
}
}
// 聯(lián)動效果在于這里
- (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; //可任意設(shè)置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;
//先生成存放標(biāo)題的數(shù)據(jù)
NSArray *array = [NSArray arrayWithObjects:@"我的",@"樂庫",@"聽歌" ,nil];
//初始化UISegmentedControl
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
//設(shè)置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;
}