本文通過實(shí)例代碼給大家介紹了iOS實(shí)現(xiàn)簡單的頭部縮放功能。實(shí)現(xiàn)思路有頭部視圖,滾動視圖,控制頭部動畫等多個示例代碼塊,大家可以參考下本文。
簡單實(shí)現(xiàn)并集成一個頭部縮放的功能,適用于UIScrollView以及其子類。
頭部伴隨模糊效果放大縮小,并在一定位置時懸停充當(dāng)導(dǎo)航欄。這里提供實(shí)現(xiàn)思路,如有符合可直接使用。
效果如下圖。
實(shí)現(xiàn):
首先分解為兩部分,一部分為頭部視圖,一部分為滾動視圖。頭部視圖負(fù)責(zé)展示,滾動視圖負(fù)責(zé)控制頭部視圖如何展示,比如放大和縮小。
一:頭部視圖
頭部視圖拆解為負(fù)責(zé)展示圖片的UIImageView,負(fù)責(zé)模糊效果的UIVisualEffectView,負(fù)責(zé)標(biāo)題顯示的UILabel,以及返回等功能按鈕的UIButton。
進(jìn)一步分析,模糊效果的視圖應(yīng)該和展示圖片的視圖做同樣的處理,同樣的縮放,為了更好的控制將其包裝到一containView中。跟據(jù)滾動的位置改變containView的大小,模糊視圖根據(jù)滾動的位置改變模糊的程度。標(biāo)題視圖在滾動視圖到達(dá)一定位置時出現(xiàn)并停在那里。這里利用UIImageView的特性,改變它的contentMode為UIViewContentModeScaleAspectFill,這樣只用簡單的改變圖片視圖的高度時就能營造放大縮小的效果了。
UIImageView部分代碼
_blurImageView = [[UIImageView alloc] init];_blurImageView.clipsToBounds = YES;_blurImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;_blurImageView.contentMode = UIViewContentModeScaleAspectFill;[self addSubview:_blurImageView];
UIVisualEffectView部分代碼
UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; _imageEffectView = [[UIVisualEffectView alloc]initWithEffect:beffect]; _imageEffectView.alpha = 0;_imageEffectView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self addSubview:_imageEffectView];
二:滾動視圖
滾動視圖需要做的就是設(shè)置 contentInset ,讓出一部分空間給頭部視圖。這里如果將頭部視圖直接加到滾動視圖上,無法做到頭部視圖最后懸停在一定位置,因此直接加到和滾動視圖同級就行。
示例代碼
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kWindowWidth, kWindowHeight)]; self.webView.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.webView]; NSURL * url = [NSURL URLWithString:@"https://yongliangp.github.io/"]; NSMutableURLRequest *re = [NSMutableURLRequest requestWithURL:url]; [self.webView loadRequest:re]; //初始化header self.headerView.headerImage = [UIImage imageNamed:@"saber.jpeg"]; self.headerView.tittle = @"哈哈是個demo"; self.headerView.isShowLeftButton = YES; self.headerView.isShowRightButton = YES; __weak typeof(self) weakSelf = self; self.headerView.leftClickBlock = ^(UIButton *btn){ [weakSelf.navigationController popViewControllerAnimated:YES]; }; self.headerView.rightClickBlock = ^(UIButton *btn){ NSLog(@"點(diǎn)擊了分享"); }; [self.webView.scrollView handleSpringHeadView:self.headerView];
三:控制頭部動畫
和其他的滾動視圖做動畫一樣,實(shí)現(xiàn)滾動視圖的代理方法scrollViewDidScroll,獲取偏移量,然后根據(jù)一定的規(guī)則做動畫,這里為了解耦,也為了復(fù)用,使用了在scrollView的分類中監(jiān)聽scrollView的contentOffset方法去實(shí)現(xiàn)動畫控制。
首先確定兩個臨界點(diǎn): 視圖的初始高度 懸停的高度。
示例代碼(簡單控制)
- (void)yl_scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat offsetY = scrollView.contentOffset.y; if (offsetY>=-kNavHeight) { offsetY=-kNavHeight; if (self.headerView.frame.size.height!=kNavHeight) { self.headerView.frame = CGRectMake(0, 0, self.headerView.bounds.size.width, kNavHeight); [UIView animateWithDuration:0.25 animations:^{ self.titleLabel.frame = CGRectMake(35, 20, self.bounds.size.width-35*2, 44); self.titleLabel.alpha = 1; }]; } }else { self.headerView.frame = CGRectMake(0, 0, self.headerView.bounds.size.width, -offsetY); if (self.titleLabel.alpha!=0) { [UIView animateWithDuration:0.25 animations:^{ self.titleLabel.frame = CGRectMake(35, 40, self.bounds.size.width-35*2, 44); self.titleLabel.alpha = 0; }]; } } CGFloat alpha ; if (self.headerView.frame.size.height>=kWindowWidth/2) { alpha = 0; }else { alpha = 1-((self.headerView.frame.size.height-kNavHeight)/(kWindowWidth/2-kNavHeight)); } if (alpha>=0&α<=1) { self.headerEffectView.alpha = alpha; }}
最重要的,記得在控制器dealloc時移除監(jiān)聽者
最重要的,記得在控制器dealloc時移除監(jiān)聽者
最重要的,記得在控制器dealloc時移除監(jiān)聽者
或者你有更好的方式移除監(jiān)聽者請告訴我。
照例放Demo,僅供參考,如有問題請留言
Demo地址:
https://github.com/yongliangP/YLSpringHeader 用心良苦請 star
總結(jié)
以上所述是小編給大家介紹的iOS實(shí)現(xiàn)簡單的頭部縮放功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選