我需要根据屏幕上的位置更改UIScrollView
子视图,以便在向下移动时它们会变大,而向下移动则会变大。
有没有办法知道每个像素的变化contentOffset?
我抓住scrollViewDidScroll:
方法,但每当移动速度很快时,两次调用之间可能会有200pxls的变化。
有什么想法吗?
答案 0 :(得分:3)
你基本上有两种方法:
子类UIScrollView
并覆盖touchesBegan/Moved/Ended
;
将您自己的UIPanGestureRecognizer
添加到当前的UIScrollView
。
设置计时器,每次触发时,都会更新您的观看次数_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:
和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;
}
第三种情况很难实施。不确定它是否能给出其他两个更好的结果。