在文本阅读器应用上,翻页时可以使用不同的效果展示页面变更,通常有以下翻页效果:
本文主要对上述翻页效果的实现进行讲解,旨在帮助开发者了解常见翻页动效开发的流程及实现细节。
// entry/src/main/ets/view/UpDownFlipPage.ets
@Link currentPageNum: number;
private data: BasicDataSource = new BasicDataSource([]);
// ...
aboutToAppear(): void {
for (let i = Constants.PAGE_FLIP_PAGE_START; i <= Constants.PAGE_FLIP_PAGE_END; i++) {
this.data.pushItem(Constants.PAGE_FLIP_RESOURCE + i.toString());
}
// ...
} // entry/src/main/ets/view/UpDownFlipPage.ets
List({ initialIndex: this.currentPageNum - 1, scroller: this.scroller }) {
LazyForEach(this.data, (item: string) => {
ListItem() {
Text($r(item))
// ...
}
}, (item: string, index: number) => index + JSON.stringify(item))
}
// ...
.onScrollIndex((firstIndex: number) => {
this.currentPageNum = firstIndex + 1;
}) 覆盖翻页效果模拟卡片切换,新页面(上一页)从屏幕的左侧水平滑入,完全覆盖当前页面。当前页支持从屏幕另一侧滑出,滑出时显示下层新页面。在整个过程中页面没有弯曲或折叠效果,页面作为一个整体平面进行移动。效果如下:

使用Stack堆叠容器,存放1、2、3三个页面,借助图形变换的translate平移属性,将上层页面向左平移屏幕宽度移至窗口左侧。使用PanGesture滑动手势事件判断手势滑动方向及平移距离,依据滑动方向及平移距离,执行1页面向右平移滑入屏幕,或2页面向左平移滑出屏幕。滑动手势结束后通过显式动画 (animateTo)完成页面平移至窗口边缘,并重新渲染1、2、3页面。

// entry/src/main/ets/view/CoverFlipPage.ets
@Component
export struct CoverFlipPage {
// ...
@State offsetX: number = 0;
@State screenW: number = 0;
// ...
build() {
Stack() {
// Next page.
ReaderPage({ content: this.rightPageContent })
// Current page.
ReaderPage({ content: this.midPageContent })
.translate({ x: this.offsetX >= Constants.PAGE_FLIP_ZERO ? Constants.PAGE_FLIP_ZERO : this.offsetX })
// ...
// Previous page, shift the window width to the left.
ReaderPage({ content: this.leftPageContent })
.translate({ x: -this.screenW + this.offsetX })
// ...
}
// ...
}
// ...
} // entry/src/main/ets/view/CoverFlipPage.ets
.gesture(
PanGesture(this.panOption)
.onActionUpdate((event?: GestureEvent) => {
// ...
this.offsetX = event.offsetX;
// ...
})
.onActionEnd(() => {
// ...
this.pageAnimateTo(false);
// ...
})
) // entry/src/main/ets/view/CoverFlipPage.ets
private pageAnimateTo(isClick: boolean, isLeft?: boolean) {
this.getUIContext().animateTo({
duration: Constants.PAGE_FLIP_TO_AST_DURATION,
curve: Curve.EaseOut,
onFinish: () => {
// ...
this.offsetX = Constants.PAGE_FLIP_ZERO;
this.simulatePageContent();
// ...
}
}, () => {
// ...
this.offsetX = this.screenW;
// ...
this.offsetX = -this.screenW;
// ...
});
} 仿真翻页效果模拟真实纸质书的翻页体验。用户拖动页面的角落(右上角或右下角),被拖动的页面会随着手指的移动而卷曲、折叠。在翻动过程中,可以看到当前页的背面(为当前页的翻转显示效果)以及被翻页覆盖的下一页内容逐渐显露出来。翻页轨迹遵循贝塞尔曲线,并伴有阴影效果增强立体感。翻页效果如下:

仿真翻页基于覆盖翻页的页面布局,使用系统接口对当前页或上一页进行截图保存为pixelMap,传递至ArkGraphics 2D的@ohos.graphics.drawing (绘制模块)的相关绘制接口实现了当前页、背页、阴影等区域的绘制。页面绘制通过滑动手势事件PanGesture触发,获取手指在屏幕上的位置,通过该位置信息计算仿真翻页曲线所依赖的相关点位。手势结束时使用定时器模拟滑动触摸的点位并触发页面绘制,判断结束条件终止绘制。
仿真翻页绘制实现关键点

仿真翻页页面布局同覆盖翻页大体相同,屏幕区域保留当前页和下一页层叠,上一页向左移出屏幕。不同的是增加NodeContainer组件,在滑动手势事件触发时显示,用于绘制仿真翻页效果,翻页效果结束时隐藏。
// entry/src/main/ets/view/EmulationFlipPage.ets
Stack() {
// Page area is the same as overlay page cover.
ReaderPage({ content: this.rightPageContent })
ReaderPage({ content: this.midPageContent })
.translate({ x: this.offsetX >= Constants.PAGE_FLIP_ZERO ? Constants.PAGE_FLIP_ZERO : this.offsetX })
.id('middlePage')// Mark the component ID and use it as a screenshot.
// ...
ReaderPage({ content: this.leftPageContent })
.translate({ x: -this.screenW + this.offsetX })
.id('leftPage') // Mark the component ID and use it as a screenshot.
// Display when flipping pages, drawing the current or previous page.
NodeContainer(this.myNodeController)
.width(this.getUIContext().px2vp(this.windowWidth))
.height(this.getUIContext().px2vp(this.windowHeight))
.visibility(this.isNodeShow ? Visibility.Visible : Visibility.None)
} // entry/src/main/ets/view/EmulationFlipPage.ets
build() {
// ...
Stack() {
// ...
}
.gesture(
PanGesture({ fingers: 1 })
.onActionUpdate((event: GestureEvent) => {
// ...
if (this.panPositionX === 0) {
this.initPanPositionX(tmpFingerInfo);
return;
}
// ...
})
// ...
)
// ...
}
// ...
private initPanPositionX(tmpFingerInfo: FingerInfo): void {
this.panPositionX = tmpFingerInfo.localX;
let panPositionY = this.getUIContext().vp2px(tmpFingerInfo.localY);
// Obtain the position of the first touch point and determine the starting area for sliding.
if (panPositionY < (this.windowHeight / 3)) {
this.drawPosition = DrawPosition.DP_TOP;
} else if (panPositionY >
(this.windowHeight * 2 / 3)) {
this.drawPosition = DrawPosition.DP_BOTTOM;
} else {
this.drawPosition = DrawPosition.DP_MIDDLE;
}
} // entry/src/main/ets/view/EmulationFlipPage.ets
build() {
// ...
Stack() {
// ...
}
.gesture(
PanGesture({ fingers: 1 })
.onActionUpdate((event: GestureEvent) => {
// ...
// Perform a check before starting to draw.
if (!this.isDrawing) {
// ...
this.firstDrawingInit(tmpFingerInfo);
}
// ...
})
// ...
)
// ...
}
// ...
private firstDrawingInit(tmpFingerInfo: FingerInfo): void {
// The initial sliding direction of the page is used to determine whether to continue or cancel flipping forward or backward.
if (this.panPositionX < tmpFingerInfo.localX) {
// When flipping forward, take a screenshot of the previous page, and the flipping type is middle flipping.
this.pageMoveForward = MoveForward.MF_FORWARD;
this.snapPageId = 'leftPage';
this.drawPosition = DrawPosition.DP_MIDDLE
} else {
// When flipping back, take a screenshot of the current page and hide it.
this.pageMoveForward = MoveForward.MF_BACKWARD;
this.snapPageId = 'middlePage';
this.isMiddlePageHide = true;
}
// Take a screenshot after confirming the sliding direction of the page.
if (this.pagePixelMap) {
this.pagePixelMap.release();
}
try {
this.pagePixelMap = this.getUIContext().getComponentSnapshot().getSync(this.snapPageId);
} catch (error) {
hilog.error(0x0000, 'EmulationFlip',
`getComponentSnapshot().getSync failed. Cause: ${JSON.stringify(error)}`)
}
this.isDrawing = true;
this.isNodeShow = true;
} // entry/src/main/ets/view/EmulationFlipPage.ets
build() {
// ...
Stack() {
// ...
}
.gesture(
PanGesture({ fingers: 1 })
.onActionUpdate((event: GestureEvent) => {
// ...
// Execute drawing.
this.drawing(tmpFingerInfo);
})
// ...
)
// ...
}
// ...
private drawing(tmpFingerInfo: FingerInfo): void {
// Determine the latest sliding direction of the gesture, and after releasing the gesture,
// combine it with the sliding direction of the page to determine whether to flip or cancel.
if (this.panPositionX < tmpFingerInfo.localX) {
this.gestureMoveForward = MoveForward.MF_FORWARD;
this.panPositionX = tmpFingerInfo.localX;
} else {
this.gestureMoveForward = MoveForward.MF_BACKWARD;
this.panPositionX = tmpFingerInfo.localX;
}
AppStorage.setOrCreate('drawState', DrawState.DS_MOVING);
// Convert to px units.
this.positionX = this.getUIContext().vp2px(tmpFingerInfo.localX);
this.positionY = this.getUIContext().vp2px(tmpFingerInfo.localY);
AppStorage.setOrCreate('positionX', this.positionX);
AppStorage.setOrCreate('positionY', this.positionY);
// Execute drawing.
this.newRectNode();
} // entry/src/main/ets/view/EmulationFlipPage.ets
newRectNode() {
// Creates a RectRenderNode object.
const rectNode = new RectRenderNode();
// ...
this.myNodeController.clearNodes();
this.myNodeController.addNode(rectNode);
} // entry/src/main/ets/viewmodel/PageNodeController.ets
export class RectRenderNode extends RenderNode {
// ...
draw(context: DrawContext): void {
const canvas = context.canvas;
// Initialize data.
init();
// ...
}
}
// ...
/**
* Initialize data.
*/
function init(): void {
// ...
// Obtain touch points.
let x: number = AppStorage.get('positionX') as number;
let y: number = AppStorage.get('positionY') as number;
let viewWidth: number = AppStorage.get('windowWidth') as number;
let viewHeight: number = AppStorage.get('windowHeight') as number;
pointA = new MyPoint(x, y);
// ...
let touchPoint = new MyPoint(x, y);
let drawState: number = AppStorage.get('drawState') as number;
let drawStartPosition: number = AppStorage.get('drawPosition') as number;
// Determine the area where sliding begins.
if (DrawPosition.DP_TOP === drawStartPosition) {
// The touch point is at the top.
pointF = new MyPoint(viewWidth, 0);
if (drawState !== DrawState.DS_RELEASE) {
calcPointAByTouchPoint(touchPoint);
}
} else if (DrawPosition.DP_BOTTOM === drawStartPosition) {
// The touch point is below.
pointF = new MyPoint(viewWidth, viewHeight);
if (drawState !== DrawState.DS_RELEASE) {
calcPointAByTouchPoint(touchPoint);
}
} else {
// The touch point is in the middle.
pointA.y = viewHeight - 1;
pointF.x = viewWidth;
pointF.y = viewHeight;
}
// Saves the y-coordinate of point A.
AppStorage.setOrCreate<number>('flipPositionY', pointA.y);
// Calculate all path points.
calcPointsXY();
}
/**
* Calculate the y-coordinate of point A based on the touch point.
* @param touchPoint Touch point.
*/
function calcPointAByTouchPoint(touchPoint: MyPoint): void {
let viewWidth: number = AppStorage.get('windowWidth') as number;
let viewHeight: number = AppStorage.get('windowHeight') as number;
let r = Constants.SIXTY_PERCENT * viewWidth;
pointA.x = touchPoint.x;
// Reset the y value and restrict the region where the y value is located.
if (pointF.y === viewHeight) {
let tmpY = viewHeight - Math.abs(Math.sqrt(Math.pow(r, 2) - Math.pow(touchPoint.x - (viewWidth - r), 2)))
pointA.y = touchPoint.y >= tmpY ? touchPoint.y : tmpY;
} else {
let tmpY = Math.abs(Math.sqrt(Math.pow(r, 2) - Math.pow(touchPoint.x - (viewWidth - r), 2)))
pointA.y = touchPoint.y >= tmpY ? tmpY : touchPoint.y;
}
}
/**
* Calculate the coordinates of each path point.
*/
function calcPointsXY(): void {
pointG.x = (pointA.x + pointF.x) / 2;
pointG.y = (pointA.y + pointF.y) / 2;
pointE.x = pointG.x - (pointF.y - pointG.y) * (pointF.y - pointG.y) / (pointF.x - pointG.x);
pointE.y = pointF.y;
pointH.x = pointF.x;
pointH.y = pointG.y - (pointF.x - pointG.x) * (pointF.x - pointG.x) / (pointF.y - pointG.y);
pointC.x = pointE.x - (pointF.x - pointE.x) / 2;
pointC.y = pointF.y;
pointJ.x = pointF.x;
pointJ.y = pointH.y - (pointF.y - pointH.y) / 2;
pointB = getIntersectionPoint(pointA, pointE, pointC, pointJ);
pointK = getIntersectionPoint(pointA, pointH, pointC, pointJ);
pointD.x = (pointC.x + 2 * pointE.x + pointB.x) / 4;
pointD.y = (2 * pointE.y + pointC.y + pointB.y) / 4;
pointI.x = (pointJ.x + 2 * pointH.x + pointK.x) / 4;
pointI.y = (2 * pointH.y + pointJ.y + pointK.y) / 4;
//Calculate the distance from point d to line ae and use it to draw shadows.
let lA: number = pointA.y - pointE.y;
let lB: number = pointE.x - pointA.x;
let lC: number = pointA.x * pointE.y - pointE.x * pointA.y;
lPathAShadowDis = Math.abs((lA * pointD.x + lB * pointD.y + lC) / Math.hypot(lA, lB));
// Calculate the distance from point i to ah and use it to draw shadows.
let rA: number = pointA.y - pointH.y;
let rB: number = pointH.x - pointA.x;
let rC: number = pointA.x * pointH.y - pointH.x * pointA.y;
rPathAShadowDis = Math.abs((rA * pointI.x + rB * pointI.y + rC) / Math.hypot(rA, rB));
// Check if the drawing area is still in the window. If it is not in the window, the drawing ends.
if (!checkDrawingAreaInWindow()) {
AppStorage.setOrCreate('isFinished', true);
}
} // entry/src/main/ets/viewmodel/PageNodeController.ets
export class RectRenderNode extends RenderNode {
// ...
draw(context: DrawContext): void {
const canvas = context.canvas;
// ...
// Draw the shadow shown on the next page.
drawPathBShadow(canvas);
// ...
}
}
// ...
/**
* Draw the shadow shown on the next page.
*
* @param canvas canvas
*/
function drawPathBShadow(canvas: drawing.Canvas) {
canvas.save()
// Gradient color array.
let deepColor: number = 0xff111111;
let lightColor: number = 0x00111111;
let gradientColors: number[] = [deepColor, lightColor];
let viewWidth: number = AppStorage.get('windowWidth') as number;
let viewHeight: number = AppStorage.get('windowHeight') as number;
// The distance from A to F.
let aToF = Math.hypot((pointA.x - pointF.x), (pointA.y - pointF.y));
// The distance from A to F.
let viewDiagonalLength = Math.hypot(viewWidth, viewHeight);
let left: number = 0;
let right: number = 0;
let top: number = pointC.y;
let bottom: number = viewDiagonalLength + pointC.y;
if (pointF.x === viewWidth && pointF.y === 0) {
// The F point is located in the upper right corner.
left = pointC.x;
right = pointC.x + aToF / 4;
} else {
left = pointC.x - aToF / 4;
right = pointC.x;
}
// Calculate the rotation angle between two points (calculated in radians and converted into angles).
let deltaX: number = pointH.y - pointF.y;
let deltaY: number = pointE.x - pointF.x;
let radians: number = Math.atan2(deltaY, deltaX);
// Convert radians to angles.
let rotateDegrees: number = radians * 180 / Math.PI;
let startPt: common2D.Point = { x: pointF.y === 0 ? right : left, y: top };
let endPt: common2D.Point = { x: pointF.y === 0 ? left : right, y: top };
let shaderEffect = drawing.ShaderEffect.createLinearGradient(endPt, startPt, gradientColors, drawing.TileMode.MIRROR);
// Perform rotation.
canvas.rotate(rotateDegrees, pointC.x, pointC.y);
// Draw shadows.
let rect: common2D.Rect = {
left: left,
top: top,
right: right,
bottom: bottom
};
drawShadow(canvas, shaderEffect, rect);
}
// ...
/**
* Draw shaded areas.
*
* @param canvas canvas
* @param shaderEffect shader effect
* @param rect rect
*/
function drawShadow(canvas: drawing.Canvas, shaderEffect: drawing.ShaderEffect, rect: common2D.Rect) {
let tmpBrush = new drawing.Brush();
tmpBrush.setShaderEffect(shaderEffect);
canvas.attachBrush(tmpBrush);
canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom);
canvas.detachBrush();
canvas.restore();
} // entry/src/main/ets/viewmodel/PageNodeController.ets
export class RectRenderNode extends RenderNode {
// ...
draw(context: DrawContext): void {
const canvas = context.canvas;
// ...
// Draw the back area for flipping pages.
drawPathC(canvas);
// ...
}
}
// ...
/**
* Draw the back area for flipping pages.
*
* @param canvas canvas
*/
function drawPathC(canvas: drawing.Canvas): void {
if (canIUse('SystemCapability.Graphics.Drawing')) {
canvas.attachBrush(pathABrush);
pathC.reset();
pathC.moveTo(pointI.x, pointI.y);
pathC.lineTo(pointD.x, pointD.y);
pathC.lineTo(pointB.x, pointB.y);
pathC.lineTo(pointA.x, pointA.y);
pathC.lineTo(pointK.x, pointK.y);
pathC.close();
canvas.drawPath(pathC);
// Draw the content on the back.
canvas.save();
canvas.clipPath(pathC);
// Set the inversion and rotation matrices.
let eh = Math.hypot(pointF.x - pointE.x, pointH.y - pointF.y);
let sin0 = (pointF.x - pointE.x) / eh;
let cos0 = (pointH.y - pointF.y) / eh;
let value: Array<number> = [0, 0, 0, 0, 0, 0, 0, 0, 1.0];
value[0] = -(1 - 2 * sin0 * sin0);
value[1] = 2 * sin0 * cos0;
value[3] = 2 * sin0 * cos0;
value[4] = 1 - 2 * sin0 * sin0;
let matrix = new drawing.Matrix();
matrix.reset();
matrix.setMatrix(value);
matrix.preTranslate(-pointE.x, -pointE.y);
matrix.postTranslate(pointE.x, pointE.y);
canvas.concatMatrix(matrix);
// Draw the current page on the back.
let pagePixelMap: image.PixelMap = AppStorage.get('pagePixelMap') as image.PixelMap;
let viewWidth: number = AppStorage.get('windowWidth') as number;
let viewHeight: number = AppStorage.get('windowHeight') as number;
let verts: Array<number> = [0, 0, viewWidth, 0, 0, viewHeight, viewWidth, viewHeight];
canvas.drawPixelMapMesh(pagePixelMap, 1, 1, verts, 0, null, 0);
canvas.restore();
// Change the color on the back.
canvas.detachBrush();
canvas.attachBrush(pathCBrush);
canvas.drawPath(pathC);
canvas.detachBrush();
// Draw shadows in the back area.
canvas.save();
canvas.clipPath(pathC);
drawPathCShadow(canvas);
canvas.restore();
}
} // entry/src/main/ets/viewmodel/PageNodeController.ets
export class RectRenderNode extends RenderNode {
// ...
draw(context: DrawContext): void {
const canvas = context.canvas;
// ...
// Retrieve the cropped area of the current page.
getPathA();
// Draw the current page area.
drawPathAContent(canvas);
}
}
/**
* Retrieve the cropped area of the current page.
*/
function getPathA(): void {
if (canIUse('SystemCapability.Graphics.Drawing')) {
let viewWidth: number = AppStorage.get('windowWidth') as number;
let viewHeight: number = AppStorage.get('windowHeight') as number;
// Point F is located in the upper right corner, calculate pathA.
if (pointF.x === viewWidth && pointF.y === 0) {
pathA.reset();
pathA.lineTo(pointC.x, pointC.y);
pathA.quadTo(pointE.x, pointE.y, pointB.x, pointB.y);
pathA.lineTo(pointA.x, pointA.y);
pathA.lineTo(pointK.x, pointK.y);
pathA.quadTo(pointH.x, pointH.y, pointJ.x, pointJ.y);
pathA.lineTo(viewWidth, viewHeight);
pathA.lineTo(0, viewHeight);
pathA.close();
}
// Point F is located in the bottom right corner, calculate pathA.
if (pointF.x === viewWidth && pointF.y === viewHeight) {
pathA.reset();
pathA.lineTo(0, viewHeight);
pathA.lineTo(pointC.x, pointC.y);
pathA.quadTo(pointE.x, pointE.y, pointB.x, pointB.y);
pathA.lineTo(pointA.x, pointA.y);
pathA.lineTo(pointK.x, pointK.y);
pathA.quadTo(pointH.x, pointH.y, pointJ.x, pointJ.y);
pathA.lineTo(viewWidth, 0);
pathA.close();
}
}
}
// ...
/**
* Draw the current page area.
*
* @param canvas canvas
*/
function drawPathAContent(canvas: drawing.Canvas): void {
if (canIUse('SystemCapability.Graphics.Drawing')) {
canvas.attachBrush(pathABrush);
canvas.save();
canvas.clipPath(pathA);
// Obtain a screenshot pixelMap for displaying the current page.
let pagePixelMap: image.PixelMap = AppStorage.get('pagePixelMap') as image.PixelMap;
let viewWidth: number = AppStorage.get('windowWidth') as number;
let viewHeight: number = AppStorage.get('windowHeight') as number;
let verts: Array<number> = [0, 0, viewWidth, 0, 0, viewHeight, viewWidth, viewHeight];
// Execute drawing.
canvas.drawPixelMapMesh(pagePixelMap, 1, 1, verts, 0, null, 0);
canvas.restore();
// Draw the shadow of the current page.
if (AppStorage.get('drawPosition') === DrawPosition.DP_MIDDLE) {
drawPathAHorizontalShadow(canvas);
} else {
drawPathALeftShadow(canvas);
drawPathARightShadow(canvas);
}
}
} // entry/src/main/ets/view/EmulationFlipPage.ets
build() {
// ...
Stack() {
// ...
}
.gesture(
PanGesture({ fingers: 1 })
// ...
.onActionEnd(() => {
// ...
// Perform automatic sliding.
this.autoFlipPage();
this.isDrawing = false;
})
)
// ...
}
// ...
/**
* Perform automatic drawing.
*/
private autoFlipPage(): void {
AppStorage.set('drawState', DrawState.DS_RELEASE);
// Get the vertical axis of the drawn footer.
AppStorage.setOrCreate('positionY', (AppStorage.get('flipPositionY') as number));
let num: number = Constants.DISTANCE_FRACTION;
if (this.gestureMoveForward === MoveForward.MF_FORWARD) {
// Page forward to calculate diff.
let xDiff = (this.windowWidth - this.positionX) / num;
let yDiff = 0;
if (this.drawPosition === DrawPosition.DP_BOTTOM) {
yDiff = (this.windowHeight - this.positionY) / num;
} else {
yDiff = (0 - this.positionY) / num;
}
this.setTimer(xDiff, yDiff, () => {
this.newRectNode();
});
} else {
// Next Page.
this.setTimer(Constants.FLIP_X_DIFF, 0, () => {
this.newRectNode();
});
}
} // entry/src/main/ets/view/EmulationFlipPage.ets
private setTimer(xDiff: number, yDiff: number, drawNode: () => void) {
// Automatically flip forward.
if (this.gestureMoveForward === MoveForward.MF_FORWARD) {
this.timeID = setInterval((xDiff: number, yDiff: number, drawNode: () => void) => {
let x = AppStorage.get('positionX') as number + xDiff;
let y = AppStorage.get('positionY') as number + yDiff;
// Page forward termination condition.
if (x >= (AppStorage.get('windowWidth') as number) - 1 || y >= (AppStorage.get('windowHeight') as number) ||
y <= 0) {
this.finishLastGesture();
} else {
AppStorage.setOrCreate('positionX', x);
AppStorage.setOrCreate('positionY', y);
drawNode();
}
}, Constants.TIMER_DURATION, xDiff, yDiff, drawNode);
} else {
// Automatically flip backwards.
AppStorage.setOrCreate('isFinished', false);
this.timeID = setInterval((xDiff: number, _: number, drawNode: () => void) => {
let x = AppStorage.get('positionX') as number + xDiff;
let y = AppStorage.get('positionY') as number;
// Obtain the termination condition for determining when flipping back to draw.
let isFinished: boolean = AppStorage.get('isFinished') as boolean;
if (isFinished) {
// End automatic drawing.
this.finishLastGesture();
} else {
AppStorage.setOrCreate('positionX', x);
AppStorage.setOrCreate('positionY', y);
drawNode();
}
}, Constants.TIMER_DURATION, xDiff, yDiff, drawNode);
}
} // entry/src/main/ets/view/EmulationFlipPage.ets
private setTimer(xDiff: number, yDiff: number, drawNode: () => void) {
// Automatically flip forward.
if (this.gestureMoveForward === MoveForward.MF_FORWARD) {
this.timeID = setInterval((xDiff: number, yDiff: number, drawNode: () => void) => {
// ...
// Page forward termination condition.
if (x >= (AppStorage.get('windowWidth') as number) - 1 || y >= (AppStorage.get('windowHeight') as number) ||
y <= 0) {
this.finishLastGesture();
} else {
// ...
}
}, Constants.TIMER_DURATION, xDiff, yDiff, drawNode);
} else {
// Automatically flip backwards.
AppStorage.setOrCreate('isFinished', false);
this.timeID = setInterval((xDiff: number, _: number, drawNode: () => void) => {
// ...
// Obtain the termination condition for determining when flipping back to draw.
let isFinished: boolean = AppStorage.get('isFinished') as boolean;
if (isFinished) {
// End automatic drawing.
this.finishLastGesture();
} else {
// ...
}
}, Constants.TIMER_DURATION, xDiff, yDiff, drawNode);
}
} // entry/src/main/ets/view/EmulationFlipPage.ets
private finishLastGesture() {
clearInterval(this.timeID);
this.timeID = -1;
// Previous page.
if (this.pageMoveForward === MoveForward.MF_FORWARD && this.gestureMoveForward === MoveForward.MF_FORWARD) {
this.currentPageNum--;
this.simulatePageContent();
}
// Next page.
if (this.pageMoveForward === MoveForward.MF_BACKWARD && this.gestureMoveForward === MoveForward.MF_BACKWARD) {
this.currentPageNum++;
this.simulatePageContent();
}
AppStorage.setOrCreate('positionX', -1);
AppStorage.setOrCreate('positionY', -1);
AppStorage.setOrCreate('drawPosition', DrawPosition.DP_NONE);
AppStorage.setOrCreate('drawState', DrawState.DS_NONE);
this.isMiddlePageHide = false;
this.isNodeShow = false;
this.gestureMoveForward = MoveForward.MF_NONE;
this.panPositionX = 0;
this.drawPosition = DrawPosition.DP_NONE;
this.isDrawing = false;
this.pagePixelMap?.release();
}