1. 回顾:添加手势的步骤
- 初始化时为某视图添加手势及事件
- 实现事件方法
- 设置手势范围
1.1 初始化时添加手势及事件
[self.view addGestureRecognizer:({
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClickAction)];
tap.delegate = self;
tap;
})];
1.2 实现方法事件
- (void)tapClickAction{
//...
}
1.3 设置手势范围 -- 在UIGestureRecognizerDelegate代理中实现
- UIGestureRecognizer方案
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([gestureRecognizer locationInView:self.view].y>CGRectGetMaxY(self.topMenuView.bounds)&&[gestureRecognizer locationInView:self.view].y<self.functionalView.y) {
return YES;
}
}
- UITouch方案
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch locationInView:self.view].y>CGRectGetMaxY(self.topMenuView.bounds)&&[touch locationInView:self.view].y<self.functionalView.y) {
return YES;
}
}
2. 两种类的区别?
UIGestureRecognizer 与UITouch 都有一个 locationInView:
方法。
2.1 视图背景
VC中,self.view添加了一个WKWebView,然后在网页空白除点击。
2.2 实验结果
上面的两种方案中:
-
[touch locationInView:self.view]
返回的是点击点在self.view的位置,返回类型为CGPoint,有效,是正确的点击位置。 -
[gestureRecognizer locationInView:self.view].y
返回为0,失效。
网友评论