QMUINavigationBarScrollingSnapAnimator

@interface QMUINavigationBarScrollingSnapAnimator : QMUIScrollAnimator

实现通过界面上的 UIScrollView 滚动来控制顶部导航栏外观的类,当滚动到某个位置时,即触发导航栏外观的变化。

使用方式:

  1. 用 init 方法初始化。
  2. 通过 scrollView 属性关联一个 UIScrollView。
  3. 修改 offsetYToStartAnimation 调整动画触发的滚动位置。

Note

注意,由于在同个 UINavigationController 里的所有 viewController 的 navigationBar 都是共享的,所以如果修改了 navigationBar 的样式,需要自行处理界面切换时 navigationBar 的样式恢复。
  • 指定要关联的 UINavigationBar,若不指定,会自动寻找当前 App 可视界面上的 navigationBar

    Declaration

    Objective-C

    @property (nonatomic, weak) UINavigationBar *_Nullable navigationBar;

    Swift

    weak var navigationBar: UINavigationBar? { get set }
  • contentOffset.y 到达哪个值即开始动画,默认为 0。

    Note

    注意,如果 adjustsOffsetYWithInsetTopAutomatically 为 YES,则实际计算时的值为 (-contentInset.top + offsetYToStartAnimation),这时候 offsetYToStartAnimation = 0 则表示在列表默认的停靠位置往下拉就会触发临界点。

    Declaration

    Objective-C

    @property (nonatomic) CGFloat offsetYToStartAnimation;

    Swift

    var offsetYToStartAnimation: CGFloat { get set }
  • 传给 offsetYToStartAnimation 的值是否要自动叠加上 -contentInset.top,默认为 YES。

    Declaration

    Objective-C

    @property (nonatomic) BOOL adjustsOffsetYWithInsetTopAutomatically;

    Swift

    var adjustsOffsetYWithInsetTopAutomatically: Bool { get set }
  • 当滚动到触发位置时,可在 block 里执行动画

    Declaration

    Objective-C

    @property (nonatomic, copy) void (^_Nonnull) (QMUINavigationBarScrollingSnapAnimator *_Nonnull, BOOL) animationBlock;

    Swift

    var animationBlock: (QMUINavigationBarScrollingSnapAnimator, Bool) -> Void { get set }

    Parameters

    animator

    当前的 animator 对象

    offsetYReached

    是否已经过了临界点(也即 offsetYToStartAnimation)

  • 是否已经过了临界点(也即 offsetYToStartAnimation)

    Declaration

    Objective-C

    @property (nonatomic, readonly) BOOL offsetYReached;

    Swift

    var offsetYReached: Bool { get }
  • 如果为 NO,则当 offsetYReached 的值不再变化(例如 YES 后继续往下滚动,或者 NO 后继续往上滚动)时,就不会再触发动画,从而提升性能。

    如果为 YES,则任何时候只要有滚动产生,动画就会被触发,适合运用到类似 Plain Style 的 UITableView 里在滚动时也要适配停靠的 sectionHeader 的场景(因为需要不断计算当前正在停靠的 sectionHeader 是哪一个)。

    默认为 NO

    Declaration

    Objective-C

    @property (nonatomic) BOOL continuous;

    Swift

    var continuous: Bool { get set }