iOS使用UICollectionView實現(xiàn)列表頭部拉伸效果
本文實例為大家分享了iOS實現(xiàn)列表下拉放大效果展示的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖

突然發(fā)現(xiàn)沒有做出來之前都覺得蠻難的,做出來之后就覺得So Easy 大家都有這樣的感觸吧
做這個就重寫 UICollectionViewFlowLayout 的幾個方法就可以
OC版本
創(chuàng)建一個類 CustomCollectionViewFlowLayout 繼承 UICollectionViewFlowLayout
//
// CustomCollectionViewFlowLayout.m
//
//
// Created by GongHui_YJ on 16/8/4.
// Copyright © 2016年 Yangjian. All rights reserved.
//
#import "CustomCollectionViewFlowLayout.h"
@implementation CustomCollectionViewFlowLayout
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
UICollectionView *collectionView = [self collectionView];
UIEdgeInsets insets = [collectionView contentInset];
CGPoint offset = [collectionView contentOffset];
CGFloat minY = -insets.top;
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
if (offset.y < minY) {
CGSize headerSize = [self headerReferenceSize];
CGFloat deltaY = fabsf(offset.y - minY);
for (UICollectionViewLayoutAttributes *attrs in attributes) {
if ([attrs representedElementKind] == UICollectionElementKindSectionHeader) {
CGRect headerRect = [attrs frame];
headerRect.size.height = MAX(minY, headerSize.height + deltaY);
headerRect.origin.y = headerRect.origin.y - deltaY;
[attrs setFrame:headerRect];
break;
}
}
}
return attributes;
}
@end
在控制器中使用 先導(dǎo)入頭文件
// 創(chuàng)建collectionView CustomCollectionViewFlowLayout *flowLayout=[[CustomCollectionViewFlowLayout alloc] init]; [flowLayout setSectionInset:UIEdgeInsetsMake(0, 0, 10, 0)]; [flowLayout setItemSize:CGSizeMake(kScreenWidth / collectionCellW, kScreenWidth / collectionCellW)]; [flowLayout setHeaderReferenceSize:CGSizeMake(kScreenWidth, userInfoImageViewH)]; [flowLayout setFooterReferenceSize:CGSizeMake(kScreenWidth, 83)]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; [flowLayout setMinimumInteritemSpacing:0.0]; [flowLayout setMinimumLineSpacing:0.0]; self.homeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)collectionViewLayout:flowLayout]; self.homeCollectionView.backgroundColor = kViewBackgroundColor; self.homeCollectionView.alwaysBounceVertical = YES; self.homeCollectionView.showsVerticalScrollIndicator = NO; //設(shè)置代理 self.homeCollectionView.delegate = self; self.homeCollectionView.dataSource = self; [self.view addSubview:self.homeCollectionView]; // 注冊表頭 [self.homeCollectionView registerClass:[YJHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHeaderView]; // 注冊表尾 [self.homeCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionFooterView];
Swift版
喜歡swift 不需要導(dǎo)入頭文件那么麻煩
//
// CustomCollectionViewFlowLayout.swift
//
//
// Created by GongHui_YJ on 16/8/4.
// Copyright © 2016年YangJian. All rights reserved.
//
import UIKit
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let collectionView = self.collectionView
let insets = collectionView?.contentInset
let offset = collectionView?.contentOffset
let minY = -((insets?.top)!)
let attributesArray = super.layoutAttributesForElementsInRect(rect)
if offset!.y < minY {
let headerSize = self.headerReferenceSize
let deltaY = CGFloat(fabsf(Float((offset?.y)! - CGFloat(minY))))
for attrs:UICollectionViewLayoutAttributes in attributesArray! {
if attrs.representedElementKind == UICollectionElementKindSectionHeader {
var headerRect = attrs.frame
headerRect.size.height = max(minY, headerSize.height + deltaY)
headerRect.origin.y = headerRect.origin.y - deltaY
attrs.frame = headerRect
break
}
}
}
return attributesArray
}
}
在控制器 viewDidLoad方法實現(xiàn)
let customFlowLayout = CustomCollectionViewFlowLayout() customFlowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 203) customFlowLayout.footerReferenceSize = CGSizeMake(kScreenWidth, 83) customFlowLayout.minimumInteritemSpacing = 0 customFlowLayout.minimumLineSpacing = 0 customFlowLayout.itemSize = CGSizeMake(kScreenWidth / 3.000006, kScreenWidth / 3.00006) customFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 10, 0) self.homeCollectionView.setCollectionViewLayout(customFlowLayout, animated: true) self.homeCollectionView.backgroundColor = kViewBackgroundColor self.homeCollectionView.alwaysBounceVertical = true let nib = UINib(nibName: "CommonCollectionViewCell", bundle: nil) self.homeCollectionView.registerNib(nib, forCellWithReuseIdentifier: cellId) // 注冊表頭表尾 let headerNib = UINib(nibName: "HeaderCollectionReusableView", bundle: nil) self.homeCollectionView.registerNib(headerNib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: collectionHeaderId) self.homeCollectionView.registerClass(UICollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: collectionFooterId)
注:不要實現(xiàn)UICollectionViewDelegateFlowLayout的代理方法了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā) widget構(gòu)建詳解及實現(xiàn)代碼
這篇文章主要介紹了iOS開發(fā) widget構(gòu)建詳解的相關(guān)資料,并附實例代碼,需要的朋友可以參考下2016-11-11
iOS實現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例
本篇文章主要介紹了iOS實現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
iOS runtime forwardInvocation詳解及整理
這篇文章主要介紹了 iOS runtime forwardInvocation詳解及整理的相關(guān)資料,需要的朋友可以參考下2017-02-02
iOS頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)
本文主要介紹了iOS頁面跳轉(zhuǎn)的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)詳解
關(guān)于應(yīng)用角標(biāo)相信大家應(yīng)該都有所了解吧,這篇文章主要給大家介紹了關(guān)于iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
iOS開發(fā)之導(dǎo)航欄各種右滑返回失效的解決方法匯總
這篇文章主要給大家總結(jié)介紹了關(guān)于iOS開發(fā)教程之導(dǎo)航欄各種右滑返回失效的解決方法,文中通過示例代碼介紹的非常詳細(xì),對各位iOS具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

