这就是我在视图上添加手势的方式
- (void)_addPanGestureToView:(UIView *)view {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_handlePan:)];
panGesture.delegate = self;
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
[view addGestureRecognizer:panGesture];
}
一切都很完美,但手势在整个视图中我怎么能做一些像手势只在一半视图中做出反应的东西?
答案 0 :(得分:7)
为什么不使用CGRectContainsPoint()
并检查视图中的触摸位置是否在您希望的范围内。如果不是,请忽略它:
- (void)panGestureDetected:(UIPanGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:sender.view];
CGRect someRect = ...
if (CGRectContainsPoint(someRect, location)) {
// point is in specified area
}
}
答案 1 :(得分:4)
最简单的解决方案是在您希望手势识别器工作的区域添加透明视图,并将手势添加到该视图(当然,该视图作为子视图)。 像这样的东西:
- (void)_addPanGestureToView:(UIView *)view {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_handlePan:)];
panGesture.delegate = self;
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
UIView *viewForGesture = [[UIView alloc] initWithFrame:CGRectMake(....)]; //your frame
[viewForGesture addGestureRecognizer:panGesture];
[view addSubview:viewForGesture];
}