UITableView: scrolling programmatically the content view

后端 未结 1 643
-上瘾入骨i
-上瘾入骨i 2021-01-16 19:08

Hi i\'m trying to forward the touches I receive from an UIView which is in front of an UITableView. But doins so I can\'t make the table scroll anymore (see here).

S

1条回答
  •  离开以前
    2021-01-16 19:35

    I almost found a suitable solution (it is not like a normal scrolling but...).

    First I determine the mamimum and minimum amount of pixels in the content area of the table view on the Y axis ( the height of the contentSize when the view has been fully loaded).

    The I check the movement done incrementally but not referencing the position of the touch in the table view! I have to check it on a top layered UIView that covers it, because when you alter the content offset of the table you change dynamically its dimensions and you can end you having an acceleration effect .

    Here is some code:

    - (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];
        }
    }
    

    Some more tips:

    • here draggableAreaView is the top UIView from which I get an "absolute" position
    • OFFSET is a constant defined to increment linearly the speed of the scroolling (without it the touchesMoved: , called many times, gives a relative movement of just one pixel each time)
    • ,minY and maxY are the precalculated limit values (in order to compute them I calculated the height of all the cells in the table for max and the height of the visible content view for min)
    • I added some displacement to minY and maxY in order to have a more visible animation

    0 讨论(0)
提交回复
热议问题