QQ抽屜效果

效果圖

//定義屬性宏

#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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容