我建议使用
CAShapeLayer
。你甚至可以对填充颜色进行动画处理。这样做可以保证性能良好,内存占用低。
该函数创建一个包含六边形的CGPath:(基于OP问题)
CGPathRef CGPathCreateHexagon( CGFloat hexWidth, CGFloat hexHeight )
{
CGMutablePathRef p = CGPathCreateMutable() ;
CGPathMoveToPoint( p, NULL, hexWidth * 0.5, 0.0 ) ;
CGPathAddLineToPoint( p, NULL, hexWidth, hexHeight * 0.75 ) ;
CGPathAddLineToPoint( p, NULL, hexWidth, hexHeight * 0.75 ) ;
CGPathAddLineToPoint( p, NULL, hexWidth * 0.5, hexHeight ) ;
CGPathAddLineToPoint( p, NULL, 0.0, hexHeight * 0.75 ) ;
CGPathAddLineToPoint( p, NULL, hexWidth * 0.5, 0.0 ) ;
CGPathAddLineToPoint( p, NULL, 0.0, hexHeight * 0.25 ) ;
CGPathAddLineToPoint( p, NULL, hexWidth * 0.5, 0.0 ) ;
CGPathCloseSubpath( p ) ;
return p ;
}
设置 UIView
时使用的方法:
-(void)addHexagonLayer
{
CAShapeLayer * layer = [ CAShapeLayer layer ] ;
layer.lineWidth = 10.0 ;
{
CGPathRef p = CGPathCreateHexagon( 100.0, 100.0 ) ;
layer.path = p ;
CGPathRelease( p ) ;
}
layer.fillColor = [[ UIColor redColor ] CGColor ] ;
layer.position = (CGPoint){ CGRectGetMidX( self.view.bounds ), CGRectGetMidY( self.view.bounds ) } ;
[ self.view.layer addSublayer:layer ] ;
}
编辑 我只是为了好玩创建了一个完整的演示:
#import "AppDelegate.h"
static CGPathRef CGPathCreateHexagon( CGAffineTransform * t, CGFloat w, CGFloat h )
{
CGFloat w_4 = w * 0.25 ;
CGFloat w_2 = w * 0.5f ;
CGFloat h_2 = h * 0.5f ;
CGMutablePathRef p = CGPathCreateMutable() ;
CGPathAddLines( p, t, (CGPoint[]){
{ -w_4, h_2 }, { w_4, h_2 }, { w_2, 0 }, { w_4, -h_2 }, { -w_4, -h_2 }, { -w_2, 0 }
}, 6 ) ;
CGPathCloseSubpath( p ) ;
return p ;
}
@implementation CALayer (SetPositionPixelAligned)
-(CGPoint)pixelAlignedPositionForPoint:(CGPoint)p
{
CGSize size = self.bounds.size ;
CGPoint anchorPoint = self.anchorPoint ;
CGPoint result = (CGPoint){ roundf( p.x ) + anchorPoint.x * fmodf( size.width, 2.0f ), roundf( p.y ) + anchorPoint.y * fmodf( size.height, 2.0f ) } ;
return result;
}
@end
@interface HexagonsView : UIView
@property ( nonatomic ) CGFloat hexHeight ;
@property ( nonatomic ) CGFloat hexWidth ;
@property ( nonatomic, readonly ) CGPathRef hexagonPath ;
@end
@implementation HexagonsView
@synthesize hexagonPath = _hexagonPath ;
-(void)dealloc
{
CGPathRelease( _hexagonPath ) ;
_hexagonPath = NULL ;
}
-(CGPathRef)hexagonPath
{
if ( !_hexagonPath )
{
_hexagonPath = CGPathCreateHexagon( NULL, self.hexWidth, self.hexHeight ) ;
}
return _hexagonPath ;
}
-(void)setHexWidth:(CGFloat)w
{
_hexWidth = w ;
CGPathRelease( _hexagonPath ) ;
_hexagonPath = NULL ;
}
-(void)setHexHeight:(CGFloat)h
{
_hexHeight = h ;
CGPathRelease( _hexagonPath ) ;
_hexagonPath = NULL ;
}
-(void)layoutSubviews
{
[ super layoutSubviews ] ;
CGRect bounds = self.bounds ;
bounds.size.height += self.hexHeight * 0.5 ;
CGPoint p ;
p.x = CGRectGetMinY( bounds ) ;
while( p.y < CGRectGetMaxY( bounds ) )
{
p.x = CGRectGetMinX( bounds ) ;
while( p.x < CGRectGetMaxX( bounds ) )
{
{
CAShapeLayer * layer = [ CAShapeLayer layer ] ;
layer.path = self.hexagonPath ;
layer.fillColor = [[ UIColor colorWithHue:(CGFloat)arc4random_uniform( 100 ) / 256.0 saturation:1.0 brightness:1.0 alpha:1.0 ] CGColor ] ;
layer.position = [ layer pixelAlignedPositionForPoint:p ] ;
[ self.layer addSublayer:layer ] ;
}
CGPoint p2 = { p.x + self.hexWidth * 0.75f, p.y + self.hexHeight * 0.5f } ;
if ( p2.y < CGRectGetMaxY( bounds ))
{
CAShapeLayer * layer = [ CAShapeLayer layer ] ;
layer.path = self.hexagonPath ;
layer.fillColor = [[ UIColor colorWithHue:(CGFloat)arc4random_uniform( 256 ) / 256.0 saturation:1.0 brightness:1.0 alpha:1.0 ] CGColor ] ;
layer.position = [ layer pixelAlignedPositionForPoint:p2 ] ;
[ self.layer addSublayer:layer ] ;
}
p.x += self.hexWidth * 1.5 ;
}
p.y += self.hexHeight ;
}
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
HexagonsView * view = [[ HexagonsView alloc ] initWithFrame:self.window.bounds ] ;
view.hexHeight = 100.0f ;
view.hexWidth = 100.0f ;
[ self.window addSubview:view ] ;
[self.window makeKeyAndVisible];
return YES;
}
@end