PageComponent

页面组件是基于滚动组件的,但是是为多页面展现而设计的,而不是像滚动组件一样展示连续的内容。它支持不同尺寸的内容页,并能够基于图层位置和滚动速度来对齐图层。

The PageComponent is based on the ScrollComponent, but designed for displaying paginated instead of continuous content. It supports content layers of different sizes, and can snap to layers based on location and scroll velocity.

  1. # 创建一个新的 PageComponent 并只允许横向滚动
  2. page = new PageComponent
  3. width: Screen.width
  4. height: Screen.height
  5. scrollVertical: false
  6. # 创建第一个页面
  7. pageOne = new Layer
  8. width: page.width
  9. height: page.height
  10. parent: page.content
  11. backgroundColor: "#28affa"

创建第二个页面,点这里了解addPage方法。

Let’s add a second page now. Read more about the addPage method.

  1. # 定义第二个页面
  2. pageTwo = new Layer
  3. width: page.width
  4. height: page.height
  5. backgroundColor: "#90D7FF"
  6. # 将第二个页面添加至右侧
  7. page.addPage(pageTwo, "right")

另外一种添加页面的方式是使用 for 循环。

Another way to go about adding content is by using a for-loop.

  1. # 创建一个新的页面组件并只允许横向滚动
  2. page = new PageComponent
  3. width: Screen.width
  4. height: Screen.height
  5. scrollVertical: false
  6. backgroundColor: "#fff"
  7. # 创建五个新图层并添加至 page.content
  8. for number in [0...5]
  9. pageContent = new Layer
  10. width: page.width
  11. height: page.height
  12. x: page.width * number
  13. backgroundColor: Utils.randomColor(0.5)
  14. parent: page.content
  15. # 让每个页面显示编号
  16. pageContent.html = pageContent.html = number + 1
  17. # 让当前页面中的数字居中显示
  18. pageContent.style =
  19. "font-size" : "100px",
  20. "font-weight" : "100",
  21. "text-align" : "center",
  22. "line-height" : "#{page.height}px"

page.originX <number>

定义当手指(鼠标)松开页面自由滑行之后,页面在水平方向上是如何停靠在页面组件容器中的。他的值是 0 到 1 之间的一个数字,0 表示停靠在容器左边缘,1 表示停靠在容器右边缘,默认是 0.5 停靠在中间。

Defines how pages will be horizontally snapped to. The origin is defined as a number between 0 and 1, where 0 is the left-edge and 1 the right-edge. The default value is 0.5, the center.

  1. page = new PageComponent
  2. page.originX = 0

page.originY <number>

定义当手指(鼠标)松开页面自由滑行之后,页面在垂直方向上是如何停靠在页面组件容器中的。他的值是 0 到 1 之间的一个数字,0 表示停靠在容器上边缘,1 表示停靠在容器下边缘,默认是 0.5 停靠在中间。

Defines how pages will be vertically snapped to. The origin is defined as a number between 0 and 1, where 0 is the top-edge and 1 the bottom-edge. The default value is 0.5, the center.

  1. page = new PageComponent
  2. page.originY = 0

page.velocityThreshold <number>

速度临界值会影响最终停靠点,当你拖动之后松开手指(鼠标),PageComponent 会依据此时的滑行速度和滑行距离来决定滑到下一个页面还是后退至停在当前页。

The velocityThreshold influences the role velocity plays in snapping to a different page. Besides the scrolling distance, the PageComponent also takes your scrolling speed (velocity) into account.

  1. page = new PageComponent
  2. page.velocityThreshold = 0.2

为了更好地理解速度临界值,你可以在每次滑行结束时输出此时的速度。如果你想更大程度上让距离来决定页面的切换,就调大速度临界值。

To better understand the effects of adjusting the velocityThreshold, you can print out your velocity after scrolling. If you want switching between pages to be based mostly on distance, increase the velocityThreshold.

  1. # Increase the velocityThreshold
  2. page.velocityThreshold = 5
  3. # After switching between pages, print the velocity
  4. page.on Events.ScrollEnd, ->
  5. print Math.abs(page.velocity.x)

page.animationOptions <object>

给页面组件设置动画选项,该选项用于松开手指(鼠标)时页面自动贴合的动画。

Set the animation options for the PageComponent. This defines the animation that occurs on ScrollEnd, when snapping to a page.

  1. page = new PageComponent
  2. page.animationOptions =
  3. curve: Bezier.ease
  4. time: 0.25

page.currentPage <Layer object>

获得当前页面图层对象(只读)。

Get the current page layer. (Read-only)

  1. page = new PageComponent
  2. # Get the current page layer
  3. print page.currentPage

注意:必须要保证page.content内已添加页面图层。你也可以监听change:currentPage事件来获取到最新的页面图层。

Note that you have to have pages within the page.content layer. You can also listen to the “change:currentPage” event to get the new currentPage, after it has been changed.

  1. page = new PageComponent
  2. # 当页面发生变动时,输出该页面图层对象
  3. page.on "change:currentPage", ->
  4. print page.currentPage

page.closestPage <Layer object>

获取最近的页面图层(只读)。

Get the closest page layer. (Read-only)

  1. page = new PageComponent
  2. # Get the current page layer
  3. print page.closestPage

注意:必须要保证 page.content 内已添加页面图层。你也可以监听 Scroll 事件来获取到最近的页面图层。

Note that you have to have pages within the page.content layer. You can also listen to the Scroll event to get the page closest page while scrolling.

  1. # Create a new PageComponent and only allow horizontal scrolling.
  2. page = new PageComponent
  3. # Print the closest page while scrolling
  4. page.on Events.Scroll, ->
  5. print page.closestPage

page.nextPage(direction, currentPage)

获取下一个页面。该方法需要两个参数:方向和当前页面图层。默认情况下,方向参数为 right 且当前页面图层为第一个页面。

Get the next page. Takes two arguments: direction and the currentPage. By default, the direction is set to “right” and the currentPage will be the first page.

  1. page = new PageComponent
  2. width: Screen.width
  3. height: Screen.height
  4. pageOne = new Layer
  5. width: page.width
  6. height: page.height
  7. parent: page.content
  8. backgroundColor: "#28affa"
  9. pageTwo = new Layer
  10. width: page.width
  11. height: page.height
  12. backgroundColor: "#90D7FF"
  13. page.addPage(pageTwo, "right")
  14. print page.nextPage()

当然,我们也可以设置当前图层为其它图层,比如通过下面的代码我们可以知道第二个页面的左边是那个页面。

We can also set the currentPage to any other page. For instance, we can look which layer is at the left of the second page.

  1. # Get the page to the left of pageTwo
  2. print page.nextPage("left", pageTwo)
  3. # Returns pageOne

page.previousPage <Layer object>

获取前一个页面,实际上它和使用 page.nextPage("left") 来获取到左边的页面是一样的(只读)。

Get the previous page. Effectively the same as getting the page on the left using page.nextPage(“left”). (Read-only)

  1. page = new PageComponent
  2. # Get the previous page
  3. print page.previousPage

page.snapToPage(page, animate, animationOptions)

立即切换到某一个页面。它需要三个参数:一个 page.content 下的页面图层,是否动画(true 或者 false),以及动画选项。默认情况下是使用动画方式切换,并且动画曲线为一个弹性曲线。

Snap to a specific page. Takes three arguments: a page.content layer, animate (true or false) and animation options. By default, animate is set to true and the animationCurve use a spring curve.

  1. page = new PageComponent
  2. width: Screen.width
  3. height: Screen.height
  4. pageOne = new Layer
  5. width: page.width
  6. height: page.height
  7. parent: page.content
  8. backgroundColor: "#28affa"
  9. pageTwo = new Layer
  10. width: page.width
  11. height: page.height
  12. backgroundColor: "#90D7FF"
  13. page.addPage(pageTwo, "right")
  14. # Automatically scroll to pageTwo
  15. page.snapToPage(pageTwo)

继续使用上面的例子,我们可以通过自定义动画选项来自定义滚动动画。

In the example above, we can customize the scrolling animation by defining custom animationOptions as well.

  1. # Define a slower easing curve
  2. page.snapToPage(
  3. pageTwo
  4. true
  5. animationOptions = time: 2
  6. )

page.snapToNextPage(direction, animate, animationOptions)

立即切换到下一个页面。它需要三个参数:方向,是否动画(true 或者 false),以及动画选项。默认情况下方向为 right ,并且以动画形式切换。

Snap to a specific the next page. Takes three arguments: direction, animate (true or false) and animation options. By default, the direction is set to “right” and animate is true.

  1. page = new PageComponent
  2. width: Screen.width
  3. height: Screen.height
  4. pageOne = new Layer
  5. width: page.width
  6. height: page.height
  7. parent: page.content
  8. backgroundColor: "#28affa"
  9. pageTwo = new Layer
  10. width: page.width
  11. height: page.height
  12. backgroundColor: "#90D7FF"
  13. page.addPage(pageTwo, "right")
  14. # Automatically scroll to pageTwo
  15. page.snapToNextPage()

这可以让你切换到任意方向的页面。比如我们可以先不带动画地切换到第二个页面,再让它带动画地切换回来。

This allows you to snap to pages in any direction. For example, we can start on the first page by snapping to it without animating, and then animate back to the first page.

  1. # Start at page two by default
  2. page.snapToPage(pageTwo, false)
  3. # Scroll back to the page at its left, which is pageOne
  4. page.snapToNextPage("left", true)

page.snapToPreviousPage()

切换到上一页,该方法可以帮你切换到上一个查看的页面。

Snaps to the previous page. It keeps track of all previously visited pages, included the direction.

  1. page = new PageComponent
  2. # Snap to the previous page
  3. page.snapToPreviousPage()

page.addPage(direction)

page.content 添加一个新页面,他需要两个参数:一个页面图层和方向( rightbottom)。

Add a new page to the page.content layer of the PageComponent. It takes two arguments: a layer (page) and a direction (“right” or “bottom”).

  1. page = new PageComponent
  2. width: Screen.width
  3. height: Screen.height
  4. # The first page, placed directly within the page.content
  5. pageOne = new Layer
  6. width: page.width
  7. height: page.height
  8. parent: page.content
  9. backgroundColor: "#28affa"
  10. # Second page
  11. pageTwo = new Layer
  12. width: page.width
  13. height: page.height
  14. backgroundColor: "#90D7FF"
  15. # Third page
  16. pageThree = new Layer
  17. width: page.width
  18. height: page.height
  19. backgroundColor: "#CAECFF"
  20. # Add the second and third page to the right
  21. page.addPage(pageTwo, "right")
  22. page.addPage(pageThree, "right")

如果你想在左侧添加一个页面,你可以先在它的右侧添加一个页面,再使用 snapToPage() 让它迅速切换到第二个页面,使其看起来像是以第二个页面为初始页。

If you’re looking to add pages to the left, you can initially add them to the right, and instantly switch to a different page, using snapToPage().

  1. page = new PageComponent
  2. width: Screen.width
  3. height: Screen.height
  4. pageOne = new Layer
  5. width: page.width
  6. height: page.height
  7. parent: page.content
  8. backgroundColor: "#28affa"
  9. pageTwo = new Layer
  10. width: page.width
  11. height: page.height
  12. backgroundColor: "#90D7FF"
  13. # Add a page to the right
  14. page.addPage(pageTwo, "right")
  15. # Start at the second page by default
  16. page.snapToPage(pageTwo, false)

page.horizontalPageIndex(page)

获取水平排列的页面索引值,它的值处于 0 到页面数减 1 之间。

Get the index for a page component with horizontal pages. This is a number between 0 and the number of pages minus 1.

  1. page = new PageComponent
  2. width: Screen.width
  3. height: Screen.height
  4. pageA = new Layer
  5. size: page.size
  6. pageB = new Layer
  7. size: page.size
  8. page.addPage(pageA, "right")
  9. page.addPage(pageB, "right")
  10. print page.horizontalPageIndex(pageA)
  11. # Prints: 0
  12. print page.horizontalPageIndex(pageB)
  13. # Prints: 1

page.verticalPageIndex(page)

获取垂直排列的页面索引值,它的值处于 0 到页面数减 1 之间。

Get the index for a page component with vertical pages. This is a number between 0 and the number of pages minus 1.

  1. page = new PageComponent
  2. width: Screen.width
  3. height: Screen.height
  4. pageA = new Layer
  5. size: page.size
  6. pageB = new Layer
  7. size: page.size
  8. page.addPage(pageA, "bottom")
  9. page.addPage(pageB, "bottom")
  10. print page.verticalPageIndex(pageA)
  11. # Prints: 0
  12. print page.verticalPageIndex(pageB)
  13. # Prints: 1