实现类似网易邮箱的顶部工具栏的悬停效果

来源:互联网 发布:练钢琴软件 编辑:程序博客网 时间:2024/04/27 22:45

#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

{

    returnself.frame.origin.x;

}

-(void)setX:(CGFloat)x

{

    CGRect frame  =self.frame;

    frame.origin.x = x;

    self.frame = frame;

}

-(CGFloat)y

{

    

    returnself.frame.origin.y;

}

-(void)setY:(CGFloat)y

{

    CGRect frame =self.frame;

    frame.origin.y = y;

    self.frame  = frame;

    

}

-(CGFloat)bottomY

{

    returnself.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

{

    returnself.frame.size.width;

}

-(void)setWidth:(CGFloat)width

{

    CGRect frame =self.frame;

    frame.size.width = width;

    self.frame = frame;

    

}

-(CGFloat)height

{

    returnself.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 = [[LabelViewalloc]initWithFrame:frame];

    UILabel *label = [[UILabelalloc]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 = [UIColorclearColor];

    [view insertSubview:labelatIndex: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 {

    [superviewDidLoad];

   

    self.automaticallyAdjustsScrollViewInsets =YES;

    self.topView = [LabelViewgetlabelView:CGRectMake(0,0, 320,50) text:@"houdi"];

    CAGradientLayer *l = [CAGradientLayerlayer];

    l.frame =CGRectMake(0,0, 320,50);

    l.colors = [NSArrayarrayWithObjects:(id)[UIColorclearColor].CGColor, (id)[UIColorwhiteColor].CGColor, (id)[UIColorclearColor].CGColor,nil];

    l.startPoint =CGPointMake(1.0f,1.0f);

    l.endPoint =CGPointMake(1.0f,0.0f);

    _topView.layer.mask = l;

    _topView.backgroundColor = [UIColorgreenColor];

     [self.viewaddSubview:_topView];

    self.toolbar =  [LabelViewgetlabelView:CGRectMake(0,50, 320,40) text:@"lixiaoyi"];

    _toolbar.backgroundColor = [UIColorredColor];

    [self.viewaddSubview:_toolbar];

    

    self.tab = [[UITableViewalloc]initWithFrame:CGRectMake(0,90, self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStylePlain];

    _tab.delegate =self;

    _tab.dataSource =self;

    [self.viewaddSubview:_tab];

    

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //保证是自己的tabview

    if (scrollView ==self.tab) {

        if (scrollView.contentOffset.x ==0) {

            CGFloat y = scrollView.contentOffset.y;

            

// 这个是非常关键的变量,用于记录上一次滚动到哪个偏移位置

            staticCGFloat 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;

                }

                

            }

 // 向下滚动


            elseif (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) {

                [UIViewanimateWithDuration:0.5animations:^{

                    

                    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{

                [UIViewanimateWithDuration:0.5animations:^{

                    

                    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");

            }elseif(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) {

                [UIViewanimateWithDuration:0.5animations:^{

                    

                    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{

                [UIViewanimateWithDuration:0.5animations:^{

                    

                    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");

    returnYES;

}


- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;

{

    NSLog(@"scrollViewDidScrollToTop");

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return30;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return50;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    staticNSString *cellIdentify = @"cell";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentify];

    if (!cell) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIdentify];

    }

    cell.textLabel.text =@"22222222";

    return cell;

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

0 0
原创粉丝点击