1. 程式人生 > >tableView:實現導航欄漸變和頂部禁止彈簧效果

tableView:實現導航欄漸變和頂部禁止彈簧效果

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, self.view.bounds.size.width, self.view.bounds.size.height+20)];
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:tableView];
    
    RACSignal *signal = [tableView rac_valuesForKeyPath:@"contentOffset" observer:self];
    RAC(self.navigationController.navigationBar,alpha) = [signal map:^id(NSValue * x) {
        CGFloat offset = tableView.contentOffset.y;
        if (offset<=-20) {
            tableView.bounces = NO;
        }
        else{
            tableView.bounces = YES;
        }
        CGFloat delta = offset / 20.f + 1.f;
        delta = MAX(0, delta);
        CGFloat ofy = 200-CGRectGetMaxY(self.navigationController.navigationBar.frame);
        CGFloat alp = (offset+20)/ofy;
        return [NSNumber numberWithFloat:MIN(1, alp)];
    }];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = @"hahahahahahahh";
    return cell;
}