a、利用UIBezierPath在DrawRect:中绘制一个给定颜色的三角位图,代码如下:
- (void)drawRect:(CGRect)rect
{ // Drawing code
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(rect.size.width/2, 0)];
[path addLineToPoint:CGPointMake(rect.size.width, rect.size.height)];
[path addCurveToPoint:CGPointMake(0, rect.size.height) controlPoint1:CGPointMake(rect.size.width/2, rect.size.height) controlPoint2:CGPointMake(rect.size.width/2, rect.size.height)];
[path closePath];
[[UIColor colorWithRed:25.0/255 green:141.0/225 blue:200.0/255 alpha:1] setStroke];
[[UIColor colorWithRed:25.0/255 green:141.0/225 blue:200.0/255 alpha:1] setFill];
[path stroke];
[path fill];
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 3;
self.layer.shadowOpacity = 0.4;
// self.layer.shadowPath = [];
}
b、UIBezierPath和CAShapelayer的组合使用,其实是CAShaperLayer可以拥有一个自己的贝塞尔路径,绘制一左右两半颜色不同的图层视图,代码如下:
#import "BezierView.h"
@interface BezierView()
{
CAShapeLayer *_leftLayer;
UIBezierPath *_leftPath;
CAShapeLayer *_rightLayer;
UIBezierPath *_rightPath;
}
@end
@implementation BezierView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_leftSpan = self.frame.size.width/4;
_leftLayer = [[CAShapeLayer alloc] init];
[self.layer addSublayer:_leftLayer];//将图层添加添加上去。
_leftLayer.fillColor = nil;
_leftLayer.frame = self.bounds;
_rightLayer = [[CAShapeLayer alloc] init];
[self.layer addSublayer:_rightLayer];//图层的添加
_rightLayer.fillColor = nil;
_rightLayer.frame = self.bounds;
self.backgroundColor = [UIColor blueColor];
}
return self;
}
-(void)setLeftOne
{
_leftPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, _leftSpan, self.frame.size.height)];
_leftLayer.path = _leftPath.CGPath;
}
-(void)setRightOne
{
_rightPath = [UIBezierPath bezierPathWithRect:CGRectMake(_leftSpan, 0, self.frame.size.width-_leftSpan, self.frame.size.height)];
_rightLayer.path = _rightPath.CGPath;
}
-(void)setRightColor:(UIColor *)rightColor
{
_rightLayer.fillColor = rightColor.CGColor;
[self setRightOne];
}
-(void)setLeftColor:(UIColor *)leftColor
{
_leftLayer.fillColor = leftColor.CGColor;
[self setLeftOne];
}
-(void)setLeftSpan:(CGFloat)leftSpan
{
_leftSpan = leftSpan;
[self setLeftOne];
[self setRightOne];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
UIBezierPath,CAShaperLayer,