From 9394e8927e9803fb6da7dba867eb9bde4d5349d9 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 4 Mar 2020 13:58:47 +0800 Subject: [PATCH] feat:update demo --- doric-demo/src/ScrolledSliderDemo.ts | 106 +++++++++++++++++++++------ 1 file changed, 84 insertions(+), 22 deletions(-) diff --git a/doric-demo/src/ScrolledSliderDemo.ts b/doric-demo/src/ScrolledSliderDemo.ts index 65f2345d..68252e82 100644 --- a/doric-demo/src/ScrolledSliderDemo.ts +++ b/doric-demo/src/ScrolledSliderDemo.ts @@ -1,4 +1,4 @@ -import { Group, text, gravity, Color, LayoutSpec, vlayout, hlayout, layoutConfig, scroller, Text, ViewHolder, VMPanel, ViewModel, network, loge, HLayout, stack, image, Gravity, takeNonNull, Scroller, Image } from "doric"; +import { Group, text, gravity, Color, LayoutSpec, vlayout, hlayout, layoutConfig, scroller, Text, ViewHolder, VMPanel, ViewModel, network, loge, HLayout, stack, image, Gravity, takeNonNull, Scroller, Image, View } from "doric"; import { colors } from "./utils"; import MovieData from './movie.json' @@ -67,15 +67,20 @@ interface DoubanModel { interface MovieModel { doubanModel?: DoubanModel selectedIdx: number + anchorPos: number } +const frameWidth = 200 +const frameHeight = 300 +const padding = 20 + class MovieVH extends ViewHolder { title!: Text gallery!: HLayout scrolled!: Scroller movieTitle!: Text movieYear!: Text - + anchor!: View build(root: Group) { vlayout( [ @@ -110,9 +115,20 @@ class MovieVH extends ViewHolder { }), vlayout( [ - text({ - text: "↑", - }), + stack( + [ + this.anchor = image({ + imageBase64: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAANb0lEQVR4Xu2de6wcVR3Hv7+9tyTEQLhzS0zUINg7W6Lwjw9QEF9Ay5vykoeI4ut2ZyExJhqNMaIxJBgjiXRnKSjlTWmhtLQ8W4q8hZqYaATc2SbgH8aE7rkYjRrp3WMutFAovTtzdubMnD3f+++e3+N8fueTvfuaEfCPBEhgnwSEbEiABPZN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glQENvEWc8pAhTEqXGxWdsEKIht4qznFAEK4tS42KxtAhTENnHWc4oABXFqXGzWNgEKYps46zlF4P96ztDnxVFciwAAAABJRU5ErkJggg==", + layoutConfig: layoutConfig().just(), + width: 30, + height: 30, + }), + ], + { + layoutConfig: layoutConfig().fit().configWidth(LayoutSpec.MOST), + } + ), + this.movieTitle = text({ textSize: 20, }), @@ -140,51 +156,95 @@ class MovieVM extends ViewModel{ network(context).get("https://douban.uieee.com/v2/movie/top250").then(ret => { this.updateState(state => state.doubanModel = JSON.parse(ret.data) as DoubanModel) }) + this.updateState(state => { + state.anchorPos = padding + frameWidth / 2 + state.selectedIdx = 0 + }) + let scrollX = 0 vh.scrolled.onScroll = (offset) => { - const frameWidth = 270 / 2 * 1.5 - const centerX = offset.x + Environment.screenWidth / 2 - const idx = Math.floor(centerX / frameWidth) - if (state.selectedIdx != idx) { + if (offset.x < 0 || offset.x > (state.doubanModel?.count || 0) * frameWidth + padding * 2 - Environment.screenWidth) { + return + } + const dx = offset.x - scrollX + scrollX = offset.x + const idx = Math.floor((offset.x + state.anchorPos - padding) / frameWidth) + if (state.selectedIdx !== idx) { this.updateState(state => state.selectedIdx = idx) } - const overOff = Math.abs(centerX - frameWidth * (idx + 0.5)) - const scale = overOff / (frameWidth / 2) - takeNonNull(this.images.get(idx))(v => { - v.scaleX = v.scaleY = 1.5 - 0.5 * scale + takeNonNull(this.images.get(idx))(it => { + const scale = (offset.x + state.anchorPos - (idx + 0.5) * frameWidth - padding) / (frameWidth / 2) + it.scaleX = it.scaleY = 1.5 - Math.abs(scale * 0.5) }) + this.updateArrow() } } + updateArrow() { + takeNonNull(this.images.get(this.getState().selectedIdx))(it => { + it.getLocationOnScreen(context).then(ret => { + this.getViewHolder().anchor.centerX = ret.x + frameWidth / 2 + }) + }) + } + + onItemClicked(idx: number) { + takeNonNull(this.images.get(this.getState().selectedIdx)?.superview)(it => { + it.scaleX = it.scaleY = 1 + }) + takeNonNull(this.images.get(idx)?.superview)(it => { + it.getLocationOnScreen(context).then(ret => { + let anchor = this.getState().anchorPos + if (ret.x < 0) { + this.getViewHolder().scrolled.scrollBy(context, { x: ret.x, y: 0 }, true) + anchor = frameWidth / 2 + } else if (ret.x > Environment.screenWidth - frameWidth) { + this.getViewHolder().scrolled.scrollBy( + context, + { x: ret.x - (Environment.screenWidth - frameWidth), y: 0 }, + true) + anchor = Environment.screenWidth - frameWidth / 2 + } else { + anchor = ret.x + frameWidth / 2 + } + this.updateState(state => { + state.selectedIdx = idx + state.anchorPos = anchor + }) + }) + }) + } + onBind(state: MovieModel, vh: MovieVH) { if (state.doubanModel) { vh.title.text = state.doubanModel.title vh.gallery.children.length = 0 + const vm = this state.doubanModel.subjects.forEach((e, idx) => { vh.gallery.addChild(stack( [ image({ layoutConfig: layoutConfig().just().configAlignment(Gravity.Center), - width: 270 / 2, - height: 400 / 2, + width: frameWidth / 1.5, + height: frameHeight / 1.5, imageUrl: e.images.large, onClick: function () { const v = (this as Image).superview if (v == undefined) { return } - v.getLocationOnScreen(context).then(ret => { - const centerX = ret.x + v.width / 2; - vh.scrolled.scrollBy(context, { x: centerX - Environment.screenWidth / 2, y: 0 }, true) - }) + vm.onItemClicked(idx) }, }).also(it => { this.images.set(idx, it) + if (state.selectedIdx == idx) { + it.scaleX = it.scaleY = 1.5 + } }) ], { layoutConfig: layoutConfig().just(), - width: 270 / 2 * 1.5, - height: 400 / 2 * 1.5, + width: frameWidth, + height: frameHeight, })) }) takeNonNull(state.doubanModel.subjects[state.selectedIdx])(it => { @@ -192,6 +252,8 @@ class MovieVM extends ViewModel{ vh.movieYear.text = it.year }) } + vh.anchor.centerX = state.anchorPos + this.updateArrow() } } @@ -205,7 +267,7 @@ class SliderPanel extends VMPanel{ } getState() { - return { selectedIdx: 0, doubanModel: MovieData } + return { selectedIdx: 0, anchorPos: Environment.screenWidth / 2, doubanModel: MovieData } } } \ No newline at end of file