Last active
September 23, 2016 03:57
-
-
Save wiiiim/f60e06cb9988f57665c0 to your computer and use it in GitHub Desktop.
Springboard for Corona SDK, based on widget.scrollView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local springboard = require("springboard") | |
local group = display.newGroup() -- root display group | |
local pageTotal = 3 | |
local board -- springboard instance | |
local pageDots = {} -- dots at the top | |
local function animatePageDot() -- called each time the springboard changes page | |
local currentPage = board.page | |
-- reset all pagedots | |
for i=1, pageTotal do | |
local dot=pageDots[i] | |
dot.xScale=1; dot.yScale=1 | |
end | |
-- enlarge dot for current page | |
transition.to(pageDots[currentPage], {time=30, xScale=1.4, yScale=1.4}) | |
end | |
-- creat page dots | |
local dotGroup=display.newGroup(); dotGroup.anchorX=.5; dotGroup.anchorChildren=true | |
dotGroup.x = 0.5*display.contentWidth; dotGroup.y = 30 | |
for i=1, pageTotal do | |
local dot = display.newCircle((i-1)*50, 0, 10); dotGroup:insert(dot) -- in a real app you would use a smooth dot image, circles of this size look pixellated | |
pageDots[i]=dot | |
end | |
-- create springboard | |
local boardGroup = display.newGroup(); group:insert(boardGroup) | |
board = springboard.new(boardGroup, 300, 400, pageTotal, animatePageDot, {backgroundColor={.8,.8,1}, time=200}) | |
boardGroup.x = 10 | |
boardGroup.y = 50 | |
-- fill the springboard with content | |
for i=1, pageTotal do | |
local rect = display.newRect((i-1)*300 + 10, 50, 280, 200); rect.anchorX = 0; rect.anchorY = 0; rect:setFillColor(.2*i, .1*i, .3*i) | |
board.view:insert(rect) -- insert into springboard | |
end | |
-- flip to initial page (default on page 1) | |
board:goToPage(2, 0) -- time = 0 for instant page switch (no transition) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module(..., package.seeall) | |
local widget = require( "widget" ) | |
local math_abs = math.abs | |
local math_floor = math.floor | |
local system_getTimer = system.getTimer | |
local springboard = { } | |
local springboard_mt = { __index = springboard } | |
function springboard.new(group, pageWidth, pageHeight, pages, onScrollComplete, params) | |
local touchTime, scrollView | |
local obj = {pageWidth=pageWidth, pageHeight=pageHeight, pages=pages, page=1, onScrollComplete=onScrollComplete} | |
obj.backgroundColor=(params and params.backgroundColor) or nil | |
obj.time=(params and params.time) or nil | |
local function scrollListener(event) | |
local phase = event.phase | |
if phase == "began" then | |
touchTime = event.time | |
elseif phase == "moved" and not touchTime then -- no began phase when focus is explicetly transferred to springboard with takeFocus | |
touchTime = event.time | |
elseif phase == "ended" or phase == "off" or phase == "cancelled" then | |
touchTime = touchTime or system_getTimer() | |
local motionTime = system_getTimer() - touchTime | |
local dist = event.xStart - event.x; local swipeDist = math_abs(dist); local nextPage | |
-- calculate next page index (negative or 0) | |
if (motionTime <= 300 and swipeDist >= 30) then | |
-- fast page flip | |
nextPage = obj.page + dist/swipeDist | |
else | |
-- slow controlled drag | |
local x, y = scrollView:getContentPosition() | |
nextPage = math_floor(-x/pageWidth) + ((-x%pageWidth > 0.5*pageWidth and 1) or 0) + 1 | |
end | |
-- move to page | |
if nextPage >= 1 and nextPage <= pages then | |
-- snap to next page | |
scrollView:scrollToPosition({x=-(nextPage-1)*pageWidth, onComplete=onScrollComplete, time=obj.time}) | |
obj.page = nextPage | |
else | |
-- out of bounds | |
scrollView:scrollToPosition({x=-(obj.page-1)*pageWidth, onComplete=onScrollComplete, time=obj.time}) | |
end | |
end | |
return true | |
end | |
scrollView = widget.newScrollView | |
{ | |
top = 0, | |
left = 0, | |
width = pageWidth, | |
height = pageHeight, | |
scrollWidth = pageWidth * pages, | |
scrollHeight = pageHeight, | |
listener = scrollListener, | |
backgroundColor = obj.backgroundColor, | |
hideBackground = not obj.backgroundColor, | |
verticalScrollDisabled = true, | |
hideScrollBar = true, | |
} | |
group:insert(scrollView) | |
obj.view = scrollView | |
return setmetatable(obj, springboard_mt) | |
end | |
function springboard:goToPage(page, time) | |
local options = {x = -(page-1)*self.pageWidth, onComplete = self.onScrollComplete, time=(time or self.time)} | |
self.view:scrollToPosition(options) | |
self.page = page | |
end | |
return springboard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment