抽屉

本文详细介绍了如何在iOS应用中实现一个抽屉菜单。通过在AppDelegate.m和ViewController.m中的代码配置,展示了如何创建并设置菜单的外观和交互,包括背景颜色设定、动画效果和响应触摸事件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:
在AppDelegate.m里
首先导入头文件 #import “ViewController.h”

ViewController *vc = [[ViewController alloc]init];
    
    vc.title = @"抽屉";
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    
    self.window.rootViewController = nav;

第二步:
在ViewController.m

// 左视图
@property (nonatomic,strong) UIView *leftView;

viewDidLoad 里

 // A 视图   本身view的背景颜色
    self.view.backgroundColor = [UIColor redColor];
    
    // B 视图   根视图背景颜色
    [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor cyanColor];
    
    // 设置左视图是否可以与用户交互
    self.leftView.userInteractionEnabled = YES;
    
    self.leftView.frame = CGRectMake(-80, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
    
//    [self.navigationController.view.superview addSubview:self.leftView];
//    
//    [self.navigationController.view.superview sendSubviewToBack:self.leftView];
//    
    //设置左视图的大小
//    self.leftView.frame = self.view.frame;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(left)];
    
}

-(void)left{
    
    // 点击导航按钮 设置页面相对于原来位置变换的位置
    
    //CGAffineTransformMakeTranslation; //相对于屏幕平移的距离
    // CGAffineTransformMakeScale; //平移后相对于原来的大小 这个是按比例计算的
    [UIView animateWithDuration:0.25 animations:^{
        
        self.navigationController.view.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(380, 0), CGAffineTransformMakeScale(0.7, 1.0));
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //切换动画
    [UIView animateWithDuration:0.25 animations:^{
        self.navigationController.view.transform = CGAffineTransformIdentity;
        
        self.leftView.transform = CGAffineTransformIdentity;
        
    }];
    [super touchesBegan:touches withEvent:event];
    
    
}

运行效果:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值