您好,登錄后才能下訂單哦!
本文實例為大家分享了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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。