开发者

Programmatically pass touches to UIScrollView to scroll

Basically I'm trying to make UIScrollView only scroll on higher angles. As in right now if you move your finger 10 degrees off horizontal, the scrollview will scroll. I'd like to push that up to, say, 30 degrees.

After doing some reading, I established the best way to do this would be to put a subclassed UIView on top of the scrollview. If the UIView on top's touches are above 30 degrees, pass it down to the scrollview, and otherwise don't.

However, I can't figure out how to pass the touches down. Here's my code right now:

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

        [s开发者_如何学Gocroll_view touchesCancelled:touches withEvent:event];
    }
    else 
    {
        [scroll_view touchesEnded:touches withEvent:event];
    }
}

I know right now that it's checking for 45 degrees, but that's not the important thing. What is important is that the touches are indeed getting passed down correctly to my scroll_view. I have it doing a NSLog() on touchesbegan and touchesended, and it's doing both correctly. It's just not scrolling. I'm worried touchesBegan and touchesEnded cannot cause a scroll. Does anyone know what can, or what I'm doing wrong?


I also tried to do the same but didn't succeed. It seems scrollView doesn't handle touchesMoved/touchesBegan, but it handles some other events to understand that user wants to scroll the view. For me the solution was to determine thet shift value and set it explicitly as a scrollView content offset. It looked like this (I don't have the exact source code no, this code may work incorrectly):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜