iOS自己写抽屉效果

作者: BEYOND黄 | 来源:发表于2017-05-28 23:30 被阅读63次

自己写的简单抽屉效果,大概原理都是一样的,直接放代码。

#import"DrawViewController.h"

@interfaceDrawViewController()

@property(nonatomic,weak)UIView*leftV;

@property(nonatomic,weak)UIView*rightV;

@property(nonatomic,weak)UIView*mainV;

@end

@implementationDrawViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

[selfsetup];

//添加拖动手势

UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];

[self.mainVaddGestureRecognizer:pan];

//给控制器添加点按手势

UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap)];

[self.viewaddGestureRecognizer:tap];

}

- (void)tap{

//让mainv复位

[UIViewanimateWithDuration:0.5animations:^{

self.mainV.frame=self.view.bounds;

}];

}

#define R275

#define L -275

- (void)pan:(UIPanGestureRecognizer*)pan{

//获取偏移量

CGPointtransp = [pantranslationInView:self.mainV];

//self.mainV.transform = CGAffineTransformTranslate(self.mainV.transform, transp.x, 0);

self.mainV.frame= [selfframeWithOffset:transp.x];

//判断拖动方向

if(self.mainV.frame.origin.x>0){

//右

self.rightV.hidden=YES;

}elseif(self.mainV.frame.origin.x<0){

//左

self.rightV.hidden=NO;

}

//手指松开,自动定位

CGFloattarget =0;

if(pan.state==UIGestureRecognizerStateEnded) {

if(self.mainV.frame.origin.x> [UIScreenmainScreen].bounds.size.width*0.5) {

//右侧,大于屏幕一半

target =R;

}elseif(CGRectGetMaxX(self.mainV.frame) < [UIScreenmainScreen].bounds.size.width*0.5){

//左侧,最大x小于屏幕一半

target =L;

}

CGFloatoffset = target -self.mainV.frame.origin.x;

[UIViewanimateWithDuration:0.5animations:^{

self.mainV.frame= [selfframeWithOffset:offset];

}];

}

//复位

[pansetTranslation:CGPointZeroinView:self.mainV];

}

//固定向下偏移的最大距离

#define MAXY100

//根据偏移量计算mainV的frame

- (CGRect)frameWithOffset:(CGFloat)Offset{

CGRectframe =self.mainV.frame;

frame.origin.x+= Offset;

//向左移动时,y为负值,所以要取绝对值

frame.origin.y=fabs(frame.origin.x*MAXY/[UIScreenmainScreen].bounds.size.width);

//下方也要上移,所以mainV的高度要减去两倍

frame.size.height= [UIScreenmainScreen].bounds.size.height-2*frame.origin.y;

returnframe;

}

- (void)setup{

//左

UIView*leftV = [[UIViewalloc]initWithFrame:self.view.bounds];

leftV.backgroundColor= [UIColorblueColor];

self.leftV=leftV;

[self.viewaddSubview:leftV];

//右

UIView*rightV = [[UIViewalloc]initWithFrame:self.view.bounds];

rightV.backgroundColor= [UIColorgreenColor];

self.rightV=rightV;

[self.viewaddSubview:rightV];

//main

UIView*mainV = [[UIViewalloc]initWithFrame:self.view.bounds];

mainV.backgroundColor= [UIColorredColor];

self.mainV= mainV;

[self.viewaddSubview:mainV];

}

@end

相关文章

  • iOS自己写抽屉效果

    自己写的简单抽屉效果,大概原理都是一样的,直接放代码。 #import"DrawViewController.h"...

  • iOS 抽屉效果

    效果图 平时开发中经常会用到抽屉效果,关于抽屉的实现有许多三方库,读者可以根据需要选用,本节内容主要简单的实现一个...

  • iOS 抽屉效果

    抽屉效果思路: 三个View叠加,一个作为左View,一个作为右View,一个主View,在主View上添加拖动手...

  • iOS抽屉效果

    我们在用QQ时都会发现,消息列表向左滑动时,左侧的功能界面被显示出来,消息列表会拉到最右侧, 就像一个抽屉拉出来一...

  • ios抽屉效果

    #import @interfaceDragViewController :UIViewController @p...

  • iOS 抽屉效果 ViewDeck

    抽屉效果目前比较有名的有第三方RESideMenu和MMDrawerController。但是项目要求抽屉效果为拉...

  • iOS抽屉效果封装

    一个简单的抽屉效果,简单实用!整体思路:主要是在控制器的View上添加了两个View,一个左侧leftView和一...

  • iOS笔记--抽屉效果

  • iOS开发-抽屉效果

    抽屉效果以前比较多,现在看到的比较少,手Q和今日头条现在侧边滑动通过抽屉的方式实现,关于第三方的抽屉效果有很多,稍...

  • iOS 仿QQ抽屉效果

    效果演示:效果图代码结构 项目地址:https://github.com/douxindong/Drawer-QQ...

网友评论

    本文标题:iOS自己写抽屉效果

    本文链接:https://www.haomeiwen.com/subject/dqvhfxtx.html