本文实例为大家分享了iOS实现抽屉效果的具体代码,供大家参考,具体内容如下
抽屉效果:
#进口“DragerViewController.h” #定义screenW [UIScreen mainScreen] .bounds.size.width @ interface DragerViewController () @ property(原子、弱)UIView * leftV; @ property(原子、弱)UIView * rightV; @ property(原子、弱)UIView * mainV; @end @ implementation DragerViewController - (void) viewDidLoad { (超级viewDidLoad);//添加子控件 (自我设置);//添加手势 UIPanGestureRecognizer *潘=[[UIPanGestureRecognizer alloc] initWithTarget:自我行动:@ selector (pan:)]; (自我。mainV addGestureRecognizer:锅);//给控制器的视图中添加点按手势 UITapGestureRecognizer *利用=[[UITapGestureRecognizer alloc] initWithTarget:自我行动:@ selector (tap)]; (自我。视图addGestureRecognizer:利用); } - (void)利用{//让MainV复位 [UIView animateWithDuration: 0.5动画:^ { self.mainV.frame=self.view.bounds; }); } #定义targetR 275 #定义targetL -275 - (void)锅:潘(UIPanGestureRecognizer *) {//获取偏移量 CGPoint透明=[锅translationInView self.mainV):;//为什么不使用变换,是因为我们还要去修改高度,使用变换,只能修改,x, y//self.mainV。变换=CGAffineTransformTranslate (self.mainV。变换,透明。x, 0); self.mainV.frame=(自我frameWithOffsetX: transP.x);//判断拖动的方向 如果(self.mainV.frame.origin.x比;0){//向右 self.rightV。隐藏=是的; }else if (self.mainV.frame.origin.x & lt;0){//向左 self.rightV。隐藏=没有; }//当手指松开时,做自动定位。 CGFloat目标=0; 如果(pan。状态==UIGestureRecognizerStateEnded) { 如果(self.mainV.frame.origin.x比;screenW * 0.5) {//1判断在右侧//当前视图的x有没有大于屏幕宽度的一半,大于就是在右侧 目标=targetR; }else if (CGRectGetMaxX (self.mainV.frame) & lt;screenW * 0.5) {//2 .判断在左侧//当前视图的最大的x有没有小于屏幕宽度的一半,小于就是在左侧 目标=targetL; }//计算当前mainV的框架。 CGFloat抵消=目标——self.mainV.frame.origin.x; [UIView animateWithDuration: 0.5动画:^ { self.mainV.frame=(自我frameWithOffsetX:抵消); }); }//复位 [锅调用setTranslation: CGPointZero针对我国有关矿山截污库坝self.mainV):; } #定义maxY 100//根据偏移量计算MainV的框架 ——(CGRect中)frameWithOffsetX:(CGFloat) offsetX { NSLog (@“offsetX===% f”, offsetX); CGRect中?self.mainV.frame; NSLog (@“x====% f”, frame.origin.x); frame.origin。x +=offsetX;//当拖动的视图的x值等于屏幕宽度时,maxY为最大,最大为100//375 * 100/375=100//对计算的结果取绝对值 CGFloat y=晶圆厂(frame.origin。x * maxY/screenW); frame.origin。y=y;//屏幕的高度减去两倍的Y值 frame.size。身高=[UIScreen mainScreen] .bounds.size。高度(2 * frame.origin.y); 返回帧; } - (void)设置{//leftV UIView * leftV=[[UIView alloc] initWithFrame self.view.bounds):; leftV。写成backgroundColor=[用户界面颜色blueColor]; 自我。leftV=leftV; (自我。视图addSubview leftV):;//rightV UIView * rightV=[[UIView alloc] initWithFrame self.view.bounds):; rightV。写成backgroundColor=[用户界面颜色greenColor]; 自我。rightV=rightV; (自我。视图addSubview rightV):;//mianV UIView * mainV=[[UIView alloc] initWithFrame self.view.bounds):; mainV。写成backgroundColor=[用户界面颜色redColor]; 自我。mainV=mainV; (自我。视图addSubview mainV):; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。