注册

OC滚动时自动显示与隐藏导航条定制

本帖最后由 行走的鼠标 于 2015-3-13 08:50 编辑

//
// AMScrollingNavbarViewController.h
// AMScrollingNavbar
//
// Created by Andrea on 08/11/13.
// Copyright (c) 2013 Andrea Mazzini. All rights reserved.
//

#import

/*!
* @name HYBScrollRenderController
* @brief 在滚动视图向上滚动时,慢慢隐藏导航条或者向下滚动时,慢慢显示导航条,支持6.0以上版本系统。
* 在任何需要使用此功能的控制器类中,只要直接继承于此HYBScrollRenderController类,并在
* 创建滚动视图的时候,调用-followScrollView:方法来设置即可。
* @note 如果此类需要继承更多的特性,可直接修改继承的父类为自定义的控制器类。也可以把此类作为所有
* 类的基础父类,在继承此类的基础上,扩展所有基础属性。如HYBSuperBaseController : HYBScrollRenderController
* @author huangyibiao
*/
@inte**ce HYBScrollRenderController : UIViewController

/*!
* @brief 允许滚动的视图,支付UIScrollView、UITableView、UIWebView
* @param scrollableView 可滚动的视图
*/
- (void)followScrollView:(UIView *)scrollableView;

@end

//
// AMScrollingNavbarViewController.m
// AMScrollingNavbar
//
// Created by Andrea on 08/11/13.
// Copyright (c) 2013 Andrea Mazzini. All rights reserved.
//

#import "HYBScrollRenderController.h"

@inte**ce HYBScrollRenderController ()

@property (nonatomic, weak) UIView *scrollableView;
@property (assign, nonatomic) float lastContentOffset;
@property (strong, nonatomic) UIPanGestureRecognizer *panGesture;
@property (strong, nonatomic) UIView *overlay;
@property (assign, nonatomic) BOOL isCollapsed;
@property (assign, nonatomic) BOOL i**panded;

@end

@implementation HYBScrollRenderController

- (void)followScrollView:(UIView *)scrollableView {
self.scrollableView = scrollableView;

self.panGesture = [ initWithTarget:self action:@selector(handlePan:)];
;

;
;

/* The navbar fadeout is achieved using an overlay view with the same barTintColor.
this might be improved by adjusting the alpha component of every navbar child */
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin = CGPointZero;
self.overlay = [ initWithFrame:frame];
if () {
if (self.navigationController.navigationBar.barTintColor) {
;
}
}

;
;
;
return;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

- (void)handlePan:(UIPanGestureRecognizer*)gesture {
CGPoint translation = [gesture translationInView:];

float delta = self.lastContentOffset - translation.y;
self.lastContentOffset = translation.y;
CGRect frame;

if (delta > 0) {
if (self.isCollapsed) {
return;
}

frame = self.navigationController.navigationBar.frame;
if (frame.origin.y - delta < -24) {
delta = frame.origin.y + 24;
}

frame.origin.y = MAX(-24, frame.origin.y - delta);
self.navigationController.navigationBar.frame = frame;
if (frame.origin.y == -24) {
self.isCollapsed = YES;
self.i**panded = NO;
}

;

// Keeps the view's scroll position steady until the navbar is gone
if ([self.scrollableView isKindOfClass:]) {
UIScrollView *scrollView = (UIScrollView *)self.scrollableView;
;
}
}

if (delta < 0) {
if (self.i**panded) {
return;
}

frame = self.navigationController.navigationBar.frame;
if (frame.origin.y - delta > 20) {
delta = frame.origin.y - 20;
}
frame.origin.y = MIN(20, frame.origin.y - delta);
self.navigationController.navigationBar.frame = frame;
if (frame.origin.y == 20) {
self.i**panded = YES;
self.isCollapsed = NO;
}

;
}

if ( == UIGestureRecognizerStateEnded) {
self.lastContentOffset = 0;
;
}
return;
}

- (void)checkForPartialScroll {
CGFloat pos = self.navigationController.navigationBar.frame.origin.y;

// Get back down
if (pos >= -2) {
[UIView animateWithDuration:0.2 animations:^{
CGRect frame;
frame = self.navigationController.navigationBar.frame;
CGFloat delta = frame.origin.y - 20;
frame.origin.y = MIN(20, frame.origin.y - delta);
self.navigationController.navigationBar.frame = frame;

self.i**panded = YES;
self.isCollapsed = NO;
;
}];
} else {
// And back up
[UIView animateWithDuration:0.2 animations:^{
CGRect frame;
frame = self.navigationController.navigationBar.frame;
CGFloat delta = frame.origin.y + 24;
frame.origin.y = MAX(-24, frame.origin.y - delta);
self.navigationController.navigationBar.frame = frame;

self.i**panded = NO;
self.isCollapsed = YES;
;
}];
}
return;
}

- (void)updateSizingWithDelta:(CGFloat)delta {
CGRect frame = self.navigationController.navigationBar.frame;

float alpha = (frame.origin.y + 24) / frame.size.height;
;
self.navigationController.navigationBar.tintColor = ;

frame = self.scrollableView.superview.frame;

frame.origin.y = self.navigationController.navigationBar.frame.origin.y;
frame.origin.y += self.navigationController.navigationBar.frame.size.height;

// 判断是否是ios7及其以上版本
if (!) {
frame.origin.y = self.navigationController.navigationBar.frame.origin.y - 20;
}
frame.size.height = frame.size.height + delta;
self.scrollableView.superview.frame = frame;

// Changing the layer's frame avoids UIWebView's glitchiness
frame = self.scrollableView.layer.frame;
frame.size.height += delta;
self.scrollableView.layer.frame = frame;
return;
}

- (BOOL)isIOS7 {
return (.**Version.integerValue >= 7) ? YES : NO;
}

@end

已邀请:
多谢。正好在找相关的实现方法!

要回复问题请先登录注册