This is the code I’m using for mouse wheel scrolling. Works from an undocumented property called .scrollY.
It can be adapted slightly to work on tableViews as well.
[lua]
local middleMouseButtonScrollSpeed = 400 ;
local mouseScroll = function (event)
local sv = event.target
local scrollInfo = {
dist = event.scrollY, – not officially part of the Corona SDK
time = event.time-sv.lastTime
}
sv.lastTime = event.time;
– Add it to the scroll info table
sv.scrollInfoTable[#sv.scrollInfoTable+ 1] = scrollInfo;
– Take an average from the last 10 (or up to that if table smaller)
local sampleSize = #sv.scrollInfoTable;
if sampleSize > 10 then
sampleSize = 10 ;
end
local averageDist = 0 ; local averageTime = 0 ;
for i=#sv.scrollInfoTable, (#sv.scrollInfoTable+ 1 -sampleSize), - 1 do
averageDist = averageDist + sv.scrollInfoTable[i] .d ist;
averageTime = averageTime + sv.scrollInfoTable[i] .t ime;
end
averageDist = averageDist / sampleSize;
averageTime = averageTime / sampleSize;
local speed = math.round((averageDist/averageTime) * middleMouseButtonScrollSpeed);
local py, px = sv:getContentPosition();
local currentPosition = py;
local newPosition = currentPosition - speed;
– Don 't allow the scroll content to go off the bottom
– Would be nice to add “bounce” effect at some point
if newPosition > 0 then
newPosition = 0;
end
– Or off the top
local contentHeight = sv._collectorGroup.height;
if contentHeight > sv.ht then
if newPosition < (sv._collectorGroup.height-contentHeight) then
newPosition = sv._collectorGroup.height-contentHeight;
end
else
newPosition = 0;
end
– Do the actual scrolling (if scroll speed isn’t zero)
if speed ~= 0 then
sv:scrollToPosition {
x = 0 ,
y = newPosition,
time = 0 ,
onComplete = nil
}
end
– Remove any extra records from the table so we don’t use up any unnecessary memory
if #sv.scrollInfoTable > sampleSize then
table.remove(sv.scrollInfoTable, 1);
end
end
– assuming your scrollview is named ‘sv’
sv:addEventListener( “mouse”, mouseScroll );
sv.scrollInfoTable = {}
sv.lastTime = system.getTimer()
[/lua]