How to use swipe left/right feature on scroll view widget?

Hello everyone,

I want to use swipe feature on scroll view widget. Actually the problem is when I try to swipe left or right on scroll view widget, it never calls swipe function. Only scroll listener is being called every time.

The task is to change the text inside scroll view if a user swipes left or right direction and also should be able to scroll up and down.

Here is my code,

local getText local getHadithNo = 1 local function scrollListener( event ) &nbsp;&nbsp;&nbsp; local phase = event.phase&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; if ( phase == "began" ) then print( "touched" ) &nbsp;&nbsp;&nbsp; elseif ( phase == "moved" ) then print( "moved" ) &nbsp;&nbsp;&nbsp; elseif ( phase == "ended" ) then print( "released" ) &nbsp;&nbsp;&nbsp; end &nbsp; &nbsp;&nbsp;&nbsp; if ( event.limitReached ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( event.direction == "up" ) then print( "bottom limit" ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif ( event.direction == "down" ) then print( "top limit" ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif ( event.direction == "left" ) then print( "right limit" ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif ( event.direction == "right" ) then print( "left limit" ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp; &nbsp;&nbsp;&nbsp; return true end local scrollView = widget.newScrollView( &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; top = 180, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; left = 0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = display.contentWidth + 4, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; height = display.contentHeight - 178, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; topPadding = 10, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bottomPadding = 10, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; horizontalScrollDisabled = true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; verticalScrollDisabled = false, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hideBackground = true,&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listener = scrollListener &nbsp;&nbsp;&nbsp; } ) local function displayText()&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;getText = display.newText(\_G.getClass.getHadith("No: "..getHadithNo) , 0, 0, display.contentWidth - 15, 0, "BRONSON.TTF", vals[3])&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;getText:setTextColor(255, 255, 255) &nbsp;&nbsp; &nbsp;getText.anchorY = 0&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;getText.x = display.contentCenterX&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;scrollView:insert(getText) end local prevHadith = function(event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if event.phase == "ended" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if getHadithNo \> 1 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;getHadithNo = getHadithNo - 1&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;getHadithText.text = "Hadith Qudsi "..getHadithNo&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; scrollView:remove(getText) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; getText.text = \_G.getClass.getHadith("No: "..getHadithNo)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; scrollView:insert(getText)&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end end local nextHadith = function(event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if event.phase == "ended" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if getHadithNo \< 41 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;getHadithNo = getHadithNo + 1&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;getHadithText.text = "Hadith Qudsi "..getHadithNo&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scrollView:remove(getText) &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;getText.text = &nbsp;\_G.getClass.getHadith("No: "..getHadithNo)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scrollView:insert(getText) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end end local function swipe(event) &nbsp;if ( event.phase == "ended" or event.phase == "cancelled" ) then &nbsp;&nbsp;&nbsp; local dif = event.xStart-event.x &nbsp;&nbsp;&nbsp; if dif \> 30 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nextHadith(event)&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; elseif dif \< -30 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prevHadith(event)&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; end &nbsp;end &nbsp;return true end function myMain() &nbsp;&nbsp; &nbsp;displayText()&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;Runtime:addEventListener("touch", swipe) end myMain()

Any help would be highly appreciated.

Thanks,

Why do you have your horizontal scrolling disabled? The idea behind the scroll view is that it scrolls in the direction you allow. If you want more of a swipe and jump to a new screen, I’m not sure a scrollView is what you want.

Rob

I enabled horizontal scrolling. It did the work :slight_smile: I called functions on left / right scrolling when its limit is reached.

Thanks

Why do you have your horizontal scrolling disabled? The idea behind the scroll view is that it scrolls in the direction you allow. If you want more of a swipe and jump to a new screen, I’m not sure a scrollView is what you want.

Rob

I enabled horizontal scrolling. It did the work :slight_smile: I called functions on left / right scrolling when its limit is reached.

Thanks