cocos creator从零开发简单框架(09)-UI渐入渐出
UI
打开和退出目前是直接显示和隐藏,给它加个渐入渐出的效果。
编辑framework/scripts/AppConstants.ts
,添加UI
显示方式。
// UI 显示方式
public static readonly uiShowStyle = {
// 默认
Normal: 1,
// 渐入
FadeIn: 2,
}
编辑framework/scripts/view/UIBase.ts
,定义UI
显示方式和过度时间。
public uiName: string
public uiShowStyle: number = AppConstants.uiShowStyle.Normal
public duration: number = 0.5
编辑framework/scripts/view/UIMgr.ts
,修改hideLast
方法。
private static hideLast(duration?: number) {
const last = this._current
if (!last) return
this._current = null
last.hiding()
if (!duration) {
this.hideUI(last)
} else {
cc.tween(last.skin).
to(duration, { opacity: 0 }).
call(() => this.hideUI(last)).
start()
}
}
添加showNormal
和showFadeIn
方法。
//#region 显示方式
/** 默认 */
private static showNormal(go: UIBase) {
go.skin.opacity = 255
go.skin.active = true
go.showed()
this.hideLast()
this.showUIAfter(go)
}
/** 渐入 */
private static showFadeIn(go: UIBase) {
go.skin.opacity = 0
go.skin.active = true
cc.tween(go.skin).
to(go.duration, { opacity: 255 }).
call(() => {
go.showed()
this.showUIAfter(go)
}).
start()
this.hideLast(go.duration)
}
//#endregion
修改startShowUI
方法。
private static startShowUI(go: UIBase) {
go.showing()
// 挂载 Widget 组件, 保持跟 Canvas 一样大小
AppUtil.setWidget(go.skin)
// 添加对象到 UI层级
LayerMgr.setLayer(go.skin, AppConstants.viewLayer.UI)
if (go.uiShowStyle == AppConstants.uiShowStyle.Normal || !this._current || go.duration <= 0) {
this.showNormal(go)
return
}
switch (go.uiShowStyle) {
case AppConstants.uiShowStyle.FadeIn:
this.showFadeIn(go)
break
}
}
编辑scripts/UIBag.ts
,定义显示方式为渐入渐出。
public skinPath: string = 'UIBag'
public uiShowStyle: number = AppConstants.uiShowStyle.FadeIn
运行程序,从UIMain
界面切换到UIBag
界面有渐入渐出的效果了。
本作品采用《CC 协议》,转载必须注明作者和本文链接