HarmonyOS 组件放大缩小demo

希望提供定制化的组件demo,包含一个组件放大缩小的动画效果,以及支持传入子组件(比如 TextInput,TextPicker等)

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

示例如下:

@Entry
@Component
struct TextInputTest {
  @State message: string = 'Hello World';
  private controller:TextInputController = new TextInputController();
  //输入框初始化状态
  @State marginLeft1:number = 40
  @State marginBottom1:number = 150
  @State fontSize1:number = 20
  @State textAreaValue:string = ""

  @State marginLeft2:number = 40
  @State marginBottom2:number = 0
  @State fontSize2:number = 20
  @State textInputValue:string = ""
  //文本选择器弹窗状态
  private selected: number | number[] = 0
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
  @State selectValue:string = '';
  @State marginLeft3:number = 40
  @State marginBottom3:number = 0
  @State fontSize3:number = 20

  @State modifier1: TextModifier = new TextModifier(this.marginLeft1,this.marginBottom1,this.fontSize1)
  @State modifier2: TextModifier = new TextModifier(this.marginLeft2,this.marginBottom2,this.fontSize2)
  @State modifier3: TextModifier = new TextModifier(this.marginLeft3,this.marginBottom3,this.fontSize3)

  build() {
    Column(){
      //TextArea多行输入框
      Column(){
        Stack() {
          Text("TextArea多行输入框")
            .fontSize(this.modifier1.fontSize)
            .textAlign(TextAlign.Start)
            .margin({left:this.modifier1.marginLeft,bottom:this.modifier1.marginBottom})
            .attributeModifier(this.modifier1)
            .width("100%")
            .animation({
              duration: 300,
              curve: Curve.Friction,
              iterations: 1,
              playMode: PlayMode.Normal
            })

          TextArea({text:$$this.textAreaValue})
            .onFocus(() => {
              if(this.textAreaValue === ""){
                this.modifier1.isFocus = true
              }
            })
            .onBlur(()=>{
              //如果未输入任何内容,提示性标题复原
              if(this.textAreaValue === ""){
                this.modifier1.isFocus = false
              }
            })
            .height("80%")
          // .backgroundColor("#00FFFFFF")  //背景透明

        }
      }
      .justifyContent(FlexAlign.Center)
      .height("30%")

      //TextInput单行输入框
      Column(){
        Stack() {
          Text("TextInput单行输入框")
            .fontSize(this.modifier2.fontSize)
            .textAlign(TextAlign.Start)
            .width("100%")
            .margin({left:this.modifier2.marginLeft,bottom:this.modifier2.marginBottom})
            .attributeModifier(this.modifier2)
            .animation({
              duration: 300,
              curve: Curve.Friction,
              iterations: 1,
              playMode: PlayMode.Normal
            })


          TextInput({text:$$this.textInputValue})
            .onFocus(() => {
              if(this.textInputValue === ""){
                this.modifier2.isFocus = true
              }
            })
            .onBlur(()=>{
              //如果未输入任何内容,提示性标题复原
              if(this.textInputValue === ""){
                this.modifier2.isFocus = false
              }
            })
          // .backgroundColor("#00FFFFFF")  //背景透明
        }
      }
      .justifyContent(FlexAlign.Center)
      .height("30%")

      //文本选择弹窗
      Column(){
        Stack(){
          Text("testPickerTest")
            .width("100%")
            .fontSize(this.fontSize3)
            .margin({left:this.marginLeft3,bottom:this.marginBottom3})
            .animation({
              duration: 300,
              curve: Curve.Friction,
              iterations: 1,
              playMode: PlayMode.Normal
            })

          Row(){
            Text(this.selectValue)
              .width("80%")
              .margin({left:10})
            Image($r("app.media.startIcon"))
              .height(20)
              .margin({right:10})
              .onClick(() => {
                TextPickerDialog.show({
                  range: this.fruits,
                  selected: this.selected,
                  onAccept: (value: TextPickerResult) => {
                    //选择完成改变位置
                    this.marginLeft3 = 10
                    this.marginBottom3 = 50
                    this.fontSize3 = 13
                    // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
                    this.selected = value.index
                    console.log(this.selected + '')
                    // 点击确定后,被选到的文本数据展示到页面
                    this.selectValue = value.value as string
                    console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
                  },
                })
              })
          }
          .backgroundColor("#33eeb8b8")  //背景
          .width("100%")
          .justifyContent(FlexAlign.SpaceBetween)
        }
      }
      .backgroundColor("#33f1d8d8")  //背景
      .justifyContent(FlexAlign.Center)
      .height("30%")
      // .height("50%")

    }
    .height('100%')
    .width('100%')
  }

}

//全局动态属性
class TextModifier implements AttributeModifier<TextAttribute> {
  isFocus: boolean|undefined = undefined
  marginLeft:number = 0
  marginBottom:number = 0
  fontSize:number = 0
  applyNormalAttribute(instance: TextAttribute): void {
    if (this.isFocus) {
      this.marginLeft -= 10
      this.marginBottom += 50
      this.fontSize -= 7
      instance
        .fontSize(this.fontSize)
        .margin({left:this.marginLeft,bottom:this.marginBottom})
    } else if(this.isFocus === false) {
      //初始化时会执行该回调,所以 isFocus 初始值为undefined
      this.marginLeft += 10
      this.marginBottom -= 50
      this.fontSize += 7
      instance
        .fontSize(this.fontSize)
        .margin({left:this.marginLeft,bottom:this.marginBottom})
    }
  }

  constructor(marginLeft: number, marginBottom: number, fontSize: number) {
    this.marginLeft = marginLeft;
    this.marginBottom = marginBottom;
    this.fontSize = fontSize;
  }

}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 点击图片放大缩小
70浏览 • 1回复 待解决
HarmonyOS 相机预览是否支持放大缩小
55浏览 • 1回复 待解决
HarmonyOS 如何实现放大缩小的动画?
417浏览 • 1回复 待解决
Swiper轮播图带放大缩小动效实践
1286浏览 • 1回复 待解决
Image组件如何实现双指手势放大
2428浏览 • 1回复 待解决
HarmonyOS 自定义struct缩小异常
64浏览 • 1回复 待解决
HarmonyOS 希望提供日期组件Demo
24浏览 • 1回复 待解决
使用Web组件下载能力Demo
458浏览 • 1回复 待解决