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

/* 侧滑的功能

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];
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值