You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI下拉刷新:如何隐藏静止状态下的箭头组件?

解决静态状态下隐藏下拉刷新箭头的问题

要隐藏静止状态下的箭头,只需给Image添加一个条件化的透明度修饰符,根据refresh的状态控制箭头的可见性:

修改后的Image代码:

Image(systemName: "arrow.down")
      .font(.system(size: 16, weight: .heavy))
      .foregroundColor(.gray)
      .rotationEffect(.init(degrees: refresh.started ? 180 : 0))
      .offset(y: -25)
      .opacity(refresh.started ? 1 : 0) // 新增:静止时透明,刷新时显示
      .animation(.easeIn, value: refresh.started) // 优化动画绑定,确保状态变化时平滑过渡

原理说明:

  • 静止状态下,refresh.started为false,箭头透明度被设为0,完全不可见;
  • 当用户下拉ScrollView使得偏移量超过80时,refresh.started被设为true,箭头透明度恢复为1,同时配合旋转动画自然显示。

可选方案(完全移除布局):

如果你希望静止时箭头彻底从布局中消失(而非仅透明),可以用visibility修饰符替代opacity:

.visibility(refresh.started ? .visible : .hidden)

内容的提问来源于stack exchange,提问作者John Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:45:37