美文网首页
QQ抽屉效果

QQ抽屉效果

作者: 年轻在于折腾 | 来源:发表于2015-09-07 22:32 被阅读1372次
效果图

//定义属性宏

#define LWKeyPath(objc, keyPath) @(((void)objc.keyPath, #keyPath))

#define screenW [UIScreen mainScreen].bounds.size.width

#define screenH [UIScreen mainScreen].bounds.size.height

#define MaxY100

#define targetR300

#define targetL -220

@interface ViewController()

@property(nonatomic,weak)UIView*mainV;

@property(nonatomic,weak)UIView*leftV;

@property(nonatomic,weak)UIView*rightV;

@end

@implementation ViewController

- (void)viewDidLoad {

[superviewDidLoad];

//添加控件

[selfsetUpAllViews];

//添加手势

[selfsetGesture];

//添加点控手势

[selfsetTapGesture];

//添加监听frame移动方向的kvo

// KVO作用:时刻监听某个对象的某个属性的改变

// _main frame属性的改变

// Observer:观察者

// KeyPath:监听的属性

[_mainVaddObserver:selfforKeyPath:LWKeyPath(_mainV,frame)options:NSKeyValueObservingOptionNewcontext:nil];

}

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context{

if(_mainV.frame.origin.x>0) {

_rightV.hidden=YES;

}elseif(_mainV.frame.origin.x<0){

_rightV.hidden=NO;

}

}

-(void)dealloc{

[_mainVremoveObserver:selfforKeyPath:LWKeyPath(_mainV,frame)];

}

//点控手势

- (void)setTapGesture{

//新建点控手势识别

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

[self.viewaddGestureRecognizer:tap];

}

//点控触发事件

- (void)tap{

[UIViewanimateWithDuration:0.25animations:^{

_mainV.frame= [UIScreenmainScreen].bounds;

}];

}

//添加手势

- (void)setGesture{

//拖拽手势

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

//添加到控件上

[_mainVaddGestureRecognizer:pan];

}

//触发拖拽事件

- (void)panGestureRecognizer: (UIPanGestureRecognizer*)panGR{

//获取手势的偏移量

CGPointtransP = [panGRtranslationInView:_mainV];

//获取x轴的偏移量,相对于上一次

CGFloatoffsetX = transP.x;

//平移

//    _mainV.transform = CGAffineTransformTranslate(_mainV.transform, offsetX, 0);

//    //要想实现相对上个位置的移动,就要实现复位

//    //复位

//    [panGR setTranslation:CGPointZero inView:_mainV];

//使用frame同时实现压缩

_mainV.frame= [selfframeWithOffsetX:offsetX];

//手势操作时,实现相对效果,就要复位

[panGRsetTranslation:CGPointZeroinView:_mainV];

//如果拖拽手势结束

if(panGR.state==UIGestureRecognizerStateEnded) {

CGFloattarget =0;

//判断当前的frame

if(_mainV.frame.origin.x>screenW*0.5) {

//右移一半以上

target =targetR;

}elseif(CGRectGetMaxX(_mainV.frame)

//左移一半以上

target =targetL;

}

//获得x的偏移量

offsetX = target -_mainV.frame.origin.x;

//动画跳转新的frame

[UIViewanimateWithDuration:0.25animations:^{

_mainV.frame= [selfframeWithOffsetX:offsetX];

}];

}

}

- (CGRect)frameWithOffsetX: (CGFloat)offsetX{

//获取当前main的frame

CGRectframe =_mainV.frame;

//计算当前的x,y,w,h

CGFloatx = frame.origin.x+ offsetX;

//去x的绝对值,排除掉左移,y值变大的效果

CGFloattem_x =fabs(x);

//获取最新的y等比例缩放

CGFloaty = tem_x/screenW*MaxY;

//获取最新的高度

CGFloath =screenH-2* y;

//获取最新的缩放值

CGFloatscale = h/screenH;

//获取最新的宽度

CGFloatw =screenW* scale;

returnCGRectMake(x, y, w, h);

}

//添加控件

- (void)setUpAllViews{

//红色

UIView*v1 = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

v1.backgroundColor= [UIColorredColor];

[self.viewaddSubview:v1];

_leftV= v1;

//蓝色

UIView*v2 = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

v2.backgroundColor= [UIColorblueColor];

[self.viewaddSubview:v2];

_rightV= v2;

//黄色

UIView*v3 = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

v3.backgroundColor= [UIColoryellowColor];

[self.viewaddSubview:v3];

_mainV= v3;

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

相关文章

  • QQ抽屉效果

    //定义属性宏 #define LWKeyPath(objc, keyPath) @(((void)objc.ke...

  • 类似QQ抽屉效果--swift

    类似 qq抽屉 效果 git 项目下载地址-----------抽屉;

  • 仿QQ抽屉效果

    仿QQ抽屉效果 效果展示: -QQ效果描述:QQ中的效果是侧滑的效果,当导航控制器从左侧画开的时候,左侧的视图也跟...

  • Android自定义控件:打造自己的QQ空间主页

    前面已经实现过仿QQ的List抽屉效果以及仿QQ未读消息拖拽效果,具体请见:Android自定义控件:类QQ抽屉效...

  • iOS 仿QQ抽屉效果

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

  • iOS仿 QQ抽屉效果

    先来看看 QQ的效果 随着 QQ版本的更新QQ 抽屉效果也更新了好多次,现在的版本个人感觉是返璞归真,简约实用。先...

  • Swift-QQ最新版抽屉侧滑和弹框视图

    导读 简单用Swift写了一个抽屉效果,可以直接使用并且简单;很多软件都运了抽屉效果,比如qq的左抽屉,英雄联盟,...

  • 仿QQ抽屉左侧滑动效果

    AppDelegate中:—————— #import "AppDelegate.h" #import "View...

  • iOS 抽屉框架思想

    很多软件都运了抽屉效果,比如qq的左抽屉,滴滴打车,和uber都运用了抽屉,那么抽屉到底是什么个套路呢,这里讲下我...

  • 浅记-第三方库与其头文件

    MMDrawer 作用:QQ抽屉式左抽屉的的效果注意: OC中 ’+‘ 文件是类别,作用是相当于swift中的扩展...

网友评论

      本文标题:QQ抽屉效果

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