CAShapeLayer + UIBezierPath = sectionView

直接看看这块代码。

其实可以发现,

    CAShapeLayer *_layerRight;

    UIBezierPath *_pathLeft;

--------layer是一个绘图层,可以通过BezierPath来决定绘制图形的样式

下面的类是一个可以实现左右颜色不一,矩形视图。

#import <UIKit/UIKit.h>


@interface XWSectionPlatView : UIView

@property (nonatomic,strong) UIColor *leftColor;

@property (nonatomic,strong) UIColor *rightColor;

@property (nonatomic,assign) CGFloat leftSpan;


@end




#import "XWSectionPlatView.h"


@implementation XWSectionPlatView

{

    CAShapeLayer *_layerLeft;

    CAShapeLayer *_layerRight;

    UIBezierPath *_pathLeft;

    UIBezierPath *_pathRight;

}


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        _leftSpan =self.frame.size.width * 0.25;


        _layerLeft = [[CAShapeLayeralloc] init];

        _layerRight = [[CAShapeLayeralloc] init];

        _layerLeft.fillColor =nil;

        _layerRight.fillColor =nil;

        _layerLeft.frame =self.bounds;

        _layerRight.frame =self.bounds;


        self.backgroundColor = [UIColorblueColor];

    }

    returnself;

}

- (void)setLeftLayer{

    _pathLeft = [UIBezierPathbezierPathWithRect:CGRectMake(0, 0,_leftSpan, self.frame.size.height)];

    _layerLeft.path =_pathLeft.CGPath;

}


- (void)setRightLayer{

    _pathRight = [UIBezierPathbezierPathWithRect:CGRectMake(_leftSpan, 0,self.frame.size.width - _leftSpan, self.frame.size.height)];

}


- (void)setLeftSpan:(CGFloat)leftSpan {

    _leftSpan = leftSpan;


    [selfsetRightLayer];

    [selfsetLeftLayer];

}

- (void)setRightColor:(UIColor *)rightColor {

    _layerRight.fillColor = rightColor.CGColor;

    [selfsetRightLayer];

}

- (void)setLeftColor:(UIColor *)leftColor {

    _layerLeft.fillColor = leftColor.CGColor;

    [selfsetLeftLayer];

}



@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值