QMUINavigationBarScrollingAnimator

@interface QMUINavigationBarScrollingAnimator : QMUIScrollAnimator

实现通过界面上的 UIScrollView 滚动来控制顶部导航栏外观的类,导航栏外观会跟随滚动距离的变化而变化。

使用方式:

  1. 用 init 方法初始化。
  2. 通过 scrollView 属性关联一个 UIScrollView。
  3. 修改 offsetYToStartAnimation 调整动画触发的滚动位置。
  4. 修改 distanceToStopAnimation 调整动画触发后滚动多久到达终点。

Note

注意,由于在同个 UINavigationController 里的所有 viewController 的 navigationBar 都是共享的,所以如果修改了 navigationBar 的样式,需要自行处理界面切换时 navigationBar 的样式恢复。

Note

注意,为了性能考虑,在 progress 达到 0 后再往上滚,或者 progress 达到 1 后再往下滚,都不会再触发那一系列 animationBlock。
  • 指定要关联的 UINavigationBar,若不指定,会自动寻找当前 App 可视界面上的 navigationBar

    Declaration

    Objective-C

    @property (nonatomic, weak, nullable) UINavigationBar *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 开始,要滚动多长的距离就打到动画结束的位置,默认为 44

    Declaration

    Objective-C

    @property (nonatomic) CGFloat distanceToStopAnimation;

    Swift

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

    Declaration

    Objective-C

    @property (nonatomic) BOOL adjustsOffsetYWithInsetTopAutomatically;

    Swift

    var adjustsOffsetYWithInsetTopAutomatically: Bool { get set }
  • 当前滚动位置对应的进度

    Declaration

    Objective-C

    @property (nonatomic, readonly) float progress;

    Swift

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

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

    默认为 NO

    Declaration

    Objective-C

    @property (nonatomic) BOOL continuous;

    Swift

    var continuous: Bool { get set }
  • 用于控制不同滚动位置下的表现,总的动画 block,如果定义了这个,则滚动时不会再调用后面那几个 block

    Declaration

    Objective-C

    @property (nonatomic, copy, nullable) void (^) (QMUINavigationBarScrollingAnimator *_Nonnull, float) animationBlock;

    Swift

    var animationBlock: ((QMUINavigationBarScrollingAnimator, Float) -> Void)? { get set }

    Parameters

    animator

    当前的 animator 对象

    progress

    当前滚动位置处于 offsetYToStartAnimation 到 (offsetYToStartAnimation + distanceToStopAnimation) 之间的哪个进度

  • 返回不同滚动位置下对应的背景图

    Declaration

    Objective-C

    @property (nonatomic, copy, nullable) UIImage *_Nonnull (^) (QMUINavigationBarScrollingAnimator *_Nonnull, float) backgroundImageBlock;

    Swift

    var backgroundImageBlock: ((QMUINavigationBarScrollingAnimator, Float) -> UIImage)? { get set }

    Parameters

    animator

    当前的 animator 对象

    progress

    当前滚动位置处于 offsetYToStartAnimation 到 (offsetYToStartAnimation + distanceToStopAnimation) 之间的哪个进度

  • 返回不同滚动位置下对应的导航栏底部分隔线的图片

    Declaration

    Objective-C

    @property (nonatomic, copy, nullable) UIImage *_Nonnull (^) (QMUINavigationBarScrollingAnimator *_Nonnull, float) shadowImageBlock;

    Swift

    var shadowImageBlock: ((QMUINavigationBarScrollingAnimator, Float) -> UIImage)? { get set }

    Parameters

    animator

    当前的 animator 对象

    progress

    当前滚动位置处于 offsetYToStartAnimation 到 (offsetYToStartAnimation + distanceToStopAnimation) 之间的哪个进度

  • 返回不同滚动位置下对应的导航栏的 tintColor

    Declaration

    Objective-C

    @property (nonatomic, copy, nullable) UIColor *_Nonnull (^) (QMUINavigationBarScrollingAnimator *_Nonnull, float) tintColorBlock;

    Swift

    var tintColorBlock: ((QMUINavigationBarScrollingAnimator, Float) -> UIColor)? { get set }

    Parameters

    animator

    当前的 animator 对象

    progress

    当前滚动位置处于 offsetYToStartAnimation 到 (offsetYToStartAnimation + distanceToStopAnimation) 之间的哪个进度

  • 返回不同滚动位置下对应的导航栏的 titleView tintColor

    Declaration

    Objective-C

    @property (nonatomic, copy, nullable) UIColor *_Nonnull (^) (QMUINavigationBarScrollingAnimator *_Nonnull, float) titleViewTintColorBlock;

    Swift

    var titleViewTintColorBlock: ((QMUINavigationBarScrollingAnimator, Float) -> UIColor)? { get set }

    Parameters

    animator

    当前的 animator 对象

    progress

    当前滚动位置处于 offsetYToStartAnimation 到 (offsetYToStartAnimation + distanceToStopAnimation) 之间的哪个进度

  • 返回不同滚动位置下对应的状态栏样式

    Warning

    需在项目的 Info.plist 文件内设置字段 “View controller-based status bar appearance” 的值为 NO 才能生效,如果不设置,或者值为 YES,则请自行通过系统提供的 - preferredStatusBarStyle 方法来实现,statusbarStyleBlock 无效

    Declaration

    Objective-C

    @property (nonatomic, copy, nullable) UIStatusBarStyle (^) (QMUINavigationBarScrollingAnimator *_Nonnull, float) statusbarStyleBlock;

    Swift

    var statusbarStyleBlock: ((QMUINavigationBarScrollingAnimator, Float) -> UIStatusBarStyle)? { get set }

    Parameters

    animator

    当前的 animator 对象

    progress

    当前滚动位置处于 offsetYToStartAnimation 到 (offsetYToStartAnimation + distanceToStopAnimation) 之间的哪个进度

  • 返回不同滚动位置下对应的导航栏的 barTintColor

    Declaration

    Objective-C

    @property (nonatomic, copy) UIColor *_Nonnull (^_Nonnull) (QMUINavigationBarScrollingAnimator *_Nonnull, float) barTintColorBlock;

    Swift

    var barTintColorBlock: (QMUINavigationBarScrollingAnimator, Float) -> UIColor { get set }

    Parameters

    animator

    当前的 animator 对象

    progress

    当前滚动位置处于 offsetYToStartAnimation 到 (offsetYToStartAnimation + distanceToStopAnimation) 之间的哪个进度