国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS實(shí)現(xiàn)抽屜效果

2020-07-26 01:29:30
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)抽屜效果的具體代碼,供大家參考,具體內(nèi)容如下

抽屜效果:

#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; } //計算當(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ù)偏移量計算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 //對計算的結(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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 渝北区| 兴和县| 晴隆县| 台南市| 仙居县| 北京市| 庆城县| 洛宁县| 舟曲县| 浦江县| 大田县| 吉木萨尔县| 遵化市| 江都市| 珠海市| 昌吉市| 满洲里市| 大化| 铜鼓县| 积石山| 双牌县| 东明县| 永安市| 五家渠市| 石泉县| 邛崃市| 临颍县| 昌乐县| 潞城市| 朝阳区| 蒙阴县| 柞水县| 永和县| 北碚区| 三江| 乌兰县| 平远县| 虹口区| 东乌珠穆沁旗| 吴川市| 宣武区|