0

我有一个 UITableView ,它有一个 UIImageView ,通过单击按钮(上/下)一次遍历一行。我现在想做的是只允许用户向上或向下拖动 UIImageView 表(即没有横向移动)。如果 UIImageView 的大部分位于特定单元格上,那么当用户松开手指时,我希望 UIImageView 链接到该行。这是 UITableView 的图像,带有 UIImageView:

在此处输入图像描述

滚动条是需要移动或向下移动的 UIImageView。我意识到我应该实现以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // We only support single touches, so anyObject retrieves just that touch from touches.
    UITouch *touch = [touches anyObject];

    if ([touch view] != _imageView) {

    return;
}

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];


    if ([touch view] == _imageView) {

        return;
    }
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    //here is where I guess I need to determine which row contains majority of the scrollbar.  This would only measure the y coordinate value, and not the x, since it will only be moving up or down.
        return;
    }
}

但是,我不确定如何实现此功能。我试图在网上找到类似的示例,并且查看了 Apple 的 MoveMe 示例代码,但我仍然卡住了。另请注意,我的滚动条与表格中的行大小不完全相同,而是更长一些,但高度相同。

提前感谢所有回复的人

4

1 回答 1

0

尝试将UIPanGestureRecognizer添加到 UIImageView。首先获取图像视图的当前位置,然后使用该translationInView方法确定将图像视图移动到哪里。

来自 Apple 的文档:

如果您想调整视图的位置以使其保持在用户手指下方,请在该视图的父视图的坐标系中请求平移...在首次识别手势时将平移值应用于视图的状态——不要连接该值每次调用处理程序时。

这是添加手势识别器的基本代码:

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];

[imageView addGestureRecognizer:panGesture];

然后,进行数学运算以确定将视图移动到何处。

- (void)panView:(UIPanGestureRecognizer*)sender
{
    CGPoint translation = [sender translationInView:self];

    // Your code here - change the frame of the image view, and then animate
    // it to the closest cell when panning finishes
}
于 2013-06-17T21:22:26.383 回答