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

//根据偏移量计算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];


}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目前市场上很多APP都有抽屉效果的界面,界面大同小异,一下是我个人分析和实现的抽屉效果,我是以代码加注释的方式分析...
    coderwx阅读 763评论 0 0
  • 自己写的简单抽屉效果,大概原理都是一样的,直接放代码。 #import"DrawViewController.h"...
    BEYOND黄阅读 417评论 0 3
  • //定义属性宏 #define LWKeyPath(objc, keyPath) @(((void)objc.ke...
    年轻在于折腾阅读 1,889评论 0 3
  • #import "SSYViewController.h" #define ScreenW [UIScreen m...
    iOSkiller阅读 378评论 0 0
  • 思路:跟控制器(rootVc)添加子控制器(下面三个)的view在跟控制器的 view 上面。(注意三个子控制器的...
    码农耕阅读 373评论 0 0