时间:2021-05-20
前言
iOS中抽屉效果的简单实现现在很多应用中都使用到了,网上也有很多了例子,本文主要是通过简单的一些代码来实现的,有需要的可以一起学习学习。
下面是效果图
示例代码如下
#import <UIKit/UIKit.h>@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总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了iOS实现抽屉效果的全部代码,供大家参考,具体内容如下iOS实现简易抽屉效果,代码:@interfaceViewController(){U
导读简单用swift写了一个抽屉效果,可以直接使用并且简单;很多软件都运了抽屉效果,比如qq的左抽屉,英雄联盟,滴滴打车,和uber等等都运用了抽屉;效果ios
本文实例介绍了iOS实现侧拉栏抽屉效果的相关代码,分享给大家供大家参考,具体内容如下需要导入第三方的类库如下:抽屉效果所需第三方类库下载效果:既可以两侧都实现抽
本文实例为大家分享了vue实现抽屉弹窗效果的具体代码,供大家参考,具体内容如下以下代码比较简单。主要就是实现侧边弹窗而且不会影响页面操作的方式,求点赞!!!不多
最近,看到好多Android上的抽屉效果,也忍不住想要自己写一个。在Android里面可以用SlidingDrawer,很方便的实现。IOS上面就只有自己写了。