Buttons within a scrollview - no way to detect a Release event?

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).

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);  

There has got to be a way around this right? The scrollView shouldn’t steal the release event from the button.

Also note that I can’t use “onRelease” in the levelBtn creation, because I need access to the button’s id. [import]uid: 52127 topic_id: 10235 reply_id: 310235[/import]

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:

[lua] 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[/lua]

This makes the handler return false after it releases the focus in order to propagate the event to the next listener.

Not sure if it works as I cannot test your code…but it may be worth a try.

-FrankS.
[import]uid: 8093 topic_id: 10235 reply_id: 37510[/import]

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. [import]uid: 52127 topic_id: 10235 reply_id: 37932[/import]

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:

elseif( self.isFocus ) then  
 ...  

This never happens, so the scrollView never moves. [import]uid: 52127 topic_id: 10235 reply_id: 37938[/import]