iOS 自定义手势

前言

最近写的项目有一个手势需求:手势右滑触发已背诵过事件。了解了一下手势的用法

代码

全局手势

.h文件中定义一个全局手势属性

@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;

.m文件:
如果使用到这个ViewController的地方都需要它的手势的话,这段代码写在 -(void)viewWillAppear:(BOOL)animated 也可
我这个ViewController可以被两个界面调用,而只有一个需要手势,就写在方法里,需要时调用方法就好

//加入手势
- (void)setHand {`在这里插入代码片`
    //原生方法无效
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    //设置手势方法
    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(toRemember)];
    [self.view addGestureRecognizer:self.panGestureRecognizer];
    
}

局部手势

.h文件里定义手势

/** 左滑手势 */
@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer;

.m文件:
这里是直接写在 -(void)viewWillAppear:(BOOL)animated ,只要使用这个ViewController就会有手势

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //原生方法无效
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

    self.edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(openMenuClick)];
    self.edgePanGestureRecognizer.delegate = self;
    self.edgePanGestureRecognizer.edges = UIRectEdgeRight;
    [self.view addGestureRecognizer:self.edgePanGestureRecognizer];
}

参考文献

iOS-自定义手势操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值