類似於QQ個人主頁,NavigationBar透明漸變

#import "TableCell.h"


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


@interface FMHomeViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (strong, nonatomic) IBOutlet UITableView *mainTableView;


@property (strong, nonatomicUIImageView *topImgV;


@property (nonatomic, assign) CGFloat alphaMemory;


@end


@implementation FMHomeViewController

static NSString *const Identfier=@"TableCellID";



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

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

    headerView.backgroundColor=[UIColor clearColor];

    self.mainTableView.tableHeaderView=headerView;

    

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

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

    [self.view insertSubview:self.topImgV belowSubview:self.mainTableView];

    self.topImgV.contentMode=UIViewContentModeScaleAspectFill;

    self.topImgV.clipsToBounds=YES;

    

    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:37.0/255 green:180.0/255 blue:237.0/255 alpha:1]];

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

    

    

    

}


-(void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:_alphaMemory];

    

    if (_alphaMemory == 0) {

        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    }

    else {

        self.navigationController.navigationBar.tintColor = [UIColor blackColor];

    }


}


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

{

    return 20;

}


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

{

    return 60;

}


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

{

    

    TableCell *cell=[tableView dequeueReusableCellWithIdentifier:Identfier forIndexPath: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 = [UIColor blackColor];

            

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

            

            [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:_alphaMemory];

            

        }else if (offsetY>300){

            

            _alphaMemory = 1;

            [[[self.navigationController.navigationBar subviews] 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;

    }

  

    


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章