【问题标题】:Router navigate on Ionic with animation blinking on ios using angular animations路由器在 Ionic 上导航,动画在 ios 上使用角度动画闪烁
【发布时间】:2019-10-02 17:39:41
【问题描述】:

我正在使用 @angular/animations 在 Ionic (v4) 上的两个页面之间设置页面转换,这一切都适用于 chrome,但不适用于 safari。

在一个只有两个页面(急切加载)的简单应用程序上,我仍然遇到问题,与我尝试设置动画的属性无关。如果我创建一个空动画,闪烁只会消失,但这不是目标。另外,我正在使用急切加载。

动画.ts

 export const fadeIn =
    trigger('fadeIn', [
      state('entering', style({ //styles to be transitioned
        opacity: 1,

      })),
      transition("* => entering", [ //this styles will enter before the animation starts
          style({
            opacity: 0,
            display: block
          }),
          animate('500ms')
      ]),

      state('leaving', style({  //this styles will enter when the animation ends
        opacity: 0,
        display: none
      })),
      transition("* => leaving", [ //styles to be transitioned
        animate('220ms', style({
          opacity: 0,
        }))
      ]),

page1.ts

... 
anim = ''
import { fadeIn } from '../animations/animations'
import { Router } from '@angular/router'
...
@Component({
  selector: 'page-1',
  templateUrl: './page1.html',
  styleUrls: ['./page1.scss'],
  animations: [ fadeIn ]
})

constructor(private router: Router){ }

ionViewWillEnter(){
  this.anim = 'entering'
}

nextPage(){
  this.router.navigate(['/page2'])
}

page2.ts

... 
import { fadeIn } from '../animations/animations'
import { Router } from '@angular/router'
...
anim = ''
@Component({
  selector: 'page-2',
  templateUrl: './page2.html',
  styleUrls: ['./page2.scss'],
  animations: [ fadeIn ]
})

constructor(private router: Router){ }

ionViewWillEnter(){
  this.anim = 'entering'
}

previousPage(){
  this.anim = 'leaving'
  setTimeout(() => {
    this.router.navigate(['/page1'])
  }, 200) //220 is the actual time to the transition end, but 200 to make sure that the blinking is not by an 'empty animation state'
}

page1.html

<ion-content [@fadeIn]='anim'>
  <h1> This is the first page!
</ion-content>

page2.html

<ion-content [@fadeIn]='anim'>
  <h1> This is the second page!
</ion-content>

page1.scss 和 page2.scss

ion-content{
  display: none;
  opacity: 0;
}

global.scss

@import "~@ionic/angular/css/core.css";
html > body > app-root > ion-app > ion-router-outlet > .ion-page-hidden {
  display: flex !important;
}
...

为了更好地说明问题,我用慢动作记录下来并上传到Giphy

我希望在 safari 上的 chrome 上得到相同的结果,即使用这种动画结构而不在页面之间闪烁

【问题讨论】:

  • 如果将动画(和自定义 css 规则)应用到 ion-content 而不是内部 div 会发生什么?我认为您的动画可能与 Ionic 从一页导航到另一页时使用的动画冲突...
  • 在 ion-content 中动画效果很好。我认为您的猜测是正确的,但是您认为我可以覆盖默认动画吗?我已经尝试将 animated=false 放在 ion-router-outlet 上。
  • &lt;ion-router-outlet main animated="false"&gt;&lt;/ion-router-outlet&gt;没用?
  • 是的,没用
  • 您使用的是哪个版本的 Ionic?你介意更新到4.4吗?

标签: angular ionic4


【解决方案1】:

我会添加这个答案,因为评论有点长。正如我们在 cmets 中所讨论的,问题是因为您的动画与默认的 iOS page transition animation

冲突

解决此问题的推荐方法是创建您自己的页面过渡动画,如您所见in this answer。这样做会应用您的自定义动画,覆盖整个应用程序中所有ion-navion-router-outlet 的默认“动画”

如果出于某种原因您只想在每个页面上使用 Angular 动画 - 就像您在代码中使用类似

的东西
<ion-content [@fadeIn]='anim'> 
  ...
</ion-content>

您可以通过 the Config 禁用 Ionic 的默认页面过渡动画:

动画:布尔值 |如果为 true,Ionic 将启用应用程序中的所有动画和过渡。

@NgModule({
  // ...
  imports: [
    // ...
    IonicModule.forRoot({
      animated: false
    }),
  ],
  //...

【讨论】:

  • 我认为您对故障的动机是正确的,即使它在更改 app.module 上的动画属性后仍然存在。由于我为此苦苦挣扎了好几天,并且还有其他动画方式可用,因此我将给予正确的答案。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多