• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 全屏模态转场
2
3通过bindContentCover属性为组件绑定全屏模态页面,在组件插入和删除时可通过设置转场参数ModalTransition显示过渡动效。
4
5>  **说明:**
6>
7>  从API Version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8>
9>  不支持横竖屏切换。
10>
11>  不支持路由跳转。
12
13## 属性
14
15| 名称               | 参数                                       | 参数描述                                     |
16| ---------------- | ---------------------------------------- | ---------------------------------------- |
17| bindContentCover | isShow: boolean,<br>builder: [CustomBuilder](ts-types.md#custombuilder8),<br>options?: [ContentCoverOptions](#contentcoveroptions) | 给组件绑定全屏模态页面,点击后显示模态页面。模态页面内容自定义,显示方式可设置无动画过渡,上下切换过渡以及透明渐变过渡方式。<br> isShow: 是否显示全屏模态页面。<br>从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量<br>builder: 配置全屏模态页面内容。<br> options: 配置全屏模态页面的可选属性。 |
18## ContentCoverOptions
19| 名称              | 类型                                       | 必填   | 描述            |
20| --------------- | ---------------------------------------- | ---- | ------------- |
21| modalTransition | [ModalTransition](ts-types.md#modaltransition10) | 否    | 全屏模态页面的转场方式。  |
22| backgroundColor | [ResourceColor](ts-types.md#resourcecolor) | 否    | 全屏模态页面的背板颜色。  |
23| onAppear        | () => void                               | 否    | 全屏模态页面显示回调函数。 |
24| onDisappear     | () => void                               | 否    | 全屏模态页面回退回调函数。 |
25
26## 示例
27
28### 示例1
29
30全屏模态无动画转场模式下,自定义转场动画。
31
32```ts
33// xxx.ets
34@Entry
35@Component
36struct ModalTransitionExample {
37  @State isShow:boolean = false
38  @State isShow2:boolean = false
39
40  @Builder myBuilder2() {
41    Column() {
42      Button("close modal 2")
43        .margin(10)
44        .fontSize(20)
45        .onClick(()=>{
46          this.isShow2 = false;
47        })
48    }
49    .width('100%')
50    .height('100%')
51  }
52
53  @Builder myBuilder() {
54    Column() {
55      Button("transition modal 2")
56        .margin(10)
57        .fontSize(20)
58        .onClick(()=>{
59          this.isShow2 = true;
60        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Orange, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
61
62      Button("close modal 1")
63        .margin(10)
64        .fontSize(20)
65        .onClick(()=>{
66          this.isShow = false;
67        })
68    }
69    .width('100%')
70    .height('100%')
71    .justifyContent(FlexAlign.Center)
72  }
73
74  build() {
75    Column() {
76      Button("transition modal 1")
77        .onClick(() => {
78          this.isShow = true
79        })
80        .fontSize(20)
81        .margin(10)
82        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
83    }
84    .justifyContent(FlexAlign.Center)
85    .backgroundColor("#ff49c8ab")
86    .width('100%')
87    .height('100%')
88  }
89}
90```
91
92![zh-cn_full_screen_modal_none_1](figures/zh-cn_full_screen_modal_none_1.gif)
93
94### 示例2
95
96全屏模态无动画转场模式下,自定义转场动画。
97
98```ts
99// xxx.ets
100import curves from '@ohos.curves';
101@Entry
102@Component
103struct ModalTransitionExample {
104  @State  @Watch("isShow1Change") isShow:boolean = false
105  @State  @Watch("isShow2Change") isShow2:boolean = false
106  @State isScale1:number = 1;
107  @State isScale2:number = 1;
108  @State flag: boolean = true
109  @State show: string = 'show'
110
111  isShow1Change() {
112    this.isShow ? this.isScale1 = 0.95 : this.isScale1 = 1
113  }
114  isShow2Change() {
115    this.isShow2 ? this.isScale2 = 0.95 : this.isScale2 = 1
116  }
117  @Builder myBuilder2() {
118    Column() {
119      Button("close modal 2")
120        .margin(10)
121        .fontSize(20)
122        .onClick(()=>{
123          this.isShow2 = false;
124        })
125    }
126    .width('100%')
127    .height('100%')
128  }
129
130
131  @Builder myBuilder() {
132    Column() {
133      Button("transition modal 2")
134        .margin(10)
135        .fontSize(20)
136        .onClick(()=>{
137          this.isShow2 = true;
138        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Orange, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
139
140      Button("close modal 1")
141        .margin(10)
142        .fontSize(20)
143        .onClick(()=>{
144          this.isShow = false;
145        })
146    }
147    .width('100%')
148    .height('100%')
149    .justifyContent(FlexAlign.Center)
150    .scale({x: this.isScale2, y: this.isScale2})
151    .animation({curve:curves.springMotion()})
152  }
153
154  build() {
155    Column() {
156      Button("transition modal 1")
157        .onClick(() => {
158          this.isShow = true
159        })
160        .fontSize(20)
161        .margin(10)
162        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.NONE, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
163    }
164    .justifyContent(FlexAlign.Center)
165    .backgroundColor("#ff49c8ab")
166    .width('100%')
167    .height('100%')
168    .scale({ x: this.isScale1, y: this.isScale1 })
169    .animation({ curve: curves.springMotion() })
170  }
171}
172```
173
174![zh-cn_full_screen_modal_none_2](figures/zh-cn_full_screen_modal_none_2.gif)
175
176### 示例3
177
178全屏模态上下切换转场。
179
180```ts
181// xxx.ets
182@Entry
183@Component
184struct ModalTransitionExample {
185  @State isShow:boolean = false
186  @State isShow2:boolean = false
187
188  @Builder myBuilder2() {
189    Column() {
190      Button("close modal 2")
191        .margin(10)
192        .fontSize(20)
193        .onClick(()=>{
194          this.isShow2 = false;
195        })
196    }
197    .width('100%')
198    .height('100%')
199  }
200
201  @Builder myBuilder() {
202    Column() {
203      Button("transition modal 2")
204        .margin(10)
205        .fontSize(20)
206        .onClick(()=>{
207          this.isShow2 = true;
208        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.DEFAULT, backgroundColor: Color.Gray, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
209
210      Button("close modal 1")
211        .margin(10)
212        .fontSize(20)
213        .onClick(()=>{
214          this.isShow = false;
215        })
216    }
217    .width('100%')
218    .height('100%')
219    .justifyContent(FlexAlign.Center)
220  }
221
222  build() {
223    Column() {
224      Button("transition modal 1")
225        .onClick(() => {
226          this.isShow = true
227        })
228        .fontSize(20)
229        .margin(10)
230        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.DEFAULT, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
231    }
232    .justifyContent(FlexAlign.Center)
233    .backgroundColor(Color.White)
234    .width('100%')
235    .height('100%')
236  }
237}
238```
239
240![zh-cn_full_screen_modal_default](figures/zh-cn_full_screen_modal_default.gif)
241
242### 示例4
243
244全屏模态透明度渐变转场。
245
246```ts
247// xxx.ets
248@Entry
249@Component
250struct ModalTransitionExample {
251  @State isShow:boolean = false
252  @State isShow2:boolean = false
253
254  @Builder myBuilder2() {
255    Column() {
256      Button("close modal 2")
257        .margin(10)
258        .fontSize(20)
259        .onClick(()=>{
260          this.isShow2 = false;
261        })
262    }
263    .width('100%')
264    .height('100%')
265    .justifyContent(FlexAlign.Center)
266  }
267
268
269  @Builder myBuilder() {
270    Column() {
271      Button("transition modal 2")
272        .margin(10)
273        .fontSize(20)
274        .onClick(()=>{
275          this.isShow2 = true;
276        }).bindContentCover(this.isShow2, this.myBuilder2(), {modalTransition: ModalTransition.ALPHA, backgroundColor: Color.Gray, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
277
278      Button("close modal 1")
279        .margin(10)
280        .fontSize(20)
281        .onClick(()=>{
282          this.isShow = false;
283        })
284    }
285    .width('100%')
286    .height('100%')
287    .justifyContent(FlexAlign.Center)
288  }
289
290  build() {
291    Column() {
292      Button("transition modal 1")
293        .onClick(() => {
294          this.isShow = true
295        })
296        .fontSize(20)
297        .margin(10)
298        .bindContentCover(this.isShow, this.myBuilder(), {modalTransition: ModalTransition.ALPHA, backgroundColor: Color.Pink, onAppear: () => {console.log("BindContentCover onAppear.")}, onDisappear: () => {console.log("BindContentCover onDisappear.")}})
299    }
300    .justifyContent(FlexAlign.Center)
301    .backgroundColor(Color.White)
302    .width('100%')
303    .height('100%')
304  }
305}
306```
307
308![zh-cn_full_screen_modal_alpha](figures/zh-cn_full_screen_modal_alpha.gif)
309