我有一个类继承自 UIView,我想在drawRect中将view画成多边形。我已经画出了形状(红色部分),但我想把蓝色部分给裁剪掉,但我用了CGContexClip竟没有效果,请问我代码里哪里出问题啦???
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor blueColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect //画出边框
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat arrowHeight = 15.0;
CGFloat arrowWidth = 13.0;
CGPoint arrowPoint = CGPointMake(rect.origin.x+rect.size.width/2.0f,rect.origin.y);
CGMutablePathRef arrowPath = CGPathCreateMutable();
CGPathMoveToPoint(arrowPath, NULL, arrowPoint.x, arrowPoint.y);
CGPathAddLineToPoint(arrowPath, NULL, arrowPoint.x, arrowPoint.y+arrowHeight);
CGPathAddLineToPoint(arrowPath, NULL, rect.origin.x, rect.origin.y+arrowHeight);
CGPathAddLineToPoint(arrowPath, NULL, rect.origin.x, rect.origin.y+rect.size.height);
CGPathAddLineToPoint(arrowPath, NULL, rect.origin.x+rect.size.width, rect.origin.y+rect.size.height);
CGPathAddLineToPoint(arrowPath, NULL, rect.origin.x+rect.size.width, rect.origin.y+arrowHeight);
CGPathAddLineToPoint(arrowPath, NULL, arrowPoint.x+arrowWidth, rect.origin.y+arrowHeight);
CGPathCloseSubpath(arrowPath); //封口
CGContextAddPath(ctx, arrowPath);
[[UIColor redColor] setFill];
CGContextDrawPath(ctx,kCGPathFill);
CGContextClip(ctx);
CGPathRelease(arrowPath);
}
多谢多谢!!
drawRect 自定义UIView
------解决方案--------------------
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor blueColor]];//此句修改成 [self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
------解决方案--------------------
设置成背景透明,画之前先调用清除cgcontext中的内容。