带有包含 UIImageView 的 UITableViewCell 的 UITableView

UITableView with UITableViewCell containing UIImageView

好的,所以我只能滚动到 UIImageViews 的边界之外,但我需要能够在这些视图之上滚动。 UIImageView 包含平移手势、捏合手势,我不确定这是否会导致错误。我将在下面粘贴我的手机代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    imageIndex = indexPath.row

    var cell : XpandDraggableCell = tableView.dequeueReusableCellWithIdentifier("XpandDraggableCell", forIndexPath: indexPath) as! XpandDraggableCell

    cell.imgview.backgroundColor = UIColor(red: 210, green: 211, blue: 213, alpha: 1.0)

    if (horizontal) {
        defaultX = defaultSpacingX * imageIndex; /* E.g 70 * 2 = 140 from origin X. */
    }else{
        defaultY = defaultSpacingY * imageIndex; /* E.g 70 * 2 = 140 from origin Y. */
    }

    cell.imgview.frame = CGRect(x: cell.imgview.frame.origin.x, y: cell.imgview.frame.origin.y, width: 75.0, height: 75.0)
    var cellWidth : CGFloat = cell.contentView.frame.width
    var cellHeight : CGFloat = cell.contentView.frame.height
    cell.imgview.center = CGPoint(x: cell.contentView.frame.origin.x + (cellWidth / 2), y: cell.contentView.frame.origin.y + (cellHeight / 2))
    cell.imgview.image = UIImage(named: images[imageIndex]) /*Image provided*/
    cell.imgview.contentMode = UIViewContentMode.ScaleAspectFit
    cell.imgview.userInteractionEnabled = true
    cell.imgview.tag = imageIndex

    var addToViewPanGesture : UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("imageViewPanned:"))
    cell.imgview.addGestureRecognizer(addToViewPanGesture)

    var doubleTapAddGesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("cellDoubleTapped:"))
    doubleTapAddGesture.numberOfTapsRequired = 2
    cell.imgview.addGestureRecognizer(doubleTapAddGesture)

    cell.imgview.backgroundColor = UIColor.clearColor()
    cell.imgview.layer.backgroundColor = UIColor.clearColor().CGColor
    cell.imgviewbtn.backgroundColor = UIColor.clearColor()
    cell.backgroundColor = UIColor(red: 210, green: 211, blue: 213, alpha: 1.0)

    return cell
}


class XpandDraggableCell : UITableViewCell{
    @IBOutlet var imgviewbtn: UIButton!
    @IBOutlet var imgview: UIImageView!
}

平移和滚动是 "same" 手势。

问题是 imageView 的平移手势识别器正在识别手势并处理它(而不是失败或将触摸传递给 cell/tableView)。

如果您希望能够在任何方向平移您的图像,您可以做的是在 tableViewCell 而不是图像上设置平移手势识别器。

然后检查手势的位置是否在 imageView 内。如果是,则开始手势,以便平移手势能够识别它。当你"pan"在单元格外时,手势不会被平移手势识别,可以被识别为滚动。

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.view];
    CGRect frame = self.imageView.frame;
    if (CGRectContainsPoint(frame, point))
    {
        return YES;
    }
     else
    {
        return NO;
    }
}

更新:

根据您编辑的问题,您需要做的是让您的手势识别器区分拖动和滚动。同样,这些是 相似 手势。

如果你触摸一个图像,然后移动你的手指,iOS怎么知道你是想拖动图像,还是滚动table? 你将不得不做出有根据的猜测:

  • 如果手势向左,则可能是拖拽。平移手势 识别器应该识别手势。

  • 如果手势是向上或向下,则可能是滚动。平移手势 识别器应该使手势失败。

更新二:

下面是平移手势识别器的代码,用于确定触摸是拖动还是滚动。

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *gestureView = [gestureRecognizer view];
    CGPoint translation = [gestureRecognizer translationInView:[gestureView superview]];

    // Check for horizontal gesture
    if (fabsf(translation.x) > fabsf(translation.y))
    {
        return YES;
    }

    return NO;
}

这将阻止平移手势识别器将垂直手势识别为平移,它们应该根据需要滚动 table。

您可以在 similar questions 中找到更多信息。

话虽如此,其他开发人员已通过使用长按手势启动拖动来绕过此平移或滚动 tableView 问题。在这种情况下,很明显任何平移手势都将用于启动拖动,否则任何手势都会滚动 table(或滑动单元格)。

您的应用可能不需要滑动删除,但通常最好不要干扰、冲突或更改用户已知的预期手势的行为。