HarmonyOS鸿蒙Next中如何在Swiper中使用不同的背景颜色和文本对内容进行展示?

HarmonyOS鸿蒙Next中如何在Swiper中使用不同的背景颜色和文本对内容进行展示? 如何在Swiper中使用不同的背景颜色和文本对内容进行展示?#HarmonyOS最强问答官#


更多关于HarmonyOS鸿蒙Next中如何在Swiper中使用不同的背景颜色和文本对内容进行展示?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

关于设置不同的颜色和文本可以参考这个demo:

// xxx.ets
@Entry
@Component
struct SwiperCustomAnimationExample {
  private DISPLAY_COUNT: number = 2
  private MIN_SCALE: number = 0.75

  @State backgroundColors: Color[] = [Color.Green, Color.Blue, Color.Yellow, Color.Pink, Color.Gray, Color.Orange]
  @State opacityList: number[] = []
  @State scaleList: number[] = []
  @State translateList: number[] = []
  @State zIndexList: number[] = []

  aboutToAppear(): void {
    for (let i = 0; i < this.backgroundColors.length; i++) {
      this.opacityList.push(1.0)
      this.scaleList.push(1.0)
      this.translateList.push(0.0)
      this.zIndexList.push(0)
    }
  }

  build() {
    Column() {
      Swiper() {
        ForEach(this.backgroundColors, (backgroundColor: Color, index: number) => {
          Text(index.toString()).width('100%').height('100%').fontSize(50).textAlign(TextAlign.Center)
            .backgroundColor(backgroundColor)
            // 自定义动画变化透明度、缩放页面、抵消系统默认位移、渲染层级等
            .opacity(this.opacityList[index])
            .scale({ x: this.scaleList[index], y: this.scaleList[index] })
            .translate({ x: this.translateList[index] })
            .zIndex(this.zIndexList[index])
        })
      }
      .height(300)
      .indicator(false)
      .displayCount(this.DISPLAY_COUNT, true)
      .customContentTransition({
        // 页面移除视窗时超时1000ms下渲染树
        timeout: 1000,
        // 对视窗内所有页面逐帧回调transition,在回调中修改opacity、scale、translate、zIndex等属性值,实现自定义动画
        transition: (proxy: SwiperContentTransitionProxy) => {
          if (proxy.position <= proxy.index % this.DISPLAY_COUNT || proxy.position >= this.DISPLAY_COUNT + proxy.index % this.DISPLAY_COUNT) {
            // 同组页面往左滑或往右完全滑出视窗外时,重置属性值
            this.opacityList[proxy.index] = 1.0
            this.scaleList[proxy.index] = 1.0
            this.translateList[proxy.index] = 0.0
            this.zIndexList[proxy.index] = 0
          } else {
            // 同组页面往右滑且未滑出视窗外时,对同组中左右两个页面,逐帧根据position修改属性值,实现两个页面往Swiper中间靠拢并透明缩放的自定义切换动画
            if (proxy.index % this.DISPLAY_COUNT === 0) {
              this.opacityList[proxy.index] = 1 - proxy.position / this.DISPLAY_COUNT
              this.scaleList[proxy.index] = this.MIN_SCALE + (1 - this.MIN_SCALE) * (1 - proxy.position / this.DISPLAY_COUNT)
              this.translateList[proxy.index] = - proxy.position * proxy.mainAxisLength + (1 - this.scaleList[proxy.index]) * proxy.mainAxisLength / 2.0
            } else {
              this.opacityList[proxy.index] = 1 - (proxy.position - 1) / this.DISPLAY_COUNT
              this.scaleList[proxy.index] = this.MIN_SCALE + (1 - this.MIN_SCALE) * (1 - (proxy.position - 1) / this.DISPLAY_COUNT)
              this.translateList[proxy.index] = - (proxy.position - 1) * proxy.mainAxisLength - (1 - this.scaleList[proxy.index]) * proxy.mainAxisLength / 2.0
            }
            this.zIndexList[proxy.index] = -1
          }
        }
      })
      .onContentDidScroll(selectedIndex: number, index: number, position: number, mainAxisLength: number) {
        // 监听Swiper页面滑动事件,在该回调中可以实现自定义导航点切换动画等
        console.info("onContentDidScroll selectedIndex: " + selectedIndex + ", index: " + index + ", position: " + position + ", mainAxisLength: " + mainAxisLength)
      }
    }.width('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中如何在Swiper中使用不同的背景颜色和文本对内容进行展示?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,你可以通过自定义Swiper组件来实现不同背景颜色和文本的展示。首先,使用Swiper组件包裹需要展示的内容。然后,通过SwiperitemBuilder属性动态生成每个页面的内容。在itemBuilder中,你可以为每个页面设置不同的背景颜色和文本。例如:

@Entry
@Component
struct MySwiper {
  private colors: string[] = ['#FF5733', '#33FF57', '#3357FF'];
  private texts: string[] = ['Page 1', 'Page 2', 'Page 3'];

  build() {
    Swiper() {
      ForEach(this.colors, (color, index) => {
        Column() {
          Text(this.texts[index])
            .fontSize(30)
            .fontColor(Color.White)
        }
        .width('100%')
        .height('100%')
        .backgroundColor(color)
      })
    }
    .indicator(true)
    .loop(true)
  }
}

在这个示例中,colors数组定义了每个页面的背景颜色,texts数组定义了每个页面的文本内容。通过ForEach循环,动态生成每个页面的Column组件,并设置相应的背景颜色和文本。Swiper组件的indicator属性用于显示页面指示器,loop属性允许页面循环滚动。

在HarmonyOS鸿蒙Next中,可以通过Swiper组件结合自定义布局来实现不同背景颜色和文本的展示。具体步骤如下:

  1. 创建Swiper组件:在布局文件中使用<Swiper>标签定义Swiper组件。
  2. 自定义布局:为每个Swiper项创建自定义布局,包含背景颜色和文本控件。
  3. 绑定数据:通过数据绑定或动态设置,为每个Swiper项应用不同的背景颜色和文本内容。
  4. 样式调整:根据需求调整布局和样式,确保展示效果符合预期。

示例代码片段:

<Swiper>
    <SwiperItem background="#FF0000" text="Red Page"/>
    <SwiperItem background="#00FF00" text="Green Page"/>
    <SwiperItem background="#0000FF" text="Blue Page"/>
</Swiper>

通过这种方式,可以在Swiper中展示不同背景颜色和文本内容。

回到顶部