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

iOS实现简易抽屉效果、双边抽屉效果

程序员文章站 2023-11-27 19:47:22
本文实例为大家分享了ios实现抽屉效果的全部代码,供大家参考,具体内容如下 ios实现简易抽屉效果,代码: @interface viewcontroller...

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

ios实现简易抽屉效果,代码:

@interface viewcontroller () {
 uiview* _leftview;
}
 
@end
 
@implementation viewcontroller
 
- (void)viewdidload {
 [super viewdidload];
 // do any additional setup after loading the view, typically from a nib.
  
 _leftview = [[uiview alloc] init];
 //把左侧边的view先隐藏
 _leftview.frame = cgrectmake(-200, 0, 200, self.view.frame.size.height);
 _leftview.backgroundcolor = [uicolor greencolor];
 [self.view addsubview:_leftview];
  
 uipangesturerecognizer* pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)];
 [self.view addgesturerecognizer:pan];
}
 
- (void)handlepan:(uipangesturerecognizer*) recognizer {
 cgpoint translation = [recognizer translationinview:self.view];
 //增量后的x坐标位置
 cgfloat xresult = translation.x + _leftview.frame.origin.x;
  
 //向右
 if (translation.x >= 0) {
  //leftview已全部拉出,则无法再向右
  if (_leftview.frame.origin.x >= 0 || xresult >= 0) {
   _leftview.frame = cgrectmake(0, 0, 200, self.view.frame.size.height);
    
   return;
  }
 } else if (translation.x < 0) {//向左
  //leftview已全部收回,则无法再向左
  if (_leftview.frame.origin.x <= -200 || xresult <= -200) {
   _leftview.frame = cgrectmake(-200, 0, 200, self.view.frame.size.height);
   return;
  }
 }
  
 cgrect frame = _leftview.frame;
 frame.origin.x += translation.x;
 _leftview.frame = frame;
  
 //清空移动的距离,这是关键
 [recognizer settranslation:cgpointzero inview:recognizer.view];
  
 //做弹回效果,以中轴为界限
 if (recognizer.state == uigesturerecognizerstateended) {
  if (_leftview.frame.origin.x > -100) {
   [self closeview:no];
  } else {
   [self closeview:yes];
  }
 }
}
 
- (void)closeview:(bool)close {
 if (close) {
  [self moveview:cgrectmake(-200, 0, 200, self.view.frame.size.height)];
 } else {
  [self moveview:cgrectmake(0, 0, 200, self.view.frame.size.height)];
 }
}
 
- (void)moveview:(cgrect)frame {
 [uiview animatewithduration:0.3 animations:^{
  _leftview.frame = frame;
 } completion:^(bool finished) {
   
 }];
}

ios实现双边抽屉效果,代码:

#import "pathview.h"
#import "uiview+additions.h"
 
@implementation pathview
 
- (instancetype)init {
 self = [super init];
 if (self) {
  [self setupgesturerecognizer];
 }
 return self;
}
 
- (void)setupgesturerecognizer {
 uipangesturerecognizer* pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)];
 [self addgesturerecognizer:pan];
}
 
- (void)handlepan:(uipangesturerecognizer*)recognizer {
 cgpoint translation = [recognizer translationinview:self];
  
 cgfloat xresult = translation.x + self.left;
  
 if (translation.x >= 0) {//向右
  if (self.left >= _leftwidth || xresult >= _leftwidth) {
   self.frame = cgrectmake(_leftwidth, 0, self.width, self.height);
   return;
  }
 }
 else if (translation.x < 0) {//向左
  if (self.left <= -_rightwidth || xresult <= -_rightwidth) {
   self.frame = cgrectmake(-_rightwidth, 0, self.width, self.height);
   return;
  }
 }
  
 self.left += translation.x;
  
 if (recognizer.state == uigesturerecognizerstateended) {
  if (self.left > _leftwidth / 2) {
   [self openleft:yes openright:no];
  }
   
  else if (self.left < -(_rightwidth / 2)) {
   [self openleft:no openright:yes];
  }
   
  else {
   [self openleft:no openright:no];
  }
 }
  
 //清空移动的距离
 [recognizer settranslation:cgpointzero inview:recognizer.view];
}
 
- (void)openleft:(bool)left openright:(bool)right {
 if (!left && !right) {
  [self moveview:cgrectmake(0, 0, self.width, self.height)];
   
 } else if (!left && right) {
  [self moveview:cgrectmake(-_rightwidth, 0, self.width, self.height)];
   
 } else if (left && !right) {
  [self moveview:cgrectmake(_leftwidth, 0, self.width, self.height)];
   
 }
}
 
- (void)moveview:(cgrect)frame {
 [uiview animatewithduration:0.3 animations:^{
  self.frame = frame;
 } completion:^(bool finished) {
   
 }];
}

上面是实现的具体内容,所以不做多少注释,只不过把手势移到view里面,让view自己改变自己的frame;再有就是添加了两个属性,来指定左右两边各自抽屉抽出的宽度,某一边不需要,只要设置为0就好了。

以上就是本文的全部内容,希望能够帮助大家顺利实现ios抽屉效果。