ScrollView question

I’m trying to have a “tapable” object in a scrollview that “do something” when its tapped, but if it’s touched, held and moved the scrollView instead should scroll.

The below is a simplified code snippet showing the two objects in question:

local widget = require("widget") local w = display.contentWidth local h = display.contentHeight local rectTouch = function(e) if (e.phase == "ended") then e.target:setFillColor(1,0,0) end return true -- touch handled end local topScrollView = widget.newScrollView { left = 0, top = 0, width = w, height = h/6, scrollWidth = w\*4, horizontalScrollDisabled = false, verticalScrollDisabled = true, backgroundColor = {0.25,0.3,0.35} } local rect = display.newRect(w/2, h/12, h/12, h/12) rect:addEventListener( "touch", rectTouch ) topScrollView:insert(rect)

I’ve tried letting the rect object handle all the touch input and hard-setting the scroll value of the scrollview (based on the number of pixels moved (e.startX - e.x type)), but the scroll movement just becomes jerky. There must be a better way?

Yes, there is a better way: rect:addEventListener( “tap” , rectTouch ) – this reacts on taps only, doing the magic for you.

Something like this should do the trick.

 [lua]

local rectTouch = function(e)

      if (e.phase == “began”) then

        display.getCurrentStage():setFocus(e.target) – set focus on the button to stop scrollView taking control of this touch

      end

      if (e.phase == “moved”) then                    – give scrollView the focus back if finger moved

      local dx = math.abs((event.x - event.xStart)+ (event.y - event.yStart ))            – work out how far finger moved

      if dx > 2 then

            display.getCurrentStage():setFocus( nil )       – remove focus from button

            event.target = topScrollView._view

            event.phase = “began”

            gfx.topScrollView._view.touch( topScrollView._view, event )       – simulate a ‘began’ touch event on scrollView

      end

      end

      if (e.phase == “ended”) then

      e.target:setFillColor(1,0,0)

      end

      return true – touch handled

end

[/lua]

You could use the tap event as memo says, but I’ve found they can be unreliable on some Android devices.

Thanks! To both of you!

Yes, there is a better way: rect:addEventListener( “tap” , rectTouch ) – this reacts on taps only, doing the magic for you.

Something like this should do the trick.

 [lua]

local rectTouch = function(e)

      if (e.phase == “began”) then

        display.getCurrentStage():setFocus(e.target) – set focus on the button to stop scrollView taking control of this touch

      end

      if (e.phase == “moved”) then                    – give scrollView the focus back if finger moved

      local dx = math.abs((event.x - event.xStart)+ (event.y - event.yStart ))            – work out how far finger moved

      if dx > 2 then

            display.getCurrentStage():setFocus( nil )       – remove focus from button

            event.target = topScrollView._view

            event.phase = “began”

            gfx.topScrollView._view.touch( topScrollView._view, event )       – simulate a ‘began’ touch event on scrollView

      end

      end

      if (e.phase == “ended”) then

      e.target:setFillColor(1,0,0)

      end

      return true – touch handled

end

[/lua]

You could use the tap event as memo says, but I’ve found they can be unreliable on some Android devices.

Thanks! To both of you!