#import "ViewController.h"
@interface UIView (frame)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat bottomY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@end
@implementation UIView (frame)
-(CGFloat)x
{
return self.frame.origin.x;
}
-(void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
-(CGFloat)y
{
return self.frame.origin.y;
}
-(void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
-(CGFloat)bottomY
{
return self.frame.size.height+self.frame.origin.y;
}
-(void)setBottomY:(CGFloat)bottomY
{
CGRect frame = self.frame;
frame.origin.y = bottomY-self.frame.size.height;
self.frame = frame;
}
-(CGFloat)width
{
return self.frame.size.width;
}
-(void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
-(CGFloat)height
{
return self.frame.size.height;
}
-(void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
@end
@interface LabelView : UIView
+(LabelView *)getlabelView:(CGRect)frame text:(NSString *)str;
@end
@implementation LabelView
+(LabelView *)getlabelView:(CGRect)frame text:(NSString *)str
{
LabelView *view = [[LabelView alloc]initWithFrame:frame];
UILabel *label = [[UILabel alloc]init];
label.width = frame.size.width - 40;
label.height = frame.size.height - 10;
label.text = str;
label.x = 20;
label.y = 5;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[view insertSubview:label atIndex:0];
return view;
}
@end
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)LabelView *topView;
@property (nonatomic,strong) LabelView *toolbar;
@property (nonatomic,strong) UITableView *tab;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = YES;
self.topView = [LabelView getlabelView:CGRectMake(0, 0, 320, 50) text:@"houdi"];
CAGradientLayer *l = [CAGradientLayer layer];
l.frame = CGRectMake(0, 0, 320, 50);
l.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor, (id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
l.startPoint = CGPointMake(1.0f, 1.0f);
l.endPoint = CGPointMake(1.0f,0.0f);
_topView.layer.mask = l;
_topView.backgroundColor = [UIColor greenColor];
[self.view addSubview:_topView];
self.toolbar = [LabelView getlabelView:CGRectMake(0, 50, 320, 40) text:@"lixiaoyi"];
_toolbar.backgroundColor = [UIColor redColor];
[self.view addSubview:_toolbar];
self.tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tab.delegate = self;
_tab.dataSource = self;
[self.view addSubview:_tab];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//保证是自己的tabview
if (scrollView == self.tab) {
if (scrollView.contentOffset.x == 0) {
CGFloat y = scrollView.contentOffset.y;
// 这个是非常关键的变量,用于记录上一次滚动到哪个偏移位置
static CGFloat previousOffsetY = 0;
// 向上滚动
if (y>0) {
if (self.topView.bottomY <= 0) {
return;
}
// NSLog(@"self.topView.bottomY=====%f",self.topView.bottomY);
// 计算两次回调的滚动差:fabs(y - previousOffsetY)值
CGFloat bottomY = self.topView.bottomY - fabs(y-previousOffsetY);
// NSLog(@"bottomY=====%f",bottomY);
bottomY = bottomY >= 0? bottomY:0;
self.topView.bottomY = bottomY;
self.toolbar.y = self.topView.bottomY;
self.tab.frame = CGRectMake(0, self.toolbar.bottomY, scrollView.width, self.view.height- self.toolbar.bottomY);
previousOffsetY = y;
// 如果一直不松手滑动,重复向上向下滑动时,如果没有设置还原为0,则会出现马上到顶的情况。
if (previousOffsetY >= self.topView.height) {
previousOffsetY = 0;
}
}
// 向下滚动
else if (y<0)
{
if (self.topView.y >=0) {
return;
}
CGFloat bottomY = self.topView.bottomY + fabs(y);
bottomY = bottomY <= self.topView.height ? bottomY : self.topView.height;
self.topView.bottomY = bottomY;
self.toolbar.y = self.topView.bottomY;
self.tab.frame = CGRectMake(0,
self.toolbar.bottomY,
scrollView.width,
self.view.height - self.toolbar.bottomY);
}
}
}
}
//开始拖拽视图
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
{
NSLog(@"开始拖拽视图scrollViewWillBeginDragging");
}
//完成拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
NSLog(@"完成拖拽scrollViewDidEndDragging");
if (scrollView == self.tab) {
if (scrollView.contentOffset.x == 0) {
// CGFloat y = scrollView.contentOffset.y;
if (self.topView.bottomY <=self.topView.height/2) {
[UIView animateWithDuration:0.5 animations:^{
self.topView.bottomY = 0;
self.toolbar.y = 0;
self.tab.frame = CGRectMake(0,
self.toolbar.bottomY,
scrollView.width,
self.view.height - self.toolbar.bottomY);
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
self.topView.bottomY = 50;
self.toolbar.y = 50;
self.tab.frame = CGRectMake(0,
self.toolbar.bottomY,
scrollView.width,
self.view.height - self.toolbar.bottomY);
}];
}
}
}
}
//将开始降速时
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
{
// NSLog(@"");
if (scrollView == self.tab) {
if (scrollView.contentOffset.x == 0) {
CGFloat y = scrollView.contentOffset.y;
if (y > 0) {
NSLog(@"向上将开始降速时scrollViewWillBeginDecelerating");
}else if(y > 0)
{
NSLog(@"向下将开始降速时scrollViewWillBeginDecelerating");
}
}
}
}
//减速停止了时执行,手触摸时执行执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
if (scrollView == self.tab) {
if (scrollView.contentOffset.x == 0) {
// CGFloat y = scrollView.contentOffset.y;
if (self.topView.bottomY <=self.topView.height/2) {
[UIView animateWithDuration:0.5 animations:^{
self.topView.bottomY = 0;
self.toolbar.y = 0;
self.tab.frame = CGRectMake(0,
self.toolbar.bottomY,
scrollView.width,
self.view.height - self.toolbar.bottomY);
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
self.topView.bottomY = 50;
self.toolbar.y = 50;
self.tab.frame = CGRectMake(0,
self.toolbar.bottomY,
scrollView.width,
self.view.height - self.toolbar.bottomY);
}];
}
}
}
NSLog(@"减速停止了时执行,手触摸时执行执行scrollViewDidEndDecelerating");
}
//滚动动画停止时执行,代码改变时出发,也就是setContentOffset改变时
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
{
NSLog(@"setContentOffset改变时scrollViewDidEndScrollingAnimation");
}
//如果你不是完全滚动到滚轴视图的顶部,你可以轻点状态栏,那个可视的滚轴视图会一直滚动到顶部,那是默认行为,你可以通过该方法返回NO来关闭它
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewShouldScrollToTop");
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewDidScrollToTop");
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentify = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify];
}
cell.textLabel.text = @"22222222";
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}