欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

iOS实现抽屉效果

程序员文章站 2022-05-25 23:04:45
本文实例为大家分享了ios实现抽屉效果的具体代码,供大家参考,具体内容如下抽屉效果:#import "dragerviewcontroller.h"#define screenw [uiscreen...

本文实例为大家分享了ios实现抽屉效果的具体代码,供大家参考,具体内容如下

抽屉效果:

iOS实现抽屉效果

#import "dragerviewcontroller.h"

#define screenw [uiscreen mainscreen].bounds.size.width

@interface dragerviewcontroller ()

@property (nonatomic, weak) uiview *leftv;
@property (nonatomic, weak) uiview *rightv;
@property (nonatomic, weak) uiview *mainv;

@end

@implementation dragerviewcontroller

- (void)viewdidload {
 [super viewdidload];

 //添加子控件
 [self setup];


 //添加手势
 uipangesturerecognizer *pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(pan:)];

 [self.mainv addgesturerecognizer:pan];


 //给控制器的view添加点按手势
 uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tap)];
 [self.view addgesturerecognizer:tap];
}

- (void)tap{
 //让mainv复位

 [uiview animatewithduration:0.5 animations:^{
 self.mainv.frame = self.view.bounds;
 }];

}



#define targetr 275
#define targetl -275
- (void)pan:(uipangesturerecognizer *)pan{

 //获取偏移量
 cgpoint transp = [pan translationinview:self.mainv];
 //为什么不使用transform,是因为我们还要去修改高度,使用transform,只能修改,x,y
 //self.mainv.transform = cgaffinetransformtranslate(self.mainv.transform, transp.x, 0);

 self.mainv.frame = [self framewithoffsetx:transp.x];
 //判断拖动的方向
 if(self.mainv.frame.origin.x > 0){
 //向右
 self.rightv.hidden = yes;
 }else if(self.mainv.frame.origin.x < 0){
 //向左
 self.rightv.hidden = no;
 }

 //当手指松开时,做自动定位.
 cgfloat target = 0;
 if (pan.state == uigesturerecognizerstateended) {

 if (self.mainv.frame.origin.x > screenw * 0.5 ) {
  //1判断在右侧
  //当前view的x有没有大于屏幕宽度的一半,大于就是在右侧
  target = targetr;
 }else if(cgrectgetmaxx(self.mainv.frame) < screenw * 0.5){
  //2.判断在左侧
  //当前view的最大的x有没有小于屏幕宽度的一半,小于就是在左侧
  target = targetl;
 }


 //计算当前mainv的frame.
 cgfloat offset = target - self.mainv.frame.origin.x;
 [uiview animatewithduration:0.5 animations:^{

  self.mainv.frame = [self framewithoffsetx:offset];
 }];


 }


 //复位
 [pan settranslation:cgpointzero inview:self.mainv];

}

#define maxy 100
//根据偏移量计算mainv的frame
- (cgrect)framewithoffsetx:(cgfloat)offsetx {

 nslog(@"offsetx===%f",offsetx);

 cgrect frame = self.mainv.frame;
 nslog(@"x====%f",frame.origin.x);
 frame.origin.x += offsetx;

 //当拖动的view的x值等于屏幕宽度时,maxy为最大,最大为100
 // 375 * 100 / 375 = 100

 //对计算的结果取绝对值
 cgfloat y = fabs( frame.origin.x * maxy / screenw);
 frame.origin.y = y;


 //屏幕的高度减去两倍的y值
 frame.size.height = [uiscreen mainscreen].bounds.size.height - (2 * frame.origin.y);

 return frame;
}


- (void)setup{

 //leftv
 uiview *leftv = [[uiview alloc] initwithframe:self.view.bounds];
 leftv.backgroundcolor = [uicolor bluecolor];
 self.leftv = leftv;
 [self.view addsubview:leftv];
 //rightv
 uiview *rightv = [[uiview alloc] initwithframe:self.view.bounds];
 rightv.backgroundcolor = [uicolor greencolor];
 self.rightv = rightv;
 [self.view addsubview:rightv];
 //mianv
 uiview *mainv = [[uiview alloc] initwithframe:self.view.bounds];
 mainv.backgroundcolor = [uicolor redcolor];
 self.mainv = mainv;
 [self.view addsubview:mainv];
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: iOS 抽屉