识别UIScrollView按像素移动

时间:2013-01-21 09:00:47

标签: objective-c uiscrollview

我需要根据屏幕上的位置更改UIScrollView子视图,以便在向下移动时它们会变大,而向下移动则会变大。

有没有办法知道每个像素的变化contentOffset? 我抓住scrollViewDidScroll:方法,但每当移动速度很快时,两次调用之间可能会有200pxls的变化。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

你基本上有两种方法:

  1. 子类UIScrollView并覆盖touchesBegan/Moved/Ended;

  2. 将您自己的UIPanGestureRecognizer添加到当前的UIScrollView

  3. 设置计时器,每次触发时,都会更新您的观看次数_scrollview.contentOffset.x;

  4. 在第一种情况下,您可以使用触摸处理方法:

    - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    
    UITouch* touch = [touches anyObject];
       _initialLocation = [touch locationInView:self.view];
       _initialTime = touch.timestamp;
    
       <more processing here>
    
      //-- this will make the touch be processed as if your own logics were not there
      [super touchesBegan:touches withEvent:event];
    }
    

    我很确定你需要为touchesMoved做到这一点;当手势开始或结束时,不知道你是否还需要特定的东西;在这种情况下,还会覆盖touchesMoved:touchesEnded:。还要考虑touchesCancelled:

    在第二种情况下,你会做类似的事情:

    //-- add somewhere the gesture recognizer to the scroll view
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    panRecognizer.delegate = self;
    [scrollView addGestureRecognizer:panRecognizer];
    
    //-- define this delegate method inside the same class to make both your gesture
    //-- recognizer and UIScrollView's own work together
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
       return TRUE;
    }
    

    第三种情况很难实施。不确定它是否能给出其他两个更好的结果。