iOS 页面切换 && 下拉菜单

在 AppDelegate.m 中导入头文件

#import "ViewController.h"

设置导航条更改主窗口

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 更改主窗口
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    
    return YES;
}

在 ViewController.m中做以下操作
设置tableView和scrollView的协议

<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>
// 定义全局成员变量
{
    UIScrollView *topScrollView; // 顶部滚动视图
    UITableView *smallTableView; // 下拉菜单tableView
    UIButton *plusBtn;  // "+"号按钮
    UIScrollView *mainScrollView; // 内容部分的大滚动视图
}

便于使用,定义两个宏,获取视图的宽度和高度

// 屏幕的宽
#define VIEW_WIDTH [UIScreen mainScreen].bounds.size.width
// 屏幕的高
#define VIEW_HEIGHT [UIScreen mainScreen].bounds.size.height
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 标题
    self.title = @"列表";
    // 视图的背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 调用头部视图方法
    [self createTopView];
    
    // 调用内容部分大滚动视图方法
    [self createMainScrollView];
    
    
    // 创建表格
    smallTableView = [[UITableView alloc] initWithFrame:CGRectMake(VIEW_WIDTH - 20, 64 + 50, 0.1, 0.1) style:UITableViewStylePlain];
    smallTableView.delegate = self;
    smallTableView.dataSource = self;
    smallTableView.rowHeight = 60;
    [self.view addSubview:smallTableView];
    
}

头部视图的自定义方法

// 创建头部视图
-(void)createTopView
{
    // 创建头部滚动视图
    topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH - 50, 50)];
    topScrollView.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:(245/255.0)];
    [self.view addSubview:topScrollView];
    // 设置滚动范围
    topScrollView.contentSize = CGSizeMake(VIEW_WIDTH + VIEW_WIDTH / 5, 0);
    topScrollView.showsHorizontalScrollIndicator = NO;
    
    // 创建按钮数组
    NSArray *topBtnArr = @[@"第一第一",@"第二",@"第三",@"第四",@"第五",@"第六"];
    // for 循环加入按钮
    for (int i = 0; i < topBtnArr.count; i ++) {
        
        UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        topBtn.frame = CGRectMake(VIEW_WIDTH / 5 * i, 0, VIEW_WIDTH / 5, 50);
        [topBtn setTitle:topBtnArr[i] forState:UIControlStateNormal];
        [topBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        topBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        if (i == 0) {
            
            [topBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }else{
            [topBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [topBtn addTarget:self action:@selector(didClickTopBtn:) forControlEvents:UIControlEventTouchUpInside];
        topBtn.tag = 1000 + i;
        [topScrollView addSubview:topBtn];
    }
    
    // 创建 "+" 按钮
    plusBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    plusBtn.frame = CGRectMake(VIEW_WIDTH - 50, 64, 50, 50);
    [plusBtn setTitle:@"+" forState:UIControlStateNormal];
    [plusBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    plusBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [plusBtn addTarget:self action:@selector(didClickTopPlusBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:plusBtn];
    
}
// 头部按钮的点击事件
-(void)didClickTopBtn:(UIButton *)sender
{
    // 改变按钮的标题颜色
    for (int i = 0; i < 6 ; i ++) {
        
        UIButton *topBtn1 = [self.view viewWithTag:1000 + i];
        if (sender.tag == topBtn1.tag) {
            
            [topBtn1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }else{
            [topBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
    // 改变滚动视图的位置
    mainScrollView.contentOffset = CGPointMake((sender.tag - 1000) * VIEW_WIDTH, 0);
    
}
// + 点击事件
-(void)didClickTopPlusBtn:(UIButton *)sender
{
    if (sender.selected == NO) {
        
        sender.selected = YES;
        [UIView animateWithDuration:0.3 animations:^{
            smallTableView.frame = CGRectMake(VIEW_WIDTH / 5 * 3, 64 + 50, VIEW_WIDTH / 5 * 2 - 7, 180);
        }];
        
    }else if (sender.selected == YES){
        sender.selected = NO;
        [UIView animateWithDuration:0.3 animations:^{
            smallTableView.frame = CGRectMake(VIEW_WIDTH - 20, 64 + 50, 0.1, 0.1);
        }];

    }
}

创建内容部分的大滚动视图的自定义方法

// 创建内容部分滚动视图方法
-(void)createMainScrollView
{
    // 创建大的滚动视图
    mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64 + 50, VIEW_WIDTH, VIEW_HEIGHT - 64 - 50)];
    mainScrollView.delegate = self;
    mainScrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:mainScrollView];
    // 设置滚动范围
    mainScrollView.contentSize = CGSizeMake(VIEW_WIDTH * 6, 0);
    mainScrollView.pagingEnabled = YES;
    // for 循环在滚动视图上添加视图
    for (int i = 0; i < 6; i ++) {
        
        // 创建视图
        UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(VIEW_WIDTH * i, 0, VIEW_WIDTH, VIEW_HEIGHT - 64 - 50)];
        // 设置视图的背景颜色为随机颜色
        mainView.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0)green:((float)arc4random_uniform(256) / 255.0)blue:((float)arc4random_uniform(256) / 255.0)alpha:1.0];
        [mainScrollView addSubview:mainView];
    }
}

表格的数据源方法

// 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseID];
    }
    
    // 创建表格内容数组
    NSArray *cellArr = @[@"确认添加",@"确认删除",@"关闭"];
    cell.textLabel.text = cellArr[indexPath.row];
    cell.backgroundColor = [UIColor grayColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    return cell;
}

点击表格单元格

// 表格的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认添加" message:@"操作已完成" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (indexPath.row == 2){
        
        plusBtn.selected = NO;
//        smallTableView.hidden = YES;
        [UIView animateWithDuration:0.3 animations:^{
            smallTableView.frame = CGRectMake(VIEW_WIDTH-10, 64 + 50, 0.1, 0.1);
        }];
    }
}

滚动视图的代理方法 - 设置按钮与滚动视图反关联

// 滚动视图的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 按钮与滚动视图相关联
    for (int i = 0; i < 6; i ++) {
        
        UIButton *topBtn = [self.view viewWithTag:1000 + i];
        if (scrollView.contentOffset.x / VIEW_WIDTH == topBtn.tag - 1000) {
            
            [topBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }else{
            [topBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容