iOS UIPanGestureRecognizer(拖动手势) 和 点击按钮 实现类似左抽屉的效果

本文介绍如何在iOS应用中通过UIPanGestureRecognizer手势和点击按钮,实现类似左抽屉的交互效果。通过创建和配置手势识别器以及响应方法,实现在屏幕上的滑动视图左右移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="objc">//
//  PublicViewController.m
//  slidingTest
//
//  Created by rimi on 15/12/3.
//  Copyright © 2015年 hxm. All rights reserved.
//

#import "PublicViewController.h"
#import "SldingViewController.h"
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
#define MIN_CENTER_X SCREEN_W/6
#define MAX_CENTER_X SCREEN_W/6*4
@interface PublicViewController ()
{
    UIButton *okBtn;
    SldingViewController *sldingView;
}
@end

@implementation PublicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    
    /**< 初始化按钮  */
    okBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    okBtn.frame = CGRectMake(SCREEN_W/30, SCREEN_H/1.2, SCREEN_W/10, SCREEN_W/10);
    okBtn.backgroundColor = [UIColor whiteColor];
    [okBtn setTitle:@"点击" forState:UIControlStateNormal];
    [okBtn addTarget:self action:@selector(respondToOkBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:okBtn];
    
    /**< 添加滑动视图  */
    sldingView = [[SldingViewController alloc]init];
    sldingView.view.backgroundColor = [UIColor orangeColor];
    sldingView.view.frame = CGRectMake(SCREEN_W/6, 0, SCREEN_W, SCREEN_H);
    [self.view addSubview:sldingView.view];
    
    
    /**
     *  拖动手势方法
     */
    [self initializeGestureRecognizer];
    
}

#pragma mark - respondToOkBtn
- (void)respondToOkBtn
{
    //判断,如果滑动视图的中心点 > 最小距离的中心点
    if (sldingView.view.center.x > MIN_CENTER_X + CGRectGetWidth(self.view.bounds)/2) {
        [UIView animateWithDuration:0.4 animations:^{
           /**< 滑动视图的中心点 = 最小距离的中心点 */
        sldingView.view.center = CGPointMake(MIN_CENTER_X+CGRectGetWidth(self.view.bounds)/2,sldingView.view.center.y);
        }];
    }
    else
    {
        /**< 反之则执行 滑动视图的中心点 = 最大距离的中心点  */
        [UIView animateWithDuration:0.4 animations:^{
            sldingView.view.center = CGPointMake(MAX_CENTER_X+CGRectGetWidth(self.view.bounds)/2, sldingView.view.center.y);
        }];
    }
}

#pragma mark -添加拖动手势
- (void)initializeGestureRecognizer
{
    //创建拖动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self
                                                                         action:@selector(respondsToGestureRecognizer:)];
    //将手势添加到滑动视图上
    [sldingView.view addGestureRecognizer:pan];
}
/**< 响应拖动手势  */
- (void)respondsToGestureRecognizer:(UIPanGestureRecognizer*)getsure
{
    //设置移动距离
    CGPoint translation = [getsure translationInView:sldingView.view];
    //设置拖动中心
    CGPoint desCenter;
    desCenter.y = sldingView.view.center.y;

    //设置x轴的变化位置
    CGFloat source_x = sldingView.view.center.x;
    
    //如果移动距离的值 <= 0 时,则向左滑
    if (translation.x <= 0) {
        //MAX 两者之中取最大值
        desCenter.x = MAX(desCenter.x = source_x + translation.x,MIN_CENTER_X + CGRectGetWidth(self.view.bounds)/2);
    }
    else//向右滑
    {
        desCenter.x = MIN(desCenter.x = source_x + translation.x, MAX_CENTER_X + CGRectGetWidth(self.view.bounds)/2);
    }
    //移动的中心点 赋值给 拖动视图的中心点
    sldingView.view.center = desCenter;
    //拖动视图上的拖动手势设置移动的起始坐标为(0,0)
    [getsure setTranslation:CGPointMake(0, 0) inView:sldingView.view];

}
@end

效果如下:

<img src="https://img-blog.youkuaiyun.com/20151203130503982?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值