Hi,
After posting on several threads and messing around with various things I’ve found a way to get Corona widgets scrollview working with the mouse wheel on OSX. Currently I’ve only tested on my Apple Magic Mouse but it works fine with that. It’s a bit of a hack and there may well be an easier way, but I can’t find one. If you know of one, I’d be interested to hear.
It would be great to get this working with the “bounce” effect if you scroll too far as well as adding support for horizontal scrolling, but for the moment it does what I need. Code below. Hopefully someone finds it handy…
This code assumes you have a widget.newScrollview set up, referenced by the variable “sv”…
local scrollInfoTable = {}; local middleMouseButtonScrollSpeed = 100; local lastTime = 0; local h = 320; -- added this line after carloscosta pointed out h was undefined. This is the height of your scrollView. I've just used 320 as an example. -- Here we go... local function mouseScrollwheelEventHandler( event ) -- Create the scroll info (distance and time) local scrollInfo = { dist = event.scrollY, -- not officially part of the Corona SDK time = event.time-lastTime } lastTime = event.time; -- Add it to the scroll info table scrollInfoTable[#scrollInfoTable+1] = scrollInfo; -- Take an average from the last 10 (or up to that if table smaller) local sampleSize = #scrollInfoTable; if sampleSize \> 10 then sampleSize = 10; end local averageDist = 0; local averageTime = 0; for i=#scrollInfoTable, (#scrollInfoTable+1-sampleSize), -1 do averageDist = averageDist + scrollInfoTable[i].dist; averageTime = averageTime + scrollInfoTable[i].time; end averageDist = averageDist / sampleSize; averageTime = averageTime / sampleSize; local speed = math.round((averageDist/averageTime) \* middleMouseButtonScrollSpeed); local px, py = 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 \> h then if newPosition \< (h-contentHeight) then newPosition = h-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 #scrollInfoTable \> sampleSize then table.remove(scrollInfoTable, 1); end end -- Add the mouse event listener. sv:addEventListener( "mouse", mouseScrollwheelEventHandler );