New scroll in the old ScrollView

Hello everyone,

As the title I created a scroll that moves imitating the ones to which we get addicted.

I have done this because I do not like the scroll that provides the widget, and for the request of a user.

I studied the open source code of widget and wanted to directly change that, but it would require more time. I also opted for a more comprehensive solution for novices. The code is simple hope is that soon would adjust better to scroll some of the staff (surely more clever).

Some of the code has been adapted from the idea of this post: https://forums.coronalabs.com/topic/48585-is-this-really-how-the-scrollview-is-supposed-to-work/

I want to clarify that the solution is not the most “clean”. I focused more on quickness / functionality.

Now I post the code. I wanted to attach it but the crown will not let me:

local widget = require( "widget" ) local \_W = display.contentWidth\*0.5 local \_H = display.contentHeight\*0.5 local MIN\_MOVEMENT\_THAT\_RESULTS\_IN\_AUTOSCROLL = 0 local lastDeltaYArr = {0,0,0} local deltaY=0 local lastMoveY=0 local scrollSpeedY=0 local yOffs = 0 --I store various data in this function fot the automatic scrolling local function scrollListener( event ) &nbsp; &nbsp; if (event.phase == "began") then &nbsp; &nbsp; &nbsp; &nbsp; scrollSpeedY = 0 &nbsp; &nbsp; &nbsp; &nbsp; deltaY = 0 &nbsp; &nbsp; &nbsp; &nbsp; lastMoveY = event.y &nbsp; &nbsp; end &nbsp; &nbsp; if (event.phase == "moved") then &nbsp; &nbsp; &nbsp; &nbsp; deltaY = event.y - lastMoveY -- Delta Y = finger movement distance from last position &nbsp; &nbsp; &nbsp; &nbsp; -- Always save the three last finger movement distances &nbsp; &nbsp; &nbsp; &nbsp; lastDeltaYArr[3] = lastDeltaYArr[2] &nbsp; &nbsp; &nbsp; &nbsp; lastDeltaYArr[2] = lastDeltaYArr[1] &nbsp; &nbsp; &nbsp; &nbsp; lastDeltaYArr[1] = deltaY &nbsp; &nbsp; &nbsp; &nbsp; lastMoveY = event.y &nbsp; &nbsp; end &nbsp; &nbsp; if (event.phase == "ended") then &nbsp; &nbsp; &nbsp; &nbsp; local xx, yy = event.target:getContentPosition() --event.target.\_view.y &nbsp; &nbsp; &nbsp; &nbsp; yOffs = yy &nbsp; &nbsp; &nbsp; &nbsp; -- Get mean finger movement from the 3 last registered finger positions &nbsp; &nbsp; &nbsp; &nbsp; deltaY = (lastDeltaYArr[1] + lastDeltaYArr[2] + lastDeltaYArr[3])/3 &nbsp; &nbsp; &nbsp; &nbsp; -- Keep on scrolling only if finger movement is big enough &nbsp; &nbsp; &nbsp; &nbsp; if (math.abs(deltaY) \>= MIN\_MOVEMENT\_THAT\_RESULTS\_IN\_AUTOSCROLL) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollSpeedY = deltaY\*2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollSpeedY = 0 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp; return true end -- Create the widget -- imposed the friction to 0 so as to avoid the normal scrolling of the scroll local scrollView = widget.newScrollView( &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; top = 10, &nbsp; &nbsp; &nbsp; &nbsp; left = 10, &nbsp; &nbsp; &nbsp; &nbsp; width = 300, &nbsp; &nbsp; &nbsp; &nbsp; height = 470, &nbsp; &nbsp; &nbsp; &nbsp; scrollHeight = 4000, &nbsp; &nbsp; &nbsp; &nbsp; horizontalScrollDisabled = true, &nbsp; &nbsp; &nbsp; &nbsp; friction = 0, &nbsp; &nbsp; &nbsp; &nbsp; listener = scrollListener &nbsp; &nbsp; } ) --created and insert an image very long in the scrollView as an example local longImg = display.newImageRect( "longImg.png", 150, 4000 ) longImg.x = \_W longImg.y = longImg.height\*0.5 scrollView:insert(longImg) --imposed the new maximum ScrollHeight&nbsp; scrollView:setScrollHeight( 4000 ) --function which prolongs the movement after the detachment of the finger local frameRedrawListener = function(event) &nbsp; &nbsp; print(scrollView.\_view.y, scrollView.height) &nbsp; &nbsp; if (math.abs(scrollSpeedY) \> 0.5) then &nbsp; &nbsp; &nbsp; &nbsp; local newY = math.min(0, yOffs + scrollSpeedY) &nbsp; &nbsp; &nbsp; &nbsp; local maxY = (( scrollView.\_view.\_scrollHeight - scrollView.height )\*-1) &nbsp; &nbsp; &nbsp; &nbsp; if(newY \> maxY) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollView.\_view.y = newY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newY = maxY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollView.\_view.y = newY &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yOffs = newY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Reduce the scroll speed (scrolling should go slower and slower) &nbsp; &nbsp; &nbsp; &nbsp; if (scrollSpeedY ~= 0) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (scrollSpeedY \< 0) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollSpeedY = scrollSpeedY + 0.5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scrollSpeedY = scrollSpeedY - 0.5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end end Runtime:addEventListener( "enterFrame", frameRedrawListener )

The only issue on which I am perplexed is “scrollView:setScrollHeight( 4000 )”.

The scroll should not update this field to add objects automatically?

I hope you find it useful.

P.S. sorry for my English