UITapGesture仅识别UIImageView的边界

时间:2016-10-01 08:35:40

标签: ios objective-c iphone uitableview uiimageview

以下是代码:

本节写于**-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath**

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageDispalyFullScreenTapped:)];
                singleTap.numberOfTapsRequired = 1;
                singleTap.delegate=self;
                cell.imgDisplay.userInteractionEnabled = YES;
                [cell.imgDisplay addGestureRecognizer:singleTap];

接下来我实现了委托,即**UIGestureRecognizerDelegate**

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

现在在UIImageView被点击时实现选择器

-(void)imageDispalyFullScreenTapped:(UITapGestureRecognizer*)gesture
{
    UIView *tappedView = gesture.view;
    UITableViewCell *cell=[self getSuperViewOfType:[UITableViewCell class] FromView:tappedView];
    NSIndexPath *indexPath=[tblChat indexPathForCell:cell];
    Sock_ModelChat *objChat=[arrDisplay objectAtIndex:indexPath.row];
    if ([objChat.strUrlLocal stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length>0) {
        imgSelectedDisplay=[[Sock_DB sharedInstance] getImageForName:objChat.strUrlLocal];

        ImageDisplayerViewController *imageDisplayer=[ImageDisplayerViewController sharedInstance];
        imageDisplayer.delegate=self;
        [self addChildViewController:imageDisplayer];
        imageDisplayer.view.frame=CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
        [self.view addSubview:imageDisplayer.view];
        [imageDisplayer didMoveToParentViewController:self];
    }
}

但问题是每当点击图像视图的边框侧或边缘时,检测到图像视图上的点击并且选择器 imageDispalyFullScreenTapped 被触发。但是当点击中心或靠近中心,tap没有被检测到,所以相关的selctor也没有开火。

我正在使用XCode版本8.0(8A218a)和iOS 10.0。

1 个答案:

答案 0 :(得分:1)

可能是您的情况下禁用了UserInteraction

然后您可以添加以下手势:

let gesture:UITapGestureRecognizer = UITapGestureRecognizer.init(target:self, action:#selector(handleTap))
gesture.numberOfTapsRequired = 1
gesture.delegate = self
cell.imageView?.addGestureRecognizer(gesture)

然后在viewcontroller中添加以下函数

func handleTap(sender: UITapGestureRecognizer? = nil) {}