热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

iOS如何用100行代码实现简单的抽屉效果

最近在网上看到一些抽屉效果,看起来很酷!很眩!但是,下不下来看代码,所以决定还是自己写吧!!这篇文章通过近100行的代码就实现了简单的抽屉效果,有需要的朋友们可以参考借鉴,下面来一起看看吧。

前言

iOS中抽屉效果的简单实现现在很多应用中都使用到了,网上也有很多了例子,本文主要是通过简单的一些代码来实现的,有需要的可以一起学习学习。

下面是效果图

示例代码如下

#import 

@interface MainViewController : UIViewController
+ (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController;
@end
#import "MainViewController.h"

#define KWidth self.view.frame.size.width
#define KHeight self.view.frame.size.height

@interface MainViewController ()
@property (nonatomic,strong)UIViewController *leftVC;
@property (nonatomic,strong)UIViewController *centerVC;
@property (nonatomic,assign)BOOL isSlider;
@property (nonatomic,strong)UIView *corverView;
@end


@implementation MainViewController
+ (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController{

  MainViewController *mainVC = [[MainViewController alloc] init];
  mainVC.leftVC = leftViewController;
  mainVC.centerVC = mainPageViewController;
  return mainVC;
}
- (void)viewDidLoad{
  [super viewDidLoad];
  self.isSlider = NO;
  self.view.backgroundColor = [UIColor whiteColor];
  [self addChildViewController:self.leftVC];
  [self.view addSubview:self.leftVC.view];
  [self addChildViewController:self.centerVC];
  [self.view addSubview:self.centerVC.view];
  // 给左视图和主视图添加手势
  [self addGestureForView];
}
// 给主视图添加遮盖
- (void)addCorverView{
  if (self.corverView) {
    [self.corverView removeFromSuperview];
    self.corverView = nil;
  }
  self.corverView = [[UIView alloc] initWithFrame:self.centerVC.view.bounds];
  _corverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
  [self.centerVC.view addSubview:self.corverView];
}
// 移除主视图遮盖
- (void)removeCoverView{
  if (self.corverView) {
    [self.corverView removeFromSuperview];
    self.corverView = nil;
  }
}
// 给左视图和主视图添加手势
- (void)addGestureForView{
  UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
  rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
  [self.centerVC.view addGestureRecognizer:rightGesture];
  UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
  leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
  [self.centerVC.view addGestureRecognizer:leftGesture];
  UISwipeGestureRecognizer *leftVCLeftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftVCLeftSwipeAction:)];
  leftVCLeftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
  [self.leftVC.view addGestureRecognizer:leftVCLeftSwipeGesture];
}
- (void)swipeRightAction:(id)sender{
  [self moveView:self.centerVC.view scale:0.8 panValue:KWidth];
  self.isSlider = YES;
  [self addCorverView];
}
- (void)swipeLeftAction:(id)sender{
  [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2];
  self.isSlider = NO;
  [self removeCoverView];
}
- (void)leftVCLeftSwipeAction:(id)sender{
  [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2];
  self.isSlider = NO;
  [self removeCoverView];
}
// 平移和缩放一个视图
- (void)moveView:(UIView *)view scale:(CGFloat)scale panValue:(CGFloat)value{
  [UIView beginAnimations:nil context:nil];
  view.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale,scale);
  view.center = CGPointMake(value, view.center.y);
  [UIView commitAnimations];
}
@end

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。


推荐阅读
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒的应用方法及特点。ELISA法作为一项新技术在免疫诊断中的应用范围不断扩大,不仅适用于多种病原微生物引起的传染病、非传染病的免疫诊断,也可用于大/小分子抗原的定量检测。ELISA法具有灵敏、特异、简单、快速、稳定及易于自动化操作等特点,是一种早期诊断的良好方法,也可用于血清流行病学调查。MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒使用方法包括对血清和血浆的操作要求。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文介绍了多因子选股模型在实际中的构建步骤,包括风险源分析、因子筛选和体系构建,并进行了模拟实证回测。在风险源分析中,从宏观、行业、公司和特殊因素四个角度分析了影响资产价格的因素。具体包括宏观经济运行和宏经济政策对证券市场的影响,以及行业类型、行业生命周期和行业政策对股票价格的影响。 ... [详细]
author-avatar
手机用户2602890681
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有