类似于QQ个人主页,NavigationBar透明渐变

来源:互联网 发布:淘宝收货时间怎么设置 编辑:程序博客网 时间:2024/05/01 18:42

#import "TableCell.h"


#define SCR_WIDTH   [UIScreen mainScreen].bounds.size.width


@interface FMHomeViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (strong,nonatomic) IBOutletUITableView *mainTableView;


@property (strong,nonatomicUIImageView *topImgV;


@property (nonatomic,assign) CGFloat alphaMemory;


@end


@implementation FMHomeViewController

static NSString *const Identfier=@"TableCellID";



- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    UIView *headerView=[[UIViewalloc] initWithFrame:CGRectMake(0,0, SCR_WIDTH,300)];

    headerView.backgroundColor=[UIColorclearColor];

    self.mainTableView.tableHeaderView=headerView;

    

    self.topImgV=[[UIImageViewalloc] initWithFrame:CGRectMake(0,0, SCR_WIDTH,300)];

    self.topImgV.image=[UIImageimageNamed:@"meinv"];

    [self.viewinsertSubview:self.topImgVbelowSubview:self.mainTableView];

    self.topImgV.contentMode=UIViewContentModeScaleAspectFill;

    self.topImgV.clipsToBounds=YES;

    

    [self.navigationController.navigationBarsetBarTintColor:[UIColorcolorWithRed:37.0/255green:180.0/255blue:237.0/255alpha:1]];

   // self.navigationController.navigationBar.hidden=YES;

    

    

    

}


-(void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

    

    [[[self.navigationController.navigationBarsubviews] objectAtIndex:0]setAlpha:_alphaMemory];

    

    if (_alphaMemory ==0) {

        self.navigationController.navigationBar.tintColor = [UIColorwhiteColor];

    }

    else {

        self.navigationController.navigationBar.tintColor = [UIColorblackColor];

    }


}


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

{

    return20;

}


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

{

    return60;

}


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

{

    

    TableCell *cell=[tableViewdequeueReusableCellWithIdentifier:IdentfierforIndexPath:indexPath];

    return cell;

    

}


 //MARK: tableView delegate

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

    

    NSLog(@"%.f",_mainTableView.contentOffset.y);

    

    CGFloat offsetY = scrollView.contentOffset.y;

    

    CGRect frame=self.topImgV.frame;

   

    if (offsetY>=0) {

        

        frame.origin.y=-offsetY;

        //重新赋值 frame

        self.topImgV.frame=frame;

        if (offsetY<=300) {

            self.navigationController.navigationBar.tintColor = [UIColorblackColor];

            

            _alphaMemory = offsetY/(300) >=1 ? 1 : offsetY/(300);

            

            [[[self.navigationController.navigationBarsubviews] objectAtIndex:0]setAlpha:_alphaMemory];

            

        }elseif (offsetY>300){

            

            _alphaMemory =1;

            [[[self.navigationController.navigationBarsubviews] objectAtIndex:0]setAlpha:1];

            

        }

        

       

    }else{

        

        _topImgV.transformCGAffineTransformMakeScale(1 + offsetY/(-300),1 + offsetY/(-300));

        CGRect frame1=self.topImgV.frame;

        frame1.origin.y =0;

        self.topImgV.frame=frame1;

    }

  

    


}

0 0