拖动UITableView [英] Dragging UITableView

查看:93
本文介绍了拖动UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iPhone应用程序,我想将表格视图(不是单元格)拖动到屏幕中的某个点。我的桌面视图位于屏幕的下半部分,上半部分有一个图像。

I'm working on an iPhone application where I want to drag the table view (not the cells) upto a certain point in the screen. I have the tableview sitting in the bottom half of the screen and I have an image in the top-half.

当我滚动表格查看下面的行时,桌子应该实际向上移动到图像上方(y pos减小,高度将增加)。表格将一直向上,直到它填满整个屏幕,为图像留下几个像素,并且它停靠在那里,从那时起,表格应滚动。同样的行为也可以向下滚动。

When I scroll the table to see the rows below, the table should actually move up going above the image (y pos decreases and the height will increase). Table will go up until it fills the entire screen leaving a few pixels for the image and it docks there and from that point onwards, the table should scroll. Same behavior is expected for scrolling down as well.

这可能听起来有点奇怪,但这是一种新的iOS模式正在出现。

This might sound a little weird but this is a new iOS pattern that's emerging.

我设法使用下面的代码片段完成第一部分。

I managed to do the first part using the code snippet below.

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanForScrollView:)];
panGestureRecognizer.maximumNumberOfTouches = 1;
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 1;
[self.tableView addGestureRecognizer:panGestureRecognizer];

- (void)handlePanForScrollView:(UIPanGestureRecognizer *)gesture {
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            break;
        case UIGestureRecognizerStateChanged: {

            CGPoint movement = [gesture translationInView:self.view];
            CGRect frame = self.tableView.frame;
            frame.origin.y += movement.y;
            if(frame.origin.y >= Y_OFFSET && frame.origin.y < self.original_frame.origin.y){
                frame.size.height -= movement.y;
                self.tableView.frame = frame;
                [gesture setTranslation:CGPointZero inView:self.view];
            }
            break;
        }
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:{
            //NSLog(@"End");
            break;
        }
        default:
            ;
            break;
    }
}

这个工作正常,桌子向上移动并停靠低于Y_OFFSET。但正如你想象的那样,桌子不再滚动了。

This is working fine and the table moves up and docks below the Y_OFFSET. But as you would have imagined, the table doesn't scroll any more.

所以我试过这个

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if(self.tableView.frame.origin.y <= Y_OFFSET ||  self.tableView.frame.origin.y >= self.original_frame.origin.y){
        NSLog(@"Here");
        return YES;
    }   
    return NO;
}

此处消息在控制台上打印但表格没有滚动。

"Here" message is getting printed on the console but the table doesn't scroll.

我甚至尝试将panGestureRecognizer用于UITableView而不是我的UIPanGestureRecognizer。

I even tried using the panGestureRecognizer for the UITableView instead of my UIPanGestureRecognizer.

self.tableView.panGestureRecognizer addTarget:self action:@selector(handlePanForScrollView:)

但现在桌子滚动并向上移动。

But now the table scrolls and moves up.

我希望表格不滚动,但要移动到屏幕上的某个点,然后停止移动并开始滚动。

"I want the table not to scroll but to move until a certain point on the screen and then stop moving and start scrolling."

最好的方法是什么?

干杯

推荐答案

我认为你这样做的方式很好。您只需要停止UIGestureRecognizer在某个点之后处理任何更多的触摸。所以我会做的(对现有代码进行最少的更改)只是覆盖委托函数:

I think the way you're doing it is fine. You just need to stop the UIGestureRecognizer from handling any more touches after a certain point. So what I would do (with the least amount of change to your existing code) is just override the delegate function:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

在该函数中,分析表视图的位置,如果表视图已达到某个高度,则返回NO。这将阻止UIPanGestureRecognizer接受触摸事件并将其传递给链,这应该直接进入表视图的UIScrollView。

In that function, analyze the position of the table view and if the table view has reached a certain height already, return NO. This will stop the UIPanGestureRecognizer from accepting the touch event and pass it up the chain, which should go straight to the UIScrollView of the table view.

替代方案,如果我有时间来计算这个,我会尝试使用图像作为表的标题(self.tableView.tableHeaderView)来实现这一点。这样,所有滚动都在表的滚动视图内完成,并且表的框架不必移动。如果你想为tableHeaderView添加一些很酷的效果,你总是可以覆盖UIScrollViewDelegate函数 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 并稍微移动一下表格向上滚动初始半屏幕(这与Path的效果相同)。但是,这不允许您将图像固定在表格视图的最顶部...整个图像将滚动。只需2美分。 :)

Alternative, if I had the time to figure this out, I would try to accomplish this with the image as the table's header (self.tableView.tableHeaderView). This way all of the scrolling is done within the the scrollview of the table and the table's frame doesn't have to move. If you wanted to add some cool effects to the tableHeaderView, you could always override the UIScrollViewDelegate function - (void)scrollViewDidScroll:(UIScrollView *)scrollView and slightly shift the image in the header as the table scrolls up the initial half screen (this is the same effect that Path does). However, this wouldn't allow you to peg the image at the very top of the table view... the entire image would scroll away. Just my 2 cents. :)

这篇关于拖动UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆