iOS Xib畫虛線方法

  • 代碼設置尺寸
  • xib設置類型!

Xib添加虛線

@implementation BNDiscountCouponDottedLine

#pragma mark - Initial Methods
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self drawDottedLine];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
    
    if (self = [super initWithCoder:aDecoder]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self drawDottedLine];
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    [self drawDottedLine];
}

#pragma mark - Private

-(void)drawDottedLine{
    
    CAShapeLayer *dotteShapeLayer = [CAShapeLayer layer];
    CGMutablePathRef dotteShapePath =  CGPathCreateMutable();
    //設置虛線顏色為blackColor
    [dotteShapeLayer setStrokeColor:UIColorFromHexValue(0xededed).CGColor];
    //設置虛線寬度
    dotteShapeLayer.lineWidth = 1.0f ;
    //10=線的寬度 5=每條線的間距
    NSArray *dotteShapeArr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:4],[NSNumber numberWithInt:2], nil];
    [dotteShapeLayer setLineDashPattern:dotteShapeArr];
    CGPathMoveToPoint(dotteShapePath, NULL, 0 ,0);
    CGPathAddLineToPoint(dotteShapePath, NULL, self.width, 0);
    [dotteShapeLayer setPath:dotteShapePath];
    CGPathRelease(dotteShapePath);
    //把繪制好的虛線添加上來
    [self.layer removeAllSublayers];
    [self.layer addSublayer:dotteShapeLayer];
}
@end
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容