iOS 高性能圓角及邊框(實(shí)線/虛線)

/**
 設(shè)置任意圓角及邊框(實(shí)線/虛線)
 
 @param corners 要設(shè)置的圓角位置集合
 @param radius 圓角半徑
 @param lineWidth 邊框?qū)挾? @param lineColor 邊框顏色
 @param lineDashPattern 虛線集合
 */
- (void)rounderWithCorners:(UIRectCorner)corners radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth lineColor:(UIColor *_Nullable )lineColor dash:(NSArray<NSNumber *>*_Nullable )lineDashPattern{
    
    ///解決masonry布局獲取不了正確的frame
    [self.superview layoutIfNeeded];
    
    //繪制圓/圓弧---適用于全圓
    //UIBezierPath *maskPath = [UIBezierPath bezierPathWithArcCenter:self.bounds radius:iconWidth*0.5 startAngle:0 endAngle:2*M_PI clockwise:YES];
    //矩形---圓角
    //UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.iconImage.width*0.5];
    //寬高相等為全圓,不等為橢圓
    //UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius,radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.lineWidth = lineWidth;
    borderLayer.strokeColor = lineColor.CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    if (lineDashPattern) {
        borderLayer.lineDashPattern = lineDashPattern;
        //borderLayer.lineCap = @"butt";
    }
    borderLayer.path = maskPath.CGPath;
    [self.layer insertSublayer:borderLayer atIndex:0];
    self.layer.mask = maskLayer;
}


/**
 設(shè)置任意圓角 --- 性能優(yōu)化
 
 @param corners 要設(shè)置的圓角位置集合
 @param radius 圓角半徑
 */
- (void)rounderWithCorners:(UIRectCorner)corners radius:(CGFloat)radius{
    
    [self.superview layoutIfNeeded];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius,radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    
}

/**
 設(shè)置圓角 --- 性能優(yōu)化
 
 @param radius 圓角半徑
 */
- (void)roundCornerWithRadius:(CGFloat)radius{
    
    [self rounderWithCorners:UIRectCornerAllCorners radius:radius];
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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