iOS下拉放大圖片上推導(dǎo)航欄漸變顏色

最近想到實(shí)現(xiàn)tableview下拉實(shí)現(xiàn)headerView圖片根據(jù)偏移值放大,上推實(shí)現(xiàn)導(dǎo)航欄根據(jù)偏移值計(jì)算導(dǎo)航欄透明度.于是就試著寫了下,寫的不好還望多多包涵, 本人也是第二次寫文章...

代碼地址:https://github.com/kuyuxing/Gradient.git

效果:


viewDidLoad方法:

- (void)viewDidLoad {

[super viewDidLoad];

// 創(chuàng)建tableView

UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];

[self.view addSubview:tableView];

tableView.backgroundColor = [UIColor greenColor];

tableView.dataSource = self;

tableView.delegate = self;

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"hahaha" style:UIBarButtonItemStylePlain target:self action:nil];

self.navigationItem.leftBarButtonItem = item;

// 設(shè)置視圖不自動(dòng)下移

self.automaticallyAdjustsScrollViewInsets = NO;

// 設(shè)置導(dǎo)航欄為顏色

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

// 獲取負(fù)責(zé)導(dǎo)航欄顏色顯示的UIImageView,用于修改導(dǎo)航欄顏色

UIImageView *barImageView = self.navigationController.navigationBar.subviews.firstObject;

// 屬性進(jìn)行引用

self.barImageView = barImageView;

self.barImageView.alpha = 0;

}

scrollView滾動(dòng)的方法:

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

// 偏移量上移0~200之間根據(jù)偏移量計(jì)算透明度

if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < 200) {

self.barImageView.alpha = scrollView.contentOffset.y / 200;

// 超過200設(shè)置為不透明,防止快速滑動(dòng)導(dǎo)致透明度不正確問題

}else if(scrollView.contentOffset.y > 200){

self.barImageView.alpha = 1;

}else{

CGFloat contentOffsetY = ABS(scrollView.contentOffset.y);

// 下拉根據(jù)偏移設(shè)置圖片放大,并設(shè)置透明

self.imageView.frame = CGRectMake(-contentOffsetY, -contentOffsetY, [UIScreen mainScreen].bounds.size.width + 2 * contentOffsetY, contentOffsetY + 250);

self.barImageView.alpha = 0;

}

}

其他部分就直接貼代碼了

// tableView返回headerView的方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 250)];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:view.bounds];

self.imageView = imageView;

[view addSubview:imageView];

imageView.image = [UIImage imageNamed:@"02"];

return view;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 200;

}

// tableView數(shù)據(jù)源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;

}

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

return 200;

}

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

static NSString *ID = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

}

cell.textLabel.text = @"國服第一JS";

return cell;

}

好像又寫的有點(diǎn)啰嗦了,呵呵~??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容