欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS使用UICollectionView實(shí)現(xiàn)列表頭部拉伸效果

 更新時(shí)間:2018年05月08日 15:01:14   作者:Break__Self  
這篇文章主要介紹了iOS使用UICollectionView實(shí)現(xiàn)列表頭部拉伸效果,OC和Swift兩個(gè)版本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)列表下拉放大效果展示的具體代碼,供大家參考,具體內(nèi)容如下

先看效果圖

突然發(fā)現(xiàn)沒有做出來之前都覺得蠻難的,做出來之后就覺得So Easy 大家都有這樣的感觸吧

做這個(gè)就重寫 UICollectionViewFlowLayout 的幾個(gè)方法就可以

OC版本

創(chuàng)建一個(gè)類 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];

  // 注冊(cè)表頭
  [self.homeCollectionView registerClass:[YJHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHeaderView];

  // 注冊(cè)表尾
  [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)

  // 注冊(cè)表頭表尾
  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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IOS判斷字符串是不是純數(shù)字的方法總結(jié)

    IOS判斷字符串是不是純數(shù)字的方法總結(jié)

    這篇文章給大家分享了在IOS中判斷字符串是不是純數(shù)字的三種方法,大家可以根據(jù)自己的需求來選擇對(duì)應(yīng)的方法實(shí)現(xiàn),有需要的朋友們可以參考借鑒,下面來看看。
    2016-09-09
  • iOS掃描二維碼實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭

    iOS掃描二維碼實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭

    這篇文章主要為大家詳細(xì)介紹了iOS掃描二維碼實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iOS中l(wèi)ebel特殊字符的自動(dòng)換行問題解決

    iOS中l(wèi)ebel特殊字符的自動(dòng)換行問題解決

    這篇文章主要給大家介紹了關(guān)于iOS中l(wèi)ebel特殊字符的實(shí)現(xiàn)不自動(dòng)換行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • IOS 開發(fā)之UISearchBar 詳解及實(shí)例

    IOS 開發(fā)之UISearchBar 詳解及實(shí)例

    這篇文章主要介紹了IOS 開發(fā)之UISearchBar 詳解及實(shí)例的相關(guān)資料,主要介紹 IOS UISearchBar的使用,附有實(shí)例代碼,需要的朋友可以參考下
    2016-12-12
  • iOS禁止所有輸入法表情的方法

    iOS禁止所有輸入法表情的方法

    這篇文章主要為大家詳細(xì)介紹了iOS禁止所有輸入法表情的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例

    iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例

    本篇文章主要介紹了iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • 如何利用iCloud Drive同步Xcode配置詳解

    如何利用iCloud Drive同步Xcode配置詳解

    這篇文章主要給大家介紹了關(guān)于如何利用iCloud Drive同步Xcode配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS獲取當(dāng)前連接的WiFi以及IP地址

    iOS獲取當(dāng)前連接的WiFi以及IP地址

    本文主要介紹了iOS獲取當(dāng)前連接的WiFi以及IP地址方法的核心代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-03-03
  • iOS中WKWebView的一些特殊使用總結(jié)

    iOS中WKWebView的一些特殊使用總結(jié)

    這篇文章主要給大家介紹了關(guān)于iOS中WKWebView的一些特殊使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解

    IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解

    這篇文章主要介紹了IOS 開發(fā)之PickerView自定義視圖的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下
    2017-08-08

最新評(píng)論