IOS笔记UI--侧滑效果的实现

来源:互联网 发布:c语言迷宫最短路径 编辑:程序博客网 时间:2024/06/10 09:21

/* 侧滑的功能

1.点击左上角的button,实现侧滑效果,再次点击恢复
2.点击左上角的button,实现侧滑效果,点击右边任意剩余部分的界面,恢复效果
3.屏幕任意左边(0~20像素)的地方,向右移动(不用垂直,只需要横坐标有变动即可),实现侧滑效果,右边任意剩余部分的界面向左拉,恢复效果
4.屏幕任意左边(0~20像素)的地方,向右移动(不用垂直,只需要横坐标有变动即可),实现侧滑效果,点击右边任意剩余部分的界面,恢复效果 */

效果图:


/*
侧滑效果实现思路
1.创建VC(ViewController),创建NC(UInavigationViewController),把VC加入NC,NC设置为根视图;
2.在NC创建leftBarButtonItem,点击后判断,如果NC的x坐标为0改变坐标为320,如果NC的x坐标为320,改为0;
3.在改变x坐标为320的时候,创建一个UITapGestureRecognizer,x坐标变为0的时候移除UITapGestureRecognizer
4.UITapGestureRecognizer的Click方法是把NC的x坐标变为0;
5.使用touchesBegan和touchesEnded方法计算是否被左拉,是则同样改变x的坐标为320,被右拉则改变x坐标为0;(注意在改变的时候也要创建UITapGestureRecognizer和移除);
*/

附上完整demo代码:http://download.csdn.net/detail/csdn_hhg/9193191

代码部分:

ViewController.m- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor =[UIColor purpleColor];    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(leftClick)];}#pragma mark - 左按钮-(void)leftClick {    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:0.5];    UITapGestureRecognizer *tap;    if (0 == self.navigationController.view.frame.origin.x) {        self.navigationController.view.frame = CGRectMake(320, 0, 375, 667);        tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];        [self.view addGestureRecognizer:tap];    } else {        self.navigationController.view.frame = CGRectMake(0, 0, 375, 667);        [self.view removeGestureRecognizer:tap];    }}#pragma mark 手势-(void)tapClick:(UITapGestureRecognizer*)tap {    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:0.5];    self.navigationController.view.frame = CGRectMake(0, 0, 375, 667);    [self.view removeGestureRecognizer:tap];}#pragma mark - 点击事件-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    UITouch *touch = [touches anyObject];    beginPoint  = [touch locationInView:self.view];}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    UITouch *touch = [touches anyObject];    CGPoint point  = [touch locationInView:self.view];    if (0<=beginPoint.x && beginPoint.x <= 20) {        if (20<=point.x<=357) {            [UIView beginAnimations:nil context:nil];            [UIView setAnimationDuration:0.5];            if (0 == self.navigationController.view.frame.origin.x) {                self.navigationController.view.frame = CGRectMake(320, 0, 375, 667);                MyTouchTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];                [self.view addGestureRecognizer:MyTouchTap];            }        }    }else if(0<=beginPoint.x && beginPoint.x <= 50) {        if (-355<=point.x<=10) {            self.navigationController.view.frame = CGRectMake(0, 0, 375, 667);            [self.view removeGestureRecognizer:MyTouchTap];        }    }}


1 0
原创粉丝点击