最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)沒(méi)有做出來(lái)之前都覺(jué)得蠻難的,做出來(lái)之后就覺(jué)得So Easy 大家都有這樣的感觸吧

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

OC版本

創(chuàng)建一個(gè)類(lèi) 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的代理方法了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS開(kāi)發(fā) widget構(gòu)建詳解及實(shí)現(xiàn)代碼

    iOS開(kāi)發(fā) widget構(gòu)建詳解及實(shí)現(xiàn)代碼

    這篇文章主要介紹了iOS開(kāi)發(fā) widget構(gòu)建詳解的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下
    2016-11-11
  • iOS NSURLProtocol的具體使用方法詳解

    iOS NSURLProtocol的具體使用方法詳解

    本篇文章主要介紹了iOS NSURLProtocol的具體使用方法詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • ios中圖像進(jìn)行壓縮方法匯總

    ios中圖像進(jìn)行壓縮方法匯總

    在Iphone上有兩種讀取圖片數(shù)據(jù)的簡(jiǎn)單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數(shù)需要兩個(gè)參數(shù):圖片的引用和壓縮系數(shù).而UIImagePNGRepresentation只需要圖片引用作為參數(shù).
    2015-05-05
  • iOS實(shí)現(xiàn)簡(jiǎn)易鐘表

    iOS實(shí)現(xiàn)簡(jiǎn)易鐘表

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)易鐘表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • IOS 線程死鎖詳細(xì)介紹

    IOS 線程死鎖詳細(xì)介紹

    這篇文章主要介紹了IOS 線程死鎖詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • iOS實(shí)現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例

    iOS實(shí)現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例

    本篇文章主要介紹了iOS實(shí)現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • iOS runtime forwardInvocation詳解及整理

    iOS runtime forwardInvocation詳解及整理

    這篇文章主要介紹了 iOS runtime forwardInvocation詳解及整理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • iOS頁(yè)面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)

    iOS頁(yè)面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)

    本文主要介紹了iOS頁(yè)面跳轉(zhuǎn)的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • iOS如何優(yōu)雅地消除應(yīng)用角標(biāo)詳解

    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)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • iOS開(kāi)發(fā)之導(dǎo)航欄各種右滑返回失效的解決方法匯總

    iOS開(kāi)發(fā)之導(dǎo)航欄各種右滑返回失效的解決方法匯總

    這篇文章主要給大家總結(jié)介紹了關(guān)于iOS開(kāi)發(fā)教程之導(dǎo)航欄各種右滑返回失效的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08

最新評(píng)論

伊川县| 尼玛县| 武穴市| 广平县| 大城县| 元谋县| 安宁市| 乌拉特前旗| 绥宁县| 新民市| 玛沁县| 大同市| 镇平县| 凤冈县| 永嘉县| 台山市| 刚察县| 井冈山市| 家居| 金门县| 封丘县| 黄陵县| 迁安市| 牡丹江市| 福清市| 江山市| 进贤县| 兰考县| 弥勒县| 乐业县| 奉节县| 澄迈县| 普陀区| 会理县| 鱼台县| 桦南县| 璧山县| 贺兰县| 稻城县| 湖北省| 怀宁县|