iOS編程俺榆,tableView 下滑上面圖片跟著變大肝匆,毛玻璃

首先是幾個(gè)不同滑動(dòng)時(shí)候的效果:

剛進(jìn)入頁面時(shí)候的效果


Simulator Screen Shot 2016年3月12日 下午2.05.06.png

向下拉


Simulator Screen Shot 2016年3月12日 下午2.05.10.png

向上滑(毛玻璃效果是逐漸變的)


Simulator Screen Shot 2016年3月12日 下午2.05.22.png

滑倒固定位置粒蜈,在向上滑沒有變化


Simulator Screen Shot 2016年3月12日 下午2.05.24.png

下面是代碼部分

創(chuàng)建控件


#pragma mark ** 創(chuàng)建頭部的控件
- (void)createSubViews {
    
    // 含有頭部需要的數(shù)據(jù).
    ModelForSpecificContentsHearder *model = [[ModelForSpecificContentsHearder alloc] init];
    
    model = [self.arrayForHeader firstObject];
    
    // 背景的大圖片.
    self.imageViewForHeader = [[UIImageView alloc] initWithFrame:CGRectMake(0, - 48, WIDTH, HEIGHT * 2 / 7 + 48 * 2)];
    
    [self.imageViewForHeader sd_setImageWithURL:[NSURL URLWithString:model.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"temp"]];
    
    [self.view addSubview:self.imageViewForHeader];
    
    [_imageViewForHeader release];
    
    // 用戶頭像的圖片.
    self.imageViewForUser = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.imageViewForHeader.frame.size.height - 48 - 55, 45, 45)];
    
    [self.imageViewForUser sd_setImageWithURL:[NSURL URLWithString:[model.user objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"temp"]];
    
    self.imageViewForUser.layer.cornerRadius = self.imageViewForUser.frame.size.height / 2;
    
    self.imageViewForUser.layer.masksToBounds = YES;
    
    self.imageViewForUser.backgroundColor = [UIColor redColor];
    
    [self.imageViewForHeader addSubview:self.imageViewForUser];
    
    [_imageViewForUser release];
    
    // 標(biāo)題的文字.
    self.labelForTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.imageViewForUser.frame.origin.x + self.imageViewForUser.frame.size.width + 10, self.imageViewForUser.frame.origin.y, WIDTH - self.imageViewForUser.frame.origin.x - self.imageViewForUser.frame.size.width - 10, 25)];
    
    self.labelForTitle.text = model.name;
    
    self.labelForTitle.textColor = [UIColor whiteColor];
    
    self.labelForTitle.font = [UIFont systemFontOfSize:15];
    
    [self.imageViewForHeader addSubview:self.labelForTitle];
    
    [_labelForTitle release];
    
    // 日期
    self.labelForStartDate = [[UILabel alloc] initWithFrame:CGRectMake(self.labelForTitle.frame.origin.x, self.labelForTitle.frame.origin.y + self.labelForTitle.frame.size.height + 5, self.labelForTitle.frame.size.width, 20)];
    
    self.labelForStartDate.text = model.start_date;
    
    self.labelForStartDate.textColor = [UIColor whiteColor];
    
    self.labelForStartDate.font = [UIFont systemFontOfSize:13];
    
    [self.imageViewForHeader addSubview:self.labelForStartDate];
    
    [_labelForStartDate release];
    
    // 毛玻璃(首先是聲明了一個(gè)UIVisualEffectView 的對象(屬性))
    self.visualEffectView = [[UIVisualEffectView alloc] initWithFrame:self.imageViewForHeader.frame];
    
    // 設(shè)置毛玻璃的效果.
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:1];
    self.visualEffectView.effect = blur;
    
    // 透明度設(shè)置為0, 后面會(huì)讓它的透明度跟著tableView 的偏移量來改變, 用來實(shí)現(xiàn)漸變模糊的效果.
    self.visualEffectView.alpha = 0;
    
    [self.view addSubview:self.visualEffectView];
    
    [_visualEffectView release];
    
}


#pragma mark ** 創(chuàng)建TableView
- (void)createTableView {
    
    self.tableViewForSpecific = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    
    self.tableViewForSpecific.showsVerticalScrollIndicator = NO;
    
    self.tableViewForSpecific.backgroundColor = [UIColor clearColor];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 2 / 7 - 64)];
    
    self.tableViewForSpecific.tableHeaderView = header;
    
    self.tableViewForSpecific.dataSource = self;
    
    self.tableViewForSpecific.delegate = self;
    
    self.stringForTableView = @"poolForTableView";
    
    self.stringForHeader = @"poolForHeader";
    
    self.stringForPhoto = @"poolForPhoto";
    
    [self.tableViewForSpecific registerClass:[TableViewCellForSpecificContents class] forCellReuseIdentifier:self.stringForTableView];
    
    [self.tableViewForSpecific registerClass:[ViewForSpecificContentsHeader class] forHeaderFooterViewReuseIdentifier:self.stringForHeader];
    
    [self.tableViewForSpecific registerClass:[TableViewCellForPhoto class] forCellReuseIdentifier:self.stringForPhoto];
    
    [self.view addSubview:self.tableViewForSpecific];
    
    [_tableViewForSpecific release];
    
}

滑動(dòng)tableView 時(shí)改變frame 來實(shí)現(xiàn)想要的效果旗国,代碼:


#pragma mark ** 滑動(dòng)tableView 的時(shí)候執(zhí)行下面代碼
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGPoint point = scrollView.contentOffset;
    
    if (0 >= point.y && - 48 * 2 <= point.y) {
        
        self.imageViewForHeader.frame = CGRectMake(0, - 48 - point.y / 2, WIDTH, HEIGHT * 2 / 7 + 48 * 2);
        
        self.imageViewForUser.frame = CGRectMake(10, self.imageViewForHeader.frame.size.height - 55 - 48 - point.y / 2, 45, 45);
        
        self.viewForBackground.frame = self.imageViewForHeader.frame;
        
        self.labelForTitle.frame =  CGRectMake(self.imageViewForUser.frame.origin.x + self.imageViewForUser.frame.size.width + 10, self.imageViewForUser.frame.origin.y, WIDTH - self.imageViewForUser.frame.origin.x - self.imageViewForUser.frame.size.width - 10, 25);
        
        self.labelForStartDate.frame = CGRectMake(self.labelForTitle.frame.origin.x, self.labelForTitle.frame.origin.y + self.labelForTitle.frame.size.height + 5, self.labelForTitle.frame.size.width, 20);
        
        self.visualEffectView.alpha = 0;
        
    } else if (0 < point.y && point.y < HEIGHT * 2 / 7 - 64) {
        
        self.imageViewForHeader.frame = CGRectMake(0, - 48 - point.y, WIDTH, HEIGHT * 2 / 7 + 48 * 2);
        
        self.imageViewForUser.frame = CGRectMake(10, self.imageViewForHeader.frame.size.height - 55 - 48, 45, 45);
        
        self.viewForBackground.frame = self.imageViewForHeader.frame;
        
        self.labelForTitle.frame =  CGRectMake(self.imageViewForUser.frame.origin.x + self.imageViewForUser.frame.size.width + 10, self.imageViewForUser.frame.origin.y, WIDTH - self.imageViewForUser.frame.origin.x - self.imageViewForUser.frame.size.width - 10, 25);
        
        self.labelForStartDate.frame = CGRectMake(self.labelForTitle.frame.origin.x, self.labelForTitle.frame.origin.y + self.labelForTitle.frame.size.height + 5, self.labelForTitle.frame.size.width, 20);
        
        self.visualEffectView.alpha = point.y / (HEIGHT * 2 / 7 - 64) - 0.2;
        
    } else if (- 48 * 2 >= point.y) {
        
        scrollView.contentOffset = CGPointMake(0, - 48 * 2);
        
    } else if (point.y >= HEIGHT * 2 / 7 - 64) {
        
        self.imageViewForHeader.frame = CGRectMake(0, - 48 - (HEIGHT * 2 / 7 - 64), WIDTH, HEIGHT * 2 / 7 + 48 * 2);
        
        self.imageViewForUser.frame = CGRectMake(10, self.imageViewForHeader.frame.size.height - 55 - 48, 45, 45);
        
        self.viewForBackground.frame = self.imageViewForHeader.frame;
        
        self.labelForTitle.frame =  CGRectMake(self.imageViewForUser.frame.origin.x + self.imageViewForUser.frame.size.width + 10, self.imageViewForUser.frame.origin.y, WIDTH - self.imageViewForUser.frame.origin.x - self.imageViewForUser.frame.size.width - 10, 25);
        
        self.labelForStartDate.frame = CGRectMake(self.labelForTitle.frame.origin.x, self.labelForTitle.frame.origin.y + self.labelForTitle.frame.size.height + 5, self.labelForTitle.frame.size.width, 20);
        
        self.visualEffectView.alpha =  0.8;
        
    }
    
}

這樣我們就能實(shí)現(xiàn)上圖的效果了枯怖。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市能曾,隨后出現(xiàn)的幾起案子度硝,更是在濱河造成了極大的恐慌,老刑警劉巖寿冕,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蕊程,死亡現(xiàn)場離奇詭異,居然都是意外死亡驼唱,警方通過查閱死者的電腦和手機(jī)存捺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來曙蒸,“玉大人捌治,你說我怎么就攤上這事∨撸” “怎么了肖油?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長臂港。 經(jīng)常有香客問我森枪,道長,這世上最難降的妖魔是什么审孽? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任县袱,我火速辦了婚禮,結(jié)果婚禮上佑力,老公的妹妹穿的比我還像新娘式散。我一直安慰自己,他們只是感情好打颤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布暴拄。 她就那樣靜靜地躺著,像睡著了一般编饺。 火紅的嫁衣襯著肌膚如雪乖篷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天透且,我揣著相機(jī)與錄音撕蔼,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛鲸沮,可吹牛的內(nèi)容都是我干的畅形。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼诉探,長吁一口氣:“原來是場噩夢啊……” “哼日熬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起肾胯,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤竖席,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后敬肚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體毕荐,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年艳馒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了憎亚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡弄慰,死狀恐怖第美,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情陆爽,我是刑警寧澤什往,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站慌闭,受9級特大地震影響别威,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜驴剔,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一省古、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧丧失,春花似錦豺妓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至炒事,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蔫慧,已是汗流浹背挠乳。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人睡扬。 一個(gè)月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓盟蚣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親卖怜。 傳聞我的和親對象是個(gè)殘疾皇子屎开,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容