Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Quick Question: scrollview + tap
Started by dodi_games Oct 05 2018 06:08 PM

4 replies to this topic

Best Answer roaminggamer , 06 October 2018 - 12:44 PM

Here is an example showing two different kinds of touchable objects in a scroller:

 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/scrollers_buttons.zip


local scroller = ssk.vScroller.new( nil, centerX, centerY,
                                    { w = 400, h = 600,
                                      backFill = hexcolor("#DDDDDD") } )


-- ======================================================
-- Tap Replacement Example
-- ======================================================
local multiTapInterval = 333
local function fakeTap( self, event )
   -- Only do work in "ended" phase
   if( event.phase == "ended" ) then
      -- Assign taps if not set yet
      self.taps = self.taps or 0

      -- Detect 'dragged' and show it
      if( event.dragged ) then 
         self.label.text = "dragged"
         self.taps = 0
         return false 
      end

      -- Update label to show consecutive taps occuring
      -- in less time than 'multiTapInterval'

      local dt = event.time - (self.lastTapTime or -10000 )
      self.lastTapTime = event.time
      print( event.time, dt )
      if( dt < multiTapInterval ) then 
         self.taps = self.taps + 1
      else
         self.taps = 1
      end
      self.label.text = self.taps

   end
   return false
end

local tmp = display.newRect( 200, 60, 100, 100 )
tmp:setFillColor(unpack(_O_))
scroller:insert(tmp)
tmp.label = display.newText("0", tmp.x, tmp.y)
scroller:insert(tmp.label)
scroller:addTouch( tmp, fakeTap )



-- ======================================================
-- Basic Button Example
-- ======================================================
local isInBounds       = ssk.easyIFC.isInBounds
local function onPush( self, event )
   if( event.phase == "began" ) then
      self:setFillColor(unpack(_LIGHTGREY_))
   
   elseif( event.phase == "moved" ) then
      if( isInBounds(event,self) ) then
         self:setFillColor(unpack(_LIGHTGREY_))
      
      else
         self:setFillColor(unpack(_DARKGREY_))
      end
   
   elseif( event.phase == "ended" ) then
      self:setFillColor(unpack(_DARKGREY_))
      
      -- Do some action if the button is released, the touch
      -- is still over the button and no drag of the scroller 
      -- happened
      if( isInBounds(event,self) and not event.dragged ) then
         self:setFillColor(unpack(_CYAN_))
         timer.performWithDelay( 100, 
            function()
               self:setFillColor(unpack(_DARKGREY_))
            end )
         
      end
   end
   return false
end

local tmp = display.newRect( 200, 220, 100, 100 )
tmp:setFillColor(unpack(_DARKGREY_))
scroller:insert(tmp)
scroller:addTouch( tmp, onPush )

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 289 posts
  • Corona SDK

Hi guys just want to know if "tap" events accept touch scroll cause I make an app with "touch" listeners and I need to use scrollview:takeFocus(), but now I add a button with a "tap" listener to a scrollview in my game and the scrollview scroll touching or not the button with the "tap" listener.

Thanks in advance
DoDi

[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,093 posts
  • Corona SDK

Disclaimer, I don't use widget buttons. I also don't use tap listeners.  I use touch exclusively.

 

I suggest making your own button code and adding those buttons to the scroller.

 

You can make it work, but you'll have to do some extra coding and custom listeners.  This is intermediate - advanced stuff.

 

i.e. If you want a button in a scroller where you can:

  • touch the button and release it and get a touch
  • touch, then drag on the button and have the scroller drag

The way I handle it is (actually a little more complicated than this, but this is the basic idea),

  1. I have a listener for the touchable object.  It returns false in all cases.  When the touch starts, I keep track of the fact that the button was touched in a table attached to the scroller.
  2. I have a listener for the scroller.   If the player moves their finger beyond a certain threshold, I track this touch sequence as a drag.  Later, when the 'ended' phase occurs, I pass the even manually to the other object's listener and add a field in the event 'dragged = true' (or false) if no drag occurred during this touch.

 

SSK includes code for this, but the feature is not well documented.  I keep it there mostly for myself.

 

If you don't work this out today post back and I'll try to take time to give an example.

 

PS - The issue you encounter when embedding touchable objects into a scroller is that the scroller steals focus and the touched object stops getting events.


Edited by roaminggamer, 06 October 2018 - 07:48 AM.

  • dodi_games likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,093 posts
  • Corona SDK

Tip.  You never 'need' a tap listener.  You can emulate double-tap tracking with a touch listener.


  • dodi_games likes this

[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,093 posts
  • Corona SDK

  Best Answer

Here is an example showing two different kinds of touchable objects in a scroller:

 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/scrollers_buttons.zip


local scroller = ssk.vScroller.new( nil, centerX, centerY,
                                    { w = 400, h = 600,
                                      backFill = hexcolor("#DDDDDD") } )


-- ======================================================
-- Tap Replacement Example
-- ======================================================
local multiTapInterval = 333
local function fakeTap( self, event )
   -- Only do work in "ended" phase
   if( event.phase == "ended" ) then
      -- Assign taps if not set yet
      self.taps = self.taps or 0

      -- Detect 'dragged' and show it
      if( event.dragged ) then 
         self.label.text = "dragged"
         self.taps = 0
         return false 
      end

      -- Update label to show consecutive taps occuring
      -- in less time than 'multiTapInterval'

      local dt = event.time - (self.lastTapTime or -10000 )
      self.lastTapTime = event.time
      print( event.time, dt )
      if( dt < multiTapInterval ) then 
         self.taps = self.taps + 1
      else
         self.taps = 1
      end
      self.label.text = self.taps

   end
   return false
end

local tmp = display.newRect( 200, 60, 100, 100 )
tmp:setFillColor(unpack(_O_))
scroller:insert(tmp)
tmp.label = display.newText("0", tmp.x, tmp.y)
scroller:insert(tmp.label)
scroller:addTouch( tmp, fakeTap )



-- ======================================================
-- Basic Button Example
-- ======================================================
local isInBounds       = ssk.easyIFC.isInBounds
local function onPush( self, event )
   if( event.phase == "began" ) then
      self:setFillColor(unpack(_LIGHTGREY_))
   
   elseif( event.phase == "moved" ) then
      if( isInBounds(event,self) ) then
         self:setFillColor(unpack(_LIGHTGREY_))
      
      else
         self:setFillColor(unpack(_DARKGREY_))
      end
   
   elseif( event.phase == "ended" ) then
      self:setFillColor(unpack(_DARKGREY_))
      
      -- Do some action if the button is released, the touch
      -- is still over the button and no drag of the scroller 
      -- happened
      if( isInBounds(event,self) and not event.dragged ) then
         self:setFillColor(unpack(_CYAN_))
         timer.performWithDelay( 100, 
            function()
               self:setFillColor(unpack(_DARKGREY_))
            end )
         
      end
   end
   return false
end

local tmp = display.newRect( 200, 220, 100, 100 )
tmp:setFillColor(unpack(_DARKGREY_))
scroller:insert(tmp)
scroller:addTouch( tmp, onPush )


  • dodi_games likes this

[TOPIC: post.html]
#5

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 289 posts
  • Corona SDK

@roaminggamer - I really admire you. Thanks for your help. I have not studied the "SSK" thoroughly but I can deduce what you want to explain from your examples. Thank you again!

 

DoDi




[topic_controls]
[/topic_controls]