CAShapeLayer + UIBezierPath = sectionView

来源:互联网 发布:淘宝复刻鞋店 编辑:程序博客网 时间:2024/05/21 11:37

直接看看这块代码。

其实可以发现,

    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



0 0