iOS如何用100行代码实现简单的抽屉效果
程序员文章站
2023-12-19 08:31:52
前言
ios中抽屉效果的简单实现现在很多应用中都使用到了,网上也有很多了例子,本文主要是通过简单的一些代码来实现的,有需要的可以一起学习学习。
下面是效果图
示例...
前言
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
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。