OC编程中,有一个类CADisplayLink
,我们可以借助这个类来实现一些循环检测任务,那么在Flutter中有没有类似的机制呢?
Flutter中可以借用下面两种方式实现类似的功能
Ticker
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
late Ticker _ticker;
@override
void initState() {
_ticker = this.createTicker((elapsed) {
// 循环调用,退后台停止,到前台自动激活
// 这里会一直循环调用,实测试60次/s 左右的频率,等同于帧率
print("_ticker $elapsed");
});
_ticker.start();
super.initState();
}
}
AnimationController
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
late AnimationController _animationController;
@override
void initState() {
_animationController =
AnimationController(vsync: this, duration: Duration(seconds: 2));
_animationController.addListener(() {
// 循环调用,退后台停止,到前台自动激活
print("_animationController ${DateTime.now()}");
});
_animationController.repeat();
super.initState();
}
}
网友评论