以编程方式将触摸传递给UIScrollView以进行滚动

时间:2010-03-10 18:57:46

标签: iphone uiscrollview

基本上我试图让UIScrollView只在更高的角度滚动。就像现在如果你将手指向水平移动10度,滚动视图将滚动。我想把它推到30度。

在做了一些阅读之后,我确定了最好的方法是将子类UIView放在scrollview的顶部。如果顶部触摸的UIView高于30度,则将其传递到滚动视图,否则不要。

但是,我无法弄清楚如何传递这些东西。这是我现在的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"glass touch began");
    UITouch *touch = [touches anyObject];
    beginning_touch_point = [touch locationInView:nil];
    [scroll_view touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"glass touch ended");
    UITouch *touch = [touches anyObject];
    CGPoint previous_point = beginning_touch_point;
    CGPoint current_point = [touch locationInView:nil];

    float x_change = fabs(previous_point.x - current_point.x);
    float y_change = fabs(previous_point.y - current_point.y);

    if(x_change > y_change)
    {
        if(previous_point.x - current_point.x < 0)
        {
            [(MyScheduleViewController *)schedule_controller didFlickLeft];
        }
        else
        {
            [(MyScheduleViewController *)schedule_controller didFlickRight];
        }

        [scroll_view touchesCancelled:touches withEvent:event];
    }
    else 
    {
        [scroll_view touchesEnded:touches withEvent:event];
    }
}

我现在知道它正在检查45度,但这不重要。重要的是,触摸确实正确地传递到我的scroll_view。我在touchesbegan和touchesended上做了一个NSLog(),它正确地做了两个。它只是不滚动。我担心touchesBegan和touchesEnded不能引起滚动。有谁知道什么可以,或者我做错了什么?

1 个答案:

答案 0 :(得分:0)

我也尝试过这样做,但没有成功。似乎scrollView不处理touchesMoved / touchesBegan,但它处理一些其他事件以了解用户想要滚动视图。 对我来说,解决方案是确定移位值并将其显式设置为scrollView内容偏移量。它看起来像这样(我没有确切的源代码否,这段代码可能无法正常工作):

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

    CGPoint curr = [touch locationInView: self];
    CGPoint offset = [scrollView contentOffset];

    [scrollView setContentOffset: CGPointMake(offset.x + (curr.x - prev.x), offset.y) animated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    prev = [[touches anyObject] previousLocationInView: self];
}