UICollectionView: custom Layout cellular Layout, uicollectionview
The Layout of UICollectionView on the Internet. The cell shapes are mostly Rectangles and circles.
This article uses the regular hexagonal form as the basic cell shape to show you the unique cellular layout effect and source code.
It helps you make your apps stand out and stand out from each other.
Latest complete code: https://github.com/duzixi/Varied-Layouts
Blog address: http://blog.csdn.net/duzixi
Implementation:
Core source code:
Custom Layout
/// HoneyCombLayout. h // Demo-Layouts /// Created by duzixi on 14-9-1. // Copyright (c) 2014 lanou3g.com All rights reserved. // # import <UIKit/UIKit. h> @ interface HoneyCombLayout: UICollectionViewLayout @ property (nonatomic, assign) NSInteger margin; @ property (nonatomic, assign) NSInteger oX; @ property (nonatomic, assign) NSInteger oY; @ end
/// HoneyCombLayout. m // Demo-Layouts /// Created by duzixi on 14-9-1. // Copyright (c) 2014 lanou3g.com All rights reserved. // # import "HoneyCombLayout. h "@ implementation HoneyCombLayout // the SIZE of the returned content, used to determine whether to accelerate the slide-(CGSize) collectionViewContentSize {float height = (SIZE + self. margin) * ([self. collectionView numberOfItemsInSection: 0]/4 + 1); return CGSizeMake (320, height);} // return YES, change layout/*-(BOOL) shouldInvalidateLayoutForBoundsChange :( CGRect) newBounds {return YES;} */# pragma mark-UICollectionViewLayout // generate Layout features for each Item-(UICollectionViewLayoutAttributes *) layoutattriattributesforitematindexpath :( NSIndexPath *) indexPath {UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutbutesforcellwithindexpath: indexPath]; UICollectionView * collection = self. collectionView; float x = (SIZE + self. margin) * (indexPath. item % COL + 1) * 0.75; float y = (SIZE + self. margin) * (indexPath. item/COL + 0.5) * cos (M_PI * 30366f/180.0f); if (indexPath. item % 2 = 1) {y + = (SIZE + self. margin) * 0.5 * cosf (M_PI * 30366f/180.0f);} x + = self. oX; y + = self. oY; attributes. center = CGPointMake (x + collection. contentOffset. x, y + collection. contentOffset. y); attributes. size = CGSizeMake (SIZE, SIZE * cos (M_PI * 30366f/180.0f); return attributes;}-(NSArray *) layoutAttributesForElementsInRect :( CGRect) rect {NSArray * arr = [super layoutAttributesForElementsInRect: rect]; if ([arr count]> 0) {return arr;} NSMutableArray * attributes = [NSMutableArray array]; for (NSInteger I = 0; I <[self. collectionView numberOfItemsInSection: 0]; I ++) {NSIndexPath * indexPath = [NSIndexPath indexPathForItem: I inSection: 0]; [attributes addObject: [self layoutAttributesForItemAtIndexPath: indexPath];} return attributes;} @ end
Custom cell:
/// HoneycombViewCell. h // Demo-Layouts /// Created by duzixi on 14-9-1. // Copyright (c) 2014 lanou3g.com All rights reserved. // # import <UIKit/UIKit. h> @ interface HoneycombViewCell: UICollectionViewCell @ property (nonatomic, strong) UILabel * titleLabel; @ end
/// HoneycombViewCell. m // Demo-Layouts /// Created by duzixi on 14-9-1. // Copyright (c) 2014 lanou3g.com All rights reserved. // # import "HoneycombViewCell. h "@ implementation HoneycombViewCell-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// Initialization code self. titleLabel = [[UILabel alloc] init]; self. titleLabel. textColor = [UIColor whiteColor]; [self. contentView addSubview: self. titleLabel];} return self;}-(void) layoutSubviews {[super layoutSubviews]; // step 1: generate the hexagonal path CGFloat longSide = SIZE * 0.5 * cosf (M_PI * 30/180); CGFloat lateral side = SIZE * 0.5 * sin (M_PI * 30/180); UIBezierPath * path = [UIBezierPath bezierPath]; [path moveToPoint: CGPointMake (0, longSide)]; [path addLineToPoint: CGPointMake (lateral side, 0)]; [path addLineToPoint: CGPointMake (lateral side + SIZE * 0.5, 0)]; [path addLineToPoint: CGPointMake (SIZE, longSide)]; [path addLineToPoint: CGPointMake (lateral side + SIZE * 0.5, longSide * 2)]; [path addLineToPoint: CGPointMake (lateral side, longSide * 2)]; [path closePath]; // step 2: generate a mask CAShapeLayer * maskLayer = [CAShapeLayer layer]; maskLayer. path = [path CGPath]; // step 3: add the template self to the cell. layer. mask = maskLayer; self. backgroundColor = [UIColor orangeColor]; self. titleLabel. textAlignment = NSTextAlignmentCenter; self. titleLabel. frame = self. contentView. frame;}/* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. -(void) drawRect :( CGRect) rect {// Drawing code} */@ end
How does Android get the custom components in the Layout file in Layout?
First, obtain the LayoutInflater of the current Activity:
LayoutInflater factory = LayoutInflater. from (this );
Use the LayoutInflater object to obtain Layout.
Final View DialogView = factory. inflate (R. layout. dialog, null );
Finally, use the layout to obtain the component.
M_EditText1 = (EditText) DialogView. findViewById (R. id. ip_address2 );