前言
最近写的项目有一个手势需求:手势右滑触发已背诵过事件。了解了一下手势的用法
代码
全局手势
.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];
}