1.背景颜色渐变色渲染
// 渲染背景
CAGradientLayer *gl = [CAGradientLayer layer];
gl.frame = titleView.bounds;
gl.startPoint = CGPointMake(1, 0.3215215802192688);
gl.endPoint = CGPointMake(0, 0.3252418041229248);
gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:164/255.0 blue:6/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:254/255.0 green:59/255.0 blue:83/255.0 alpha:1.0].CGColor];
gl.locations = @[@(0),@(1.0f)];
[titleView.layer addSublayer:gl];
这种方式不能直接在当前View(titleView)添加组件,组件不能正常显示,同样会被该方法渲染掉。推荐使用一个backView做渲染背景,组件放在底部View上,且在渲染背景backView上方。
2.设置部分圆角
UIButton * button =[UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];//一定要先添加到视图上
button.frame=CGRectMake(100, 100, 100, 40);
button.backgroundColor=[UIColor blueColor];
UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer=[[CAShapeLayer alloc]init];
maskLayer.frame=button.bounds;
maskLayer.path=maskPath.CGPath;
button.layer.mask=maskLayer;
设置圆角会跟动态高度布局出现问题,使用时请谨慎使用。
3.绘制虚线
// 调用方法
[self drawDashLine:self.rightImaginaryLineView lineLength:5 lineSpacing:5 lineColor:RGB(237, 237, 237)];
/**
** lineView: 需要绘制成虚线的view
** lineLength: 虚线的宽度
** lineSpacing: 虚线的间距
** lineColor: 虚线的颜色
**/
- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:lineView.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// 设置虚线颜色
[shapeLayer setStrokeColor:lineColor.CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength],[NSNumber numberWithInt:lineSpacing], nil]];
// 设置路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[lineView.layer addSublayer:shapeLayer];
}
4.绘制单边边框
// 单边边框(下边)
CALayer * bottomBorder = [CALayer layer];
float height = self.commentTextView.height;
float width = self.commentTextView.width;
bottomBorder.frame = CGRectMake(0, height-1, width, 0.5);
bottomBorder.backgroundColor = RGB(240, 240, 240).CGColor;
[self.commentTextView.layer addSublayer:bottomBorder];
网友评论