Allow interactivity in Uiscroolview and prevent interaction with content within it while scrolling

Asked

Viewed 31 times

2

Is there any way to prevent user interaction with content within a Uiscrollview?

I have a Scrollview the contains Uiviews inside it.
In these Uiviews contains only one object, Uiimage.

UIScroolView
 ||
\  /
 \/
----------------------------------------------------------------
          ______    ______
         |      |  |      |
         |      |  |      |   <-- UIview com imagem dentro
         |______|  |______|

----------------------------------------------------------------

I want to allow the user to interact with Uiscrollview, so that he can scroll back and forth, but while he is rolling, he cannot interact with the objects inside (the images). The images feature a tethered drag'n drop event.

The touch and drag effect can only start if the scroll is stopped.

So far, it works as expected, however, interactivity with scroll is disabled for this to happen because I do not know a way to disable only content from within Uiscrollview.

I’m applying userInteractionEnabled and this completely disables the interaction while rolling. When you finish rolling, userInteractionEnabled is set as YES.

- (void)scrollInteractionEnabled:(bool)val :(UIScrollView *)sender
{

    /*
    O objeto UIScrollView está setado como objeto.delegate = self
    */

    //  Esse está e uso e funciona bem.
    // O problema é que fica desativado por completo, não permitindo rolar
    sender.userInteractionEnabled = val;

    /*
    Algumas combinações que tentei, sem sucesso
    */
    //sender.multipleTouchEnabled = YES;         //sender.scrollEnabled = YES;
    //sender.canCancelContentTouches = YES;
}


/*
Os métodos abaixo capturam eventos de rolagem, especificamente quando desacelera e quando para.
Isso é necessário para reverter o valor de userInteractionEnabled
*/
- (void)scrollViewDidScroll:(UIScrollView *)sender{
    [self scrollInteractionEnabled:NO:sender];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    if(!decelerate) {
        [self scrollInteractionEnabled:YES:scrollView];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollInteractionEnabled:YES:scrollView];
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    [self scrollInteractionEnabled:YES:scrollView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollInteractionEnabled:YES:scrollView];
}

Something that seems closer to solving is [objeto_scroll setCanCancelContentTouches:YES]. Obviously I tested and it didn’t work.

I also consider some library, class, or functional use. The reason for all this is the class I use has a bug that when it touches an object inside the scroll while it is rolling, the drag effect is not detected. Only the touch event runs.

[Edit]

Solved as per @Márioklein’s tip

- (void)scrollInteractionEnabled:(bool)val :(UIScrollView *)sender
{
    /*
    Possuo 2 objetos do tipo UIScrollView, por isso foi necessário identifica o Scroll em contexto (onde contém os objetos)
    */
    if (sender == mScrollView) {

        // Itera os objetos de dentro, habilitando e desabilitando a interatividade de cada um individualmente ao invés de aplicar ao UIScrollView.  
        for (UIView* subview in mScrollView.subviews)
        {
            subview.userInteractionEnabled = val;
        }

    } else if (!mScrollView.isDragging || !mScrollView.isDecelerating) {
        // Se o scroll parar de se mover, é habilitada a interatividade nele mesmo. Essa regra ainda continua.
        mScrollView.userInteractionEnabled = YES;
        mScrollView.scrollEnabled = YES;
    }
}

1 answer

3


Daniel,

Try to navigate through the view tree [scrollView subviews] and define them all with userInteractionEnabled = false while performing the scroll. When finished, do the reverse (userInteractionEnabled = true).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.