ios抽屜效果

#import

@interfaceDragViewController :UIViewController

@property (nonatomic, strong, readonly) UIView *mainV;

@property (nonatomic, strong, readonly) UIView *leftV;

@property (nonatomic, strong, readonly) UIView *rightV;

@end


#import "DragViewController.h"

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

@interface DragViewController ()<UIGestureRecognizerDelegate>

@end

@implementationDragViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.


? ? [selfsetUp];


? ? //添加手勢

? ? UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

? ? pan.delegate=self;

? ? [self.mainV addGestureRecognizer:pan];


? ? //控制器的view添加點按手勢

? ? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

? ? [self.view addGestureRecognizer:tap];

}

-(void)tap:(UITapGestureRecognizer *)tap{

? ? //讓mainV復位

? ? [UIView animateWithDuration:0.5 animations:^{

? ? ? ? self.mainV.frame=self.view.bounds;

? ? }];

}

#define targetR screenW *0.5

#define targetL - (screenW *0.5)

-(void)pan:(UIPanGestureRecognizer *)pan{

? ? //獲取偏移量

? ? CGPointtransP = [pantranslationInView:self.mainV];


? ? //為什么不使用transform,是因為我們還要修改高度,這里只能修改x,y

? ? //self.mainV.transform = CGAffineTransformTranslate(self.mainV.transform, transP.x, 0);


? ? self.mainV.frame= [selfframeWithOffsetX:transP.x];


? ? //判斷拖動方向

? ? if(self.mainV.frame.origin.x>0) {

? ? ? ? //向右

? ? ? ? self.rightV.hidden=YES;

? ? }elseif(self.mainV.frame.origin.x<0){

? ? ? ? //向左

? ? ? ? self.rightV.hidden=NO;

? ? }


? ? //當手指松開時自動定位

? ? CGFloattarget =0;

? ? if (pan.state == UIGestureRecognizerStateEnded) {


? ? ? ? if(self.mainV.frame.origin.x>screenW*0.5) {

? ? ? ? ? ? //判斷在右側

? ? ? ? ? ? //當前View的x沒有大于屏幕寬度的一半,大于就是在右側

? ? ? ? ? ? target =targetR;

? ? ? ? }elseif(CGRectGetMaxX(self.mainV.frame)

? ? ? ? ? ? target =targetL;

? ? ? ? }


? ? ? ? //計算當前mainV的frame

? ? ? ? CGFloatoffset = target -self.mainV.frame.origin.x;

? ? ? ? [UIView animateWithDuration:0.5 animations:^{

? ? ? ? ? ? self.mainV.frame= [selfframeWithOffsetX:offset];

? ? ? ? }];


? ? }


? ? //復位

? ? [pansetTranslation:CGPointZero inView:self.mainV];

}

#define maxY100

//根據(jù)偏移量計算mainV的frame

-(CGRect)frameWithOffsetX:(CGFloat)offsetX{

? ? CGRectframe =self.mainV.frame;

? ? frame.origin.x+= offsetX;


? ? CGFloaty =fabs(frame.origin.x*maxY/screenW);

? ? frame.origin.y= y;


? ? //屏幕的高度減去兩倍的y值

? ? frame.size.height = [UIScreen mainScreen].bounds.size.height - (2 * frame.origin.y);


? ? returnframe;

}

-(void)setUp{

? ? UIView*leftV = [[UIViewalloc]initWithFrame:self.view.bounds];

? ? leftV.backgroundColor = [UIColor blueColor];

? ? //self.leftV = leftV;

? ? _leftV= leftV;

? ? [self.viewaddSubview:leftV];


? ? UIView*rightV = [[UIViewalloc]initWithFrame:self.view.bounds];

? ? rightV.backgroundColor = [UIColor greenColor];

? ? //self.rightV= rightV;

? ? _rightV= rightV;

? ? [self.viewaddSubview:rightV];


? ? UIView*mainV = [[UIViewalloc]initWithFrame:self.view.bounds];

? ? mainV.backgroundColor = [UIColor redColor];

? ? //self.mainV = mainV;

? ? _mainV= mainV;

? ? [self.viewaddSubview:mainV];


}

- (void)didReceiveMemoryWarning {

? ? [super didReceiveMemoryWarning];

? ? // Dispose of any resources that can be recreated.

}

@end

注意://當一個控制器的view添加到另一個控制器的view的時候,此時view所在的控制器也應該成為上一個控制器的子控制器


應用:

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.


? ? //當一個控制器的view添加到另一個控制器的view的時候,此時view所在的控制器也應該成為上一個控制器的子控制器

? ? TableViewController *vc1 = [[TableViewController alloc] init];

? ? vc1.view.frame=? self.mainV.bounds;

? ? [self.mainVaddSubview:vc1.view];

? ? [self addChildViewController:vc1];


? ? TableViewController *vc2 = [[TableViewController alloc] init];

? ? vc2.view.backgroundColor = [UIColor redColor];

? ? vc2.view.frame=? self.mainV.bounds;

? ? [self.leftVaddSubview:vc2.view];

? ? [self addChildViewController:vc2];


}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 目前市場上很多APP都有抽屜效果的界面,界面大同小異,一下是我個人分析和實現(xiàn)的抽屜效果,我是以代碼加注釋的方式分析...
    coderwx閱讀 791評論 0 0
  • 自己寫的簡單抽屜效果,大概原理都是一樣的,直接放代碼。 #import"DrawViewController.h"...
    BEYOND黃閱讀 472評論 0 3
  • //定義屬性宏 #define LWKeyPath(objc, keyPath) @(((void)objc.ke...
    年輕在于折騰閱讀 1,942評論 0 3
  • #import "SSYViewController.h" #define ScreenW [UIScreen m...
    iOSkiller閱讀 384評論 0 0
  • 思路:跟控制器(rootVc)添加子控制器(下面三個)的view在跟控制器的 view 上面。(注意三個子控制器的...
    碼農耕閱讀 405評論 0 0

友情鏈接更多精彩內容