如何垂直移动UITableView

时间:2012-05-04 13:47:56

标签: objective-c ios ios5 ios4

我创建了小型iPhone应用程序。我把UITableView放在单元格里面放了一些UIView。我添加了在单元格中水平移动视图的功能。现在我想添加逻辑来垂直移动整个UITableView,如果我在UIView中垂直移动我的手指。那是我的代码:

-(void)moveView:(UIPanGestureRecognizer *)recognizer
{

    if([recognizer state] == UIGestureRecognizerStateBegan)
    {
        CGPoint location = [recognizer locationInView:[self superview]];
        prevPos = location;
        startPosition = self.frame.origin.x;

    }
    else if([recognizer state] == UIGestureRecognizerStateChanged)
    {
        CGPoint location = [recognizer locationInView:[self superview]];
        float delX =  location.x - prevPos.x;
        float delY =  location.y - prevPos.y;
        if(delX > 0 && delX * delX > delY * delY)
        {
            CGPoint np = CGPointMake(self.frame.origin.x+delX, self.frame.origin.y);
            CGRect fr = self.frame;
            fr.origin = np;
            self.frame = fr;
            prevPos = location;
        }
        else if(delX * delX < delY * delY)
        {

        }
    }
    else if([recognizer state] == UIGestureRecognizerStateEnded ||
            [recognizer state] == UIGestureRecognizerStateFailed ||
            [recognizer state] == UIGestureRecognizerStateCancelled)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];

        CGRect rect = self.frame;
        rect.origin.x = startPosition;
        self.frame = rect;
        startPosition = 0;
        [UIView commitAnimations];
    }
}

我应该在这种情况下输入什么代码:else if(delX * delX < delY * delY)

谢谢!

1 个答案:

答案 0 :(得分:1)

如果要滚动TableView,则需要更改TableView的contentOffset属性。

查看setContentOffset:animated:文档