Question

Situation: I have a toolbar of sorts (not a UIToolbar, just a custom UIView) that sits at the top of my view. This toolbar has a number of buttons. Immediately below it, I have a scrollview with a form. When a button on the toolbar is tapped, the corresponding form should be animated onto the screen from the right or the left, depending on which button is tapped. I don't want the toolbar to move on this. However, I do want the toolbar to scroll up when the user scrolls up or down on the scroll view. The way I've accomplished it now is in the scrollViewDidScroll delegate method (note I only allow horizontal scrolling if it's from pressing a button on the toolbar):

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView.contentOffset.x != 0 && _shouldScrollHorizontally == NO)
    {
        CGPoint offset = scrollView.contentOffset;
        offset.x = 0;
        scrollView.contentOffset = offset;
    }

    CGRect buttonFrame = [_buttons frame];

    buttonFrame.origin.y = -scrollView.contentOffset.y + 10;

    [_buttons setFrame:buttonFrame];
}

However, I'm concerned that this is inefficient and will cause some latency. I'm wondering if there's a better way to accomplish what I'm trying to achieve.

At the end of the day, I want the toolbar to remain stationary if the scrollview is scrolled horizontally, but to move with the scrollview if the scrollview is scrolled vertically.

Thanks for any suggestions!

Was it helpful?

Solution

Since horizontal 'scrolling' is only allowed when a button is tapped, then set the UIScrollView contentSize width to the width of the screen and its height as appropriate. For 'scrolling' horizontally on button taps, just animate the moving of a view horizontally.

The view hierarchy would be something like an outerView that is the width and height of what you are setting your UIScrollView contentSize to currently. Your scroll view is a subview of the outerView and the toolbar is a subview of the scroll view.

To animate the horizontal movement, it is typically easiest to change the outerView.center and the toolbarView.center, something like:

CGPoint newCenterPoint;
CGPoint newToolbarCenterPoint;

CGPoint centerPoint1 = // set center as appropriate
CGPoint centerPoint2 = // set center as appropriate
CGPoint centerPoint3 = // set center as appropriate

CGPoint toolbarCenter1 = // set center as appropriate
CGPoint toolbarCenter2 = // set center as appropriate
CGPoint toolbarCenter3 = // set center as appropriate

if (buttonTapped == button1) {
    newCenterPoint = centerPoint1;
    newToolbarCenterPoint = toolbarCenter1;
} else if (buttonTapped == button2){
    newCenterPoint = centerPoint2;
    newToolbarCenterPoint = toolbarCenter2;
} else if (buttonTapped == button3){
    newCenterPoint = centerPoint3;
    newToolbarCenterPoint = toolbarCenter3;
}
[UIView animateWithDuration:0.25 animations:^{
    outerView.center = newCenterPoint;
    toolbarView.center = newToolbarCenterPoint;
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top