2

这就是我在视图上添加手势的方式

- (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]; 
}

一切正常,但是手势在整个视图中我怎么能做手势响应只在一半视图中?

4

2 回答 2

7

为什么不直接使用CGRectContainsPoint()并检查您的视图中的触摸位置是否在您想要的区域内。如果不是,请忽略它:

- (void)panGestureDetected:(UIPanGestureRecognizer *)sender
{
    CGPoint location = [sender locationInView:sender.view];
    CGRect someRect = ...

    if (CGRectContainsPoint(someRect, location)) {
            // point is in specified area
    }
}
于 2013-08-23T13:11:27.250 回答
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]; 
}
于 2013-08-23T13:13:31.080 回答