Type Function Library widget.* Return value ScrollViewWidget Revision Release 2022.3683 Keywords widget, scrollview, scrollbox, scrolling content See also ScrollViewWidget
Creates a ScrollViewWidget object.
The ScrollViewWidget objects contains a mask which constrains its view to the defined width and height. There is a nested masking limit of 3, so care must be taken when inserting masked objects into other masked objects which act as containers, including display.newContainer, widget.newScrollView, widget.newTableView, or a masked display group. Other display objects that utilize masks include display.newText, display.newEmbossedText, and any other masked display object. For example, a text object (one mask) inside a container (one mask) inside yet another container (one mask) would reach but not exceed the limit of 3 nested masks.
The ScrollViewWidget widget does not support scaling nor changing the width/height via .width or .height.
widget.newScrollView( options )
This function takes a single argument, options
, which is a table that accepts the following parameters:
String. An optional identification string to assign to the scroll view. Default is widget_scrollView
.
Numbers. Coordinates for the widget's x and y center point. These values will be overridden by left
and top
if those values are defined.
Numbers. The left and top position where the widget will be created. If specified, these values override the x
and y
parameters.
Numbers. The visible (on screen) width and height of the scroll view.
Numbers. The width and height of the total scrollable content area. This can be changed after the scroll view widget has been created via the object:setScrollWidth() and object:setScrollHeight() methods. Note that these values automatically update when you insert objects into the scroll view.
Number. Determines how fast the content travels when it is flicked. The default value is 0.972
.
Boolean. If set to true
, the scroll view will not scroll horizontally.
Boolean. If set to true
, the scroll view will not scroll vertically.
Boolean. If set to true
, the scroll view will be locked, meaning it cannot be scrolled.
Boolean. If set to false
, the bounce effect at the limits of the scrolling will be disabled. Default is true
.
Listener. The function used to listen for ScrollViewWidget motion/scroll events. In this listener function, event.phase
returns the expected touch interaction phases of "began"
, "moved"
, or "ended"
. In addition, a "stopped"
phase can be detected when the scroll view flick/swipe momentum naturally comes to a stop. Finally, the scroll limit in any direction can be detected by event.limitReached
. If this occurs, event.direction
returns the direction in which the scroll view was moving when the limit was reached.
Table. Table containing the RGB+A background color setting. Default is white.
backgroundColor = { 0.8, 0.8, 0.8 }
Boolean. If true
, the background of the scroll view will be hidden, but it still receives touches.
Boolean. If set to true
, the scroll bar will not appear for the scroll view.
Table. Table containing the image sheet specifications for a custom scroll bar. Customizing the scroll bar requires 3 frames from an image sheet. Each frame needs to be a square of equal size. Inside the scrollBarOptions
table, you must specify 4 required parameters: sheet
, topFrame
, middleFrame
, and bottomFrame
.
scrollBarOptions = { sheet = scrollBarSheet, -- Reference to the image sheet topFrame = 1, -- Number of the "top" frame middleFrame = 2, -- Number of the "middle" frame bottomFrame = 3 -- Number of the "bottom" frame }
Numbers. The number of pixels from the top and bottom of the scroll view at which the scrollable content area will stop when it reaches the top/bottom limits. The default value for both is 0
.
Numbers. The number of pixels from the left and right of the scroll view at which the scrollable content area will stop when it reaches the left/right limits. The default value for both is 0
.
Visual content can be added to or removed from a scroll view via the object:insert() and object:remove() functions respectively.
local widget = require( "widget" ) -- ScrollView listener local function scrollListener( event ) local phase = event.phase if ( phase == "began" ) then print( "Scroll view was touched" ) elseif ( phase == "moved" ) then print( "Scroll view was moved" ) elseif ( phase == "ended" ) then print( "Scroll view was released" ) end -- In the event a scroll limit is reached... if ( event.limitReached ) then if ( event.direction == "up" ) then print( "Reached bottom limit" ) elseif ( event.direction == "down" ) then print( "Reached top limit" ) elseif ( event.direction == "left" ) then print( "Reached right limit" ) elseif ( event.direction == "right" ) then print( "Reached left limit" ) end end return true end -- Create the widget local scrollView = widget.newScrollView( { top = 100, left = 10, width = 300, height = 400, scrollWidth = 600, scrollHeight = 800, listener = scrollListener } ) -- Create a image and insert it into the scroll view local background = display.newImageRect( "assets/scrollimage.png", 768, 1024 ) scrollView:insert( background )
local widget = require( "widget" ) -- ScrollView listener local function scrollListener( event ) local phase = event.phase if ( phase == "began" ) then print( "Scroll view was touched" ) elseif ( phase == "moved" ) then print( "Scroll view was moved" ) elseif ( phase == "ended" ) then print( "Scroll view was released" ) end -- In the event a scroll limit is reached... if ( event.limitReached ) then if ( event.direction == "up" ) then print( "Reached bottom limit" ) elseif ( event.direction == "down" ) then print( "Reached top limit" ) elseif ( event.direction == "left" ) then print( "Reached right limit" ) elseif ( event.direction == "right" ) then print( "Reached left limit" ) end end return true end -- Create image sheet for custom scroll bar local scrollBarOpt = { width = 20, height = 20, numFrames = 3, sheetContentWidth = 20, sheetContentHeight = 60 } local scrollBarSheet = graphics.newImageSheet( "scrollBar.png", scrollBarOpt ) -- Create the widget local scrollView = widget.newScrollView( { top = 100, left = 10, width = 300, height = 400, scrollWidth = 600, scrollHeight = 800, listener = scrollListener, scrollBarOptions = { sheet = scrollBarSheet, topFrame = 1, middleFrame = 2, bottomFrame = 3 } } ) -- Create a image and insert it into the scroll view local background = display.newImageRect( "assets/scrollimage.png", 768, 1024 ) scrollView:insert( background )