UICollectionView自定义Layout之蜂窝布局

自定义Layout

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
//  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
</uikit>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
//  HoneyCombLayout.m
//  Demo-Layouts
//
//  Created by 杜子兮(duzixi) on 14-9-1.
//  Copyright (c) 2014年 lanou3g.com All rights reserved.
//
 
# import HoneyCombLayout.h
 
@implementation HoneyCombLayout
 
///  返回内容大小,用于判断是否需要加快滑动
 
-(CGSize)collectionViewContentSize
{
     float height = (SIZE + self.margin) * ([self.collectionView numberOfItemsInSection: 0 ] / 4 + 1 );
     return CGSizeMake( 320 , height);
}
 
 
///  返回YES,改变布局
/*
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
     return YES;
}
*/
#pragma mark - UICollectionViewLayout
///  为每一个Item生成布局特性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
     UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath: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 * 30 .0f / 180 .0f);
     if (indexPath.item % 2 == 1 ) {
         y += (SIZE + self.margin) * 0.5 * cosf(M_PI * 30 .0f / 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 * 30 .0f / 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

自定义cell:

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
//  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
</uikit>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
//  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: 生成六边形路径
     CGFloat longSide = SIZE * 0.5 * cosf(M_PI * 30 / 180 );
     CGFloat shortSide = SIZE * 0.5 * sin(M_PI * 30 / 180 );
     UIBezierPath *path = [UIBezierPath bezierPath];
     [path moveToPoint:CGPointMake( 0 , longSide)];
     [path addLineToPoint:CGPointMake(shortSide, 0 )];
     [path addLineToPoint:CGPointMake(shortSide + SIZE * 0.5 , 0 )];
     [path addLineToPoint:CGPointMake(SIZE, longSide)];
     [path addLineToPoint:CGPointMake(shortSide + SIZE * 0.5 , longSide * 2 )];
     [path addLineToPoint:CGPointMake(shortSide, longSide * 2 )];
     [path closePath];
     
     // step 2: 根据路径生成蒙板
     CAShapeLayer *maskLayer = [CAShapeLayer layer];
     maskLayer.path = [path CGPath];
     
     // step 3: 给cell添加模版
     self.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Log4j2是一个灵活的日志框架,允许用户自定义日志输出格式(layout)。以下是自定义Log4j2 layout的步骤: 1. 创建自定义layout类,该类应继承AbstractStringLayout,并实现toSerializable方法。该方法接受一个LogEvent对象,将其转换为字符串格式并返回。 示例代码如下: ```java @Plugin(name = "CustomLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) public class CustomLayout extends AbstractStringLayout { protected CustomLayout() { super(StandardCharsets.UTF_8); } public String toSerializable(LogEvent event) { // 自定义日志输出格式的实现代码 return null; } @PluginFactory public static CustomLayout createLayout(@PluginAttribute(value = "pattern") String pattern) { return new CustomLayout(); } } ``` 2. 在log4j2.xml文件中添加自定义layout的配置,例如: ```xml <Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <CustomLayout/> </Console> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> ``` 在以上配置中,我们将自定义layout作为Console appender的子元素,并命名为CustomLayout。 3. 在代码中使用自定义layout,例如: ```java private static final Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME); public static void main(String[] args) { logger.info("Hello, world!"); } ``` 以上是自定义Log4j2 layout的简单步骤,您可以根据实际需求自定义不同的layout,输出不同的日志格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值