UITableView:以编程方式滚动内容视图

时间:2010-08-11 11:47:33

标签: iphone uitableview scroll uiscrollview touch

您好我正试图转发我从UITiew收到的触摸,它位于UITableView前面。但是,所以我不能让桌子滚动了(see here)。

所以现在我正在尝试以编程方式进行桌面视图滚动,我正在查看setContentOffset:animated:scrollRectToVisible:animated:,只有第一个成功。
这是我在UITableView的子类中的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"BEGTB");
    //previousXPan = 0.0;

    UITouch * touch = [touches anyObject];
    CGPoint tPoint = [touch locationInView:self]; 
    previousYPan = tPoint.y;

    [super touchesBegan:touches withEvent:event];
    //
    //[super touchesShouldBegin:touches withEvent:event inContentView:self];
    //[[self nextResponder] touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    CGPoint tPoint = [touch locationInView:self]; 


    NSLog(@"MOVTB");
    NSLog(@"BOUNDZ %f, %f, %f, %f", self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.height, self.bounds.size.width);
    NSLog(@"CONTENT SIZE  %f, %f",  self.contentSize.height, self.contentSize.width);

    CGRect rc = [self bounds];
    NSLog(@"%f %f", rc.size.height, rc.size.width);
    CGRect newRect = CGRectMake(rc.origin.x, rc.origin.y + self.contentSize.height, self.contentSize.width, fabs(tPoint.y - previousYPan));
    NSLog(@"BOND RECT %f, %f, %f, %f", rc.origin.x, rc.origin.y, rc.size.height, rc.size.width);
    NSLog(@"NEW RECT %f, %f, %f, %f", newRect.origin.x, newRect.origin.y, newRect.size.height, newRect.size.width);
    //[self scrollRectToVisible:newRect animated:YES];
    NSLog(@"Content OFFSET %f",tPoint.y - previousYPan);
    [self setContentOffset:CGPointMake(rc.origin.x,(tPoint.y - previousYPan) *2.0 )animated:YES];
    previousYPan = tPoint.y;
    //[super touchesMoved:touches withEvent:event];
    //[[self nextResponder] touchesMoved:touches withEvent:event];
}

但结果真的很迟钝和错误。还有更好的主意吗?

1 个答案:

答案 0 :(得分:0)

我几乎找到了一个合适的解决方案(它不像普通的滚动但是......)。

首先,我确定Y轴上表格视图的内容区域中的最大和最小像素数量(当视图已满载时,contentSize 的高度)。

我会逐步检查移动,但不会在表格视图中参考触摸的位置!我必须在覆盖它的顶层UIView上检查它,因为当你改变表格的内容偏移时,你会动态改变它的尺寸,你可以结束加速效果

以下是一些代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];

    CGPoint tPoint = [touch locationInView:[viewController draggableAreaView]];



    CGRect rc = [self bounds];


    float totalOS = tPoint.y - previousYPan;
    if ((contentOffSetY >= minY && contentOffSetY <= maxY) ||
        (contentOffSetY < minY - 5 && totalOS < 0) ||
        (contentOffSetY > maxY + 5 && totalOS > 0))
    {

        if (totalOS > 0)
            totalOS += OFFSET;
        else if (totalOS < 0)
            totalOS -= OFFSET;
        [self setContentOffset:CGPointMake(rc.origin.x,self.contentOffset.y + totalOS)animated:NO];

    }
    else /*if (contentOffSetY < minY - 5 && totalOS < 0) */
    {
        [self resetOffset];
    }
    previousYPan = tPoint.y; 

}

- (void)resetOffset{

    CGRect rc = [self bounds];
    if(contentOffSetY < minY)
    {
        [self setContentOffset:CGPointMake(rc.origin.x, minY + 50)animated:YES];
    }
    else if (contentOffSetY > maxY)
    {
        [self setContentOffset:CGPointMake(rc.origin.x, maxY - 50)animated:YES];
    }
}

更多提示:

  • 这里draggableAreaView是我从中获得“绝对”位置的顶级UIView
  • OFFSET是一个常量,用于增加线性 scroolling的速度(没有它touchesMoved:,多次调用,每次只给出一个像素的相对运动)
  • minYmaxY是预先计算的限制值(为了计算它们,我计算了表格中所有单元格的最大高度和最小可见内容视图的高度)
  • 我向minYmaxY添加了一些位移,以便有更明显的动画