iOS使用UICollectionView實(shí)現(xiàn)列表頭部拉伸效果
本文實(shí)例為大家分享了iOS實(shí)現(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方法實(shí)現(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)
注:不要實(shí)現(xiàn)UICollectionViewDelegateFlowLayout的代理方法了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS掃描二維碼實(shí)現(xiàn)手勢拉近拉遠(yuǎn)鏡頭
這篇文章主要為大家詳細(xì)介紹了iOS掃描二維碼實(shí)現(xiàn)手勢拉近拉遠(yuǎn)鏡頭,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
IOS 開發(fā)之UISearchBar 詳解及實(shí)例
這篇文章主要介紹了IOS 開發(fā)之UISearchBar 詳解及實(shí)例的相關(guān)資料,主要介紹 IOS UISearchBar的使用,附有實(shí)例代碼,需要的朋友可以參考下2016-12-12
iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例
本篇文章主要介紹了iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解
這篇文章主要介紹了IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分知識,需要的朋友可以參考下2017-08-08

