本文實(shí)例為大家分享了iOS實(shí)現(xiàn)抽屜效果的具體代碼,供大家參考,具體內(nèi)容如下
專業(yè)領(lǐng)域包括做網(wǎng)站、成都網(wǎng)站制作、商城網(wǎng)站建設(shè)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,成都創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。抽屜效果:
#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添加點(diǎn)按手勢 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]; [self.view addGestureRecognizer:tap]; } - (void)tap{ //讓MainV復(fù)位 [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,是因?yàn)槲覀冞€要去修改高度,使用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; } //當(dāng)手指松開時,做自動定位. CGFloat target = 0; if (pan.state == UIGestureRecognizerStateEnded) { if (self.mainV.frame.origin.x > screenW * 0.5 ) { //1判斷在右側(cè) //當(dāng)前View的x有沒有大于屏幕寬度的一半,大于就是在右側(cè) target = targetR; }else if(CGRectGetMaxX(self.mainV.frame) < screenW * 0.5){ //2.判斷在左側(cè) //當(dāng)前View的大的x有沒有小于屏幕寬度的一半,小于就是在左側(cè) target = targetL; } //計(jì)算當(dāng)前mainV的frame. CGFloat offset = target - self.mainV.frame.origin.x; [UIView animateWithDuration:0.5 animations:^{ self.mainV.frame = [self frameWithOffsetX:offset]; }]; } //復(fù)位 [pan setTranslation:CGPointZero inView:self.mainV]; } #define maxY 100 //根據(jù)偏移量計(jì)算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; //當(dāng)拖動的View的x值等于屏幕寬度時,maxY為大,大為100 // 375 * 100 / 375 = 100 //對計(jì)算的結(jié)果取絕對值 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
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。