试图在UITableView上上下拖动UIImageView

时间:2013-06-17 20:24:39

标签: ios uitableview uiimageview

我有一个UITableView,它有一个UIImageView,只需点击一个按钮(向上/向下)就可以一次遍历它。我现在想做的是允许用户仅在表格中向上或向下拖动UIImageView(即没有侧向移动)。如果大多数UIImageView都在特定的单元格上,那么当用户放开他们的手指时,我希望UIImageView链接到该行。这是UITableView的图像,带有UIImageView:

enter image description here

滚动条是需要移动或移动的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示例代码,但我仍然卡住了。另请注意,我的滚动条与表格中的行尺寸不完全相同,而是有点长,但高度相同。

提前致谢所有回复

的人

1 个答案:

答案 0 :(得分:0)

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

来自Apple的文档:

  

如果您想调整视图的位置以将其保留在用户的下方   手指,请求该视图的超视图坐标中的翻译   system ...在首次识别手势时将转换值应用于视图的状态 - 每次调用处理程序时都不会连接值。

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

    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
}