Normally when a ui button registers an event listener, the listener receives both a "press" and a "release" event. However, if you put the button in a scrollView, you only get a "press" event, which means you can't detect when a user releases a button (which is the preferred way to detect a button press).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | local ScrollView = require("scrollView") local topBoundary = display.screenOriginY; local bottomBoundary = display.screenOriginY; local scrollView = ScrollView.new{ top=topBoundary, bottom=bottomBoundary }; local btnSelected = function( event ) print("phase: " .. event.phase) --this is always "press" if (event.phase == "release") then --this never happens, because you don't get the release phase print("button id: ") print(event.id) end end local levelBtn = ui.newButton{ default = "image.png", over = "image.png", id = "1", onEvent = btnSelected } scrollView:insert(levelBtn); |
Could you try to substitute the member function "scrollView:touch" in "function scrollView:touch(event)" with the following snippet and see it it works then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | function scrollView:touch(event) local phase = event.phase print(phase) if( phase == "began" ) then print(scrollView.y) self.startPos = event.y self.prevPos = event.y self.delta, self.velocity = 0, 0 if self.tween then transition.cancel(self.tween) end Runtime:removeEventListener("enterFrame", scrollView ) self.prevTime = 0 self.prevY = 0 transition.to(self.scrollBar, { time=200, alpha=1 } ) -- Start tracking velocity Runtime:addEventListener("enterFrame", trackVelocity) -- Subsequent touch events will target button even if they are outside the contentBounds of button display.getCurrentStage():setFocus( self ) self.isFocus = true elseif( self.isFocus ) then if( phase == "moved" ) then local bottomLimit = screenH - self.height - self.bottom self.delta = event.y - self.prevPos self.prevPos = event.y if ( self.y > self.top or self.y < bottomLimit ) then self.y = self.y + self.delta/2 else self.y = self.y + self.delta end scrollView:moveScrollBar() elseif( phase == "ended" or phase == "cancelled" ) then local dragDistance = event.y - self.startPos self.lastTime = event.time Runtime:addEventListener("enterFrame", scrollView ) Runtime:removeEventListener("enterFrame", trackVelocity) -- Allow touch events to be sent normally to the objects they "hit" display.getCurrentStage():setFocus( nil ) self.isFocus = false return false end end return true end |
Thanks for the suggestion. I don't see any change in behavior though. I still only get the "press" phase, and not the "released" phase.
Good news:
I switched to using coronaui_scrollView. This version does allow the "release" phase.
Bad news:
I can't get this version to scroll at all, which is not ideal for a "scrollView".
The scrollView receives the "moved" phase events but the scrollView never has focus. In other words in coronaui_scrollView.lua, line 104:
1 2 | elseif( self.isFocus ) then ... |