Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Disable scrolling on scrollview by touch

Hi all,

I had a problem where I was using a scrollableview and didn't want the user to be able to scroll it by touching it but wanted them to use some next/back buttons.

If I disabled the touch then none of the controls on the view would work!

I found a solution by enhancing the SDK with a new "scrollEnabled" property in much the same way as touchEnabled. Note this is iOS only.

This is actually supported in iOS with a UIScrollView's "scrollEnabled" property.

Modify the SDK file TiUIScrollableView.m

add this

/* CSM: Provide ability to disable scrollviews scrolling when touched */
-(void)setScrollEnabled_:(id)args
{
    scrollingEnabled = [TiUtils boolValue:args];
    [scrollview setScrollEnabled:scrollingEnabled];
}

Down anywhere (I have it just after this)

-(void)setViews_:(id)args
{
    if ((scrollview!=nil) && ([scrollview subviews]>0))
    {
        [self refreshScrollView:[self bounds] readd:YES];
    }
}

Now you should also add the scrollingEnabled into your TiUIScrollableView.h. I put it at the bottom of the object definition.

    int cacheSize;

    BOOL scrollingEnabled;

Note. You don't need that variable, I just thought it'd be nice to store it.. note I haven't done a gettor…

Then in you JS code you just do

yourscrollableview.scrollingEnabled = true/false

And it now means you can programatically control the scrollable view without people moving them and yet controls on the views (in your array attached to the scrollable view if doing multiple) still respond.

Enjoy.

— asked August 18th 2011 by Chris Moore
  • disable
  • scrollableview
  • touch
8 Comments
  • !!!!!! UPDATE !!!!!!!

    Change the code slightly to include the self reference.

    scrollingEnabled = [TiUtils boolValue:args];
    [[self scrollview] setScrollEnabled:scrollingEnabled];
    

    And it works perfectly.

    — commented August 18th 2011 by Chris Moore
  • Not working for me. Even after using the updated code above, my scrollableView is still scrollable by swiping, not just by the buttons I have designed for that purpose.

    — commented August 23rd 2011 by Luke Bartolomeo
  • I assume you've done a clean build?

    You can check that those changes over in the SDK directory are replicating to your actual build by looking in your projects

    build/iphone/Classes/TiUIScrollableView.m

    Check that it has this modification

    
    /* CSM: Provide ability to disable scrollviews scrolling when touched */
    -(void)setScrollEnabled_:(id)args
    {
        scrollingEnabled = [TiUtils boolValue:args];
        [[self scrollview] setScrollEnabled:scrollingEnabled];
    }
    

    If its not there, and it is way back over in your SDK directory you've either selected the wrong SDK file (easy if you've got lots of SDK versions installed) or else you've not done a clean build.

    I'm using this with 1.7.2 and have no problems with it working, but YMMV.

    Note that the sequence is this

        /*
         * Create a scrollable view of all the signup screens 
         */
    
        $.scroll = Titanium.UI.createScrollableView({
            scrollEnabled: false,
            views:[$.getmobile, $.getpassword, $.getconfirm]
        });
    
    
        $.winNav.add($.scroll);
    

    then I open up the holding window afterwards.. ($.winNav is added to $.win (just a container window) and then $.win is .opened.. )

    In case theres some sequencing.

    — commented August 23rd 2011 by Chris Moore
  • THANK YOU!!! I had just about given up on this.
    You might want to go back and edit the earlier post where you state:

    "Then in you JS code you just do
    yourscrollableview.scrollingEnabled = true/false"

    It looks like the property that should be referenced in JS is scrollEnabled, not scrollingEnabled. Simply changing that in my code fixed it instantly.

    Thanks again!

    — commented August 24th 2011 by Luke Bartolomeo
  • Hey, no problem.

    Oops! Yeah it was late at night on that one and I shouldn't have mixed my naming convention between C / JS like that :-)

    I can't edit the original post, hopefully people read all these comments!

    But yes, in JS as my additional post says, it should be referenced as

    scrollableView.scrollEnabled = false;
    

    Glad it worked, hopefully the Ti guys put this into the full branch as its very useful and ios standard tweak..

    — commented August 24th 2011 by Chris Moore
  • Hi, I am so grateful for this solution !!! It's so easy. It is a MUST for the next release. Thank you.

    — commented August 27th 2011 by Kai Müller
  • +1

    — commented August 28th 2011 by Javier Rayon
  • Vote it up… I'd go through the whole GIT process to add it to the master but I read a few people online saying that the Appcelerator guys never get around to accepting the GIT changes…

    — commented August 28th 2011 by Chris Moore

4 Answers

  • If anyone is still looking for help with this issue here, you should take note that as of Titanium SDK 2.0, you can use the property "scrollingEnabled: false" to disable user scrolling via swipe. You can then use on screen controls to programmatically scroll to the desired view.

    Just to be clear, the property is scrollingEnabled, NOT scrollEnabled.

    — answered June 6th 2012 by Luke Bartolomeo
    permalink
    0 Comments
  • Add the scrollableView to a parentView. Disable touchEnabled property of scrollableView.

    Add the buttons to parentView (NOT to scrollableView), with an higher zIndex.

    Capture clicks from buttons and slide the scrollableView whatever you want, using method scrollToView

    — answered August 19th 2011 by Javier Rayon
    permalink
    1 Comment
    • Hi,

      The problem with this is that you then need to deal with placing controls on the parent view and knowing the positions relevant to scrolling of multiple front views. I had thought of this originally but since my needs are scrolling 3-4 different views it also broke up the logic/code quite a bit too.

      It would be nice if Appcelerator just added the attribute to the scrollable view as the code above shows as its all perfectly legal ios properties.

      — commented August 19th 2011 by Chris Moore
  • Hi Chris,

    I'm having trouble getting this to work. I'm using the latest 1.8 build but it just does not appear to want to work and I'm unable to reference it. (Obviously, I've tried clean builds and it appears within the classes the build is using)

    — answered November 28th 2011 by Rob Chalmers
    permalink
    1 Comment
    • Make sure you read the comments as I had a typo in the original post.

      Otherwise put in some NSLop(@"We got here"); sprinkled around those modifications to see if its getting called etc..

      I've not migrated up to 1.8 yet, but it should still work I'd assume..

      — commented November 29th 2011 by Chris Moore
  • Hi Chris,

    I have tried this SDK hack several times, but can't get it to work. I am using the latest build also. Your help is greatly appreciated.

    — answered March 21st 2012 by Dave E
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.