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 ) if (event.phase == "began") then scrollSpeedY = 0 deltaY = 0 lastMoveY = event.y end if (event.phase == "moved") then deltaY = event.y - lastMoveY -- Delta Y = finger movement distance from last position -- Always save the three last finger movement distances lastDeltaYArr[3] = lastDeltaYArr[2] lastDeltaYArr[2] = lastDeltaYArr[1] lastDeltaYArr[1] = deltaY lastMoveY = event.y end if (event.phase == "ended") then local xx, yy = event.target:getContentPosition() --event.target.\_view.y yOffs = yy -- Get mean finger movement from the 3 last registered finger positions deltaY = (lastDeltaYArr[1] + lastDeltaYArr[2] + lastDeltaYArr[3])/3 -- Keep on scrolling only if finger movement is big enough if (math.abs(deltaY) \>= MIN\_MOVEMENT\_THAT\_RESULTS\_IN\_AUTOSCROLL) then scrollSpeedY = deltaY\*2 else scrollSpeedY = 0 end end 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( { top = 10, left = 10, width = 300, height = 470, scrollHeight = 4000, horizontalScrollDisabled = true, friction = 0, listener = scrollListener } ) --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 scrollView:setScrollHeight( 4000 ) --function which prolongs the movement after the detachment of the finger local frameRedrawListener = function(event) print(scrollView.\_view.y, scrollView.height) if (math.abs(scrollSpeedY) \> 0.5) then local newY = math.min(0, yOffs + scrollSpeedY) local maxY = (( scrollView.\_view.\_scrollHeight - scrollView.height )\*-1) if(newY \> maxY) then scrollView.\_view.y = newY else newY = maxY scrollView.\_view.y = newY end yOffs = newY -- Reduce the scroll speed (scrolling should go slower and slower) if (scrollSpeedY ~= 0) then if (scrollSpeedY \< 0) then scrollSpeedY = scrollSpeedY + 0.5 else scrollSpeedY = scrollSpeedY - 0.5 end end 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