iOS CollectionView the completed guide(2th) chapter 04 Example Dimensions
来源:互联网 发布:淘宝刷单被发现 编辑:程序博客网 时间:2024/05/16 02:46
Today, I learned the iOS CollectionView the completed guide(2th) chapter 04
there is one demo project Called Dimensions. this project is for demonstrating how to add layoutAttributes
for customizing the collectionView. Such as the CollectionViewCell's contentView has a imageView. how to set the imageView's contentMode for different value(UIViewContentModeScaleAspectFill, UIViewContentModeScaleAspectFit) .the solution is below.
1. define a new enum for ContentMode
typedef enum : NSUInteger{
AFCollectionViewFlowLayoutModeAspectFit, //Default
AFCollectionViewFlowLayoutModeAspectFill
}AFCollectionViewFlowLayoutMode;
2.Subclass UICollectionViewLayoutAttributes
@interface AFCollectionViewLayoutAttributes :UICollectionViewLayoutAttributes
@property (nonatomic,assign) AFCollectionViewFlowLayoutMode layoutMode;
@end
3.Subclass UICollectionViewFlowLayout
@interface AFCollectionViewFlowLayout :UICollectionViewFlowLayout
@property (nonatomic,assign) AFCollectionViewFlowLayoutMode layoutMode;
@end
4.Override +(Class)layoutAttributesClass in superClass UICollectionViewFlowLayout
+(Class)layoutAttributesClass
{
// Important for letting UICollectionView know what kind of attributes to use.
return [AFCollectionViewLayoutAttributesclass];
}
5.Override Methods#pragma mark Cell Layout
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attributesArray = [superlayoutAttributesForElementsInRect:rect];
for (AFCollectionViewLayoutAttributes *attributesin attributesArray)
{
[selfapplyLayoutAttributes:attributes];
}
return attributesArray;
}
#pragma mark - Private Helper Methods
-(void)applyLayoutAttributes:(AFCollectionViewLayoutAttributes *)attributes
{
// Check for representedElementKind being nil, indicating this is a cell and not a header or decoration view
if (attributes.representedElementKind == nil)
{
// Pass our layout mode onto the layout attributes
attributes.layoutMode = self.layoutMode;
if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:layoutModeForItemAtIndexPath:)])
{
attributes.layoutMode = [(id<AFCollectionViewDelegateFlowLayout>)self.collectionView.delegate collectionView:self.collectionView layout:selflayoutModeForItemAtIndexPath:attributes.indexPath];
}
}
}
#pragma mark - Overridden Properties
-(void)setLayoutMode:(AFCollectionViewFlowLayoutMode)layoutMode
{
// Update our backing ivar...
_layoutMode = layoutMode;
// then invalidate our layout.
[selfinvalidateLayout];
}
#pragma mark - Overide -(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes in UICollectionViewCell
#pragma mark - Overridden Methods
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
[superapplyLayoutAttributes:layoutAttributes];
// Important! Check to make sure we're actually this special subclass.
// Failing to do so could cause the app to crash!
if (![layoutAttributesisKindOfClass:[AFCollectionViewLayoutAttributesclass]])
{
return;
}
AFCollectionViewLayoutAttributes *castedLayoutAttributes = (AFCollectionViewLayoutAttributes *)layoutAttributes;
layoutMode = castedLayoutAttributes.layoutMode;
[selfsetImageViewFrame];
}
Conclusion: 1. be aware of the difference between the UIViewContentModeScaleAspectFill and UIViewContentModeScaleAspectFit
2. be aware of the bounds, frame, center of UIView
3. know how to calculate the aspectRatio according to the imageView.image.size
4. know the layoutAttributesForElementInRect from UICollectionViewFlowLayout and -(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes in UICollectionViewCell was called by iOS before the
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath.
- iOS CollectionView the completed guide(2th) chapter 04 Example Dimensions
- 《Hadoop:The Definitive Guide 4th Edition》Chapter 17 Hive——A部分
- 《Hadoop:The Definitive Guide 4th Edition》Chapter 1 Meet Hadoop
- 《Hadoop:The Definitive Guide 4th Edition》Chapter 17 Hive——B部分
- The definitive guide to the xen hypervisor ---chapter 2
- OpenGL ES2.0 Programming Guide - chapter 2:Hello Triangle: An OpenGL ES2.0 example
- 《Hadoop: The Definitive Guide》读书笔记 -- Chapter 2 MapReduce
- A bug in example of JS definitive guide 6th
- JavaScript: The Definitive Guide, 4th Edition
- Hadoop:The Definitive Guide 4th Edition
- Hadoop The Definitive Guide 4th Editon
- JavaScript operators( from Javascrip: The definitive guide, 4th guide)
- iOS Programming: The Big Nerd Ranch Guide (4th Edition) 阅读笔记-关于View和View Hierarchy
- iOS Programming: The Big Nerd Ranch Guide (4th Edition) 阅读笔记-关于 View Controllers及其生命周期
- Programmer's Guide to the JavaTM 2D API -- Chapter 5 Imaging
- Programmer's Guide to the JavaTM 2D API -- Chapter 6 Color
- 《The Definitive Guide to Linux Network Programming》 (《Linux 网络编程指南》) Chapter 2
- Chapter 2 The ARM Archetecture——Cortex-A Series Programmer's Guide
- Linux学习笔记(6)
- 源码管理十三:分支管理策略
- 神州数码
- 30 Best Tools for Data Visualization
- python学习笔记(七) - 面向对象高级编程
- iOS CollectionView the completed guide(2th) chapter 04 Example Dimensions
- Mysql 列转行统计查询 、行转列统计查询
- 斯坦福大学机器学习公开课---Programming Exercise 1: Linear Regression
- XML 文件解析 SAXParserFactory
- Vim 复制粘贴探秘
- [数据结构] 图的邻接矩阵广度优先搜索
- CentOS下安装SecureCRT的sz/rz工具包
- 基于mt7620的newifi y1的Pandorabox新软件源备忘
- c语言-数据类型-基本数据类型