現(xiàn)在越來越多的APP中存在下拉放大圖片的效果,今天貢獻(xiàn)一下我的實(shí)現(xiàn)這種方法的原理造成,和我遇到的坑显熏。??效果圖
GitHub地址:https://github.com/cf0717/DropDownAmplification
介紹一下我實(shí)現(xiàn)的原理
一進(jìn)入界面的時候隱藏掉導(dǎo)航欄,因?yàn)椴僮飨到y(tǒng)的導(dǎo)航欄較麻煩晒屎,不如自己寫一個導(dǎo)航欄來的簡單,在界面要消失的時候在把導(dǎo)航欄顯示出來即可喘蟆,(也可以自己寫一個pop動畫)
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
self.navigationController.navigationBar.hidden = NO;
}
使用懶加載的方法把假的導(dǎo)航欄,圖片和表格創(chuàng)建出來
這里有幾點(diǎn)注意事項(xiàng):
1.圖片不能添加到UITabview的HeaderView中鼓鲁,因?yàn)槿绻砑拥紿eaderView中蕴轨,那圖片就是表格的一部分了,會跟隨表格的移動和移動骇吭。
2.表格要設(shè)置contentInset來顯示出圖片橙弱,不然會有覆蓋問題
3.圖片的填充模式很重要,一定要是UIViewContentModeScaleAspectFill這樣可以節(jié)省很多代碼
4.圖片和假導(dǎo)航欄要設(shè)置clipsToBounds為YES,不然會有Bug
//lazy
-(UITableView *)tableview
{
if (!_tableview) {
_tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
CGFloat headImvH = kScreenW / 320 * 300;
//這句很重要
_tableview.contentInset = UIEdgeInsetsMake(headImvH-20, 0, 0, 0);
_tableview.dataSource = self;
_tableview.delegate = self;
}
return _tableview;
}
-(UIImageView *)headImv
{
if (!_headImv) {
_headImv = [[UIImageView alloc]init];
CGFloat headImvH = kScreenW / 320 * 300;
_headImv.frame = CGRectMake(0, 0, kScreenW, headImvH);
_headImv.image = [UIImage imageNamed:@"head"];
//一定記住模式
_headImv.contentMode = UIViewContentModeScaleAspectFill;
_headImv.clipsToBounds = YES;
}
return _headImv;
}
-(UIView *)navView
{
if (!_navView) {
_navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 64)];
_navView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.8];
_navView.clipsToBounds = YES;
_titLab = [[UILabel alloc]init];
_titLab.centerX = kScreenW * 0.5;
_titLab.bounds = CGRectMake(0, 0, 150, 44);
_titLab.textAlignment = NSTextAlignmentCenter;
_titLab.font = [UIFont systemFontOfSize:12];
_titLab.textColor = [UIColor blueColor];
_titLab.numberOfLines = 0;
_titLab.text = @"CF\n明天休息了";
[_navView addSubview:_titLab];
}
return _navView;
}
??這些代碼很好理解棘脐,不做解釋
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//添加順序不能改變
[self.view addSubview:self.tableview];
[self.view addSubview:self.headImv];
[self.view addSubview:self.navView];
[self setupNavView];
}
#pragma mark - 設(shè)置導(dǎo)航欄
-(void)setupNavView
{
UIButton *leftNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftNavBtn.frame = CGRectMake(0, 20, 64, 44);
[leftNavBtn setTitle:@"返回" forState:UIControlStateNormal];
[leftNavBtn addTarget:self action:@selector(leftNavClike) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftNavBtn];
UIButton *rightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightNavBtn.frame = CGRectMake(kScreenW-64, 20, 64, 44);
[rightNavBtn setTitle:@"更多" forState:UIControlStateNormal];
[rightNavBtn addTarget:self action:@selector(rightNavClike) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightNavBtn];
}
-(void)leftNavClike
{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)rightNavClike
{
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"測試數(shù)據(jù)——%td",indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"測試數(shù)據(jù)——%td",indexPath.row];
return cell;
}
下面為核心代碼
通過表格的代理方法來改變整體的Frame
這里我發(fā)現(xiàn)一個問題斜筐,如果表格滑動的特別快,那么這個代理方法會存在來不及執(zhí)行的效果蛀缝,或者說偏移量不是線性改變的顷链,從而導(dǎo)致根據(jù)偏移量來計(jì)算的Frame出現(xiàn)問題,所以有些控件建議在設(shè)置Frame的時候屈梁,建議用一個參照來改變(我這里假導(dǎo)航欄里面的文字用偏移出現(xiàn)了問題嗤练,所以改用假導(dǎo)航欄的透明度)
設(shè)置頭部圖片的Frame要注意,origin值不要改變一旦改變不好調(diào)整在讶。
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//拿到偏移量
CGFloat offsetY = scrollView.contentOffset.y;
NSInteger headImvH = kScreenW / 320 * 300 ;
CGFloat offset = headImvH + offsetY;//計(jì)算偏移量
//設(shè)置導(dǎo)航欄
self.navView.alpha = (offset / 250);
if (self.navView.alpha >=1) {
self.navView.alpha = 1;
}
//設(shè)置標(biāo)題文字 設(shè)置距離
//alpha 0-->1 y 64-->20
self.titLab.y = 64 - 44 * self.navView.alpha;
//設(shè)置頭部圖片大小
self.headImv.frame = CGRectMake(0, 0, kScreenW, headImvH-offset);
}
附上結(jié)構(gòu)圖