4

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

有没有办法知道 contentOffset 随着每个像素的变化?我抓住了这个scrollViewDidScroll:方法,但是只要移动很快,两次调用之间可能会有一些 200pxls 的变化。

有任何想法吗?

4

1 回答 1

3

你基本上有两种方法:

  1. 子类UIScrollView和覆盖touchesBegan/Moved/Ended

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

  3. 设置一个计时器,每次触发时,更新您的视图阅读_scrollview.contentOffset.x

在第一种情况下,你会为​​触摸处理方法做:

- (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:and 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;
}

第三种情况实现起来非常简单。不确定它是否会比其他两个提供更好的结果。

于 2013-01-21T09:09:24.893 回答