開發(fā)中常用到圓角,圓角邊框色等瘤缩,現(xiàn)在記錄一種高效繪制方式:
主要用到類有:UIBezierPath喇完、CAShapeLayer
僅僅圓角,直接上代碼:
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake([Adapter adapterWidthBy6s:30], 0, [Adapter adapterWidthBy6s:200], [Adapter adapterWidthBy6s:200])];? UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:img.bounds cornerRadius:3.f];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
? ? maskLayer.frame =
;
? ? maskLayer.path = maskPath.CGPath;
? ? img.layer.mask = maskLayer;
此處比較簡(jiǎn)單剥啤,不再附截圖锦溪,具體效果,可直接粘貼代碼查看效果
圓角+邊框色:
??? UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake([Adapter adapterWidthBy6s:30], 0, [Adapter adapterWidthBy6s:200], [Adapter adapterWidthBy6s:200])];
??? //圓角+邊框色
??? CAShapeLayer *maskLayer = [CAShapeLayer layer];
??? maskLayer.frame = img.bounds;
??? CAShapeLayer *borderLayer = [CAShapeLayer layer];
??? borderLayer.frame = img.bounds;
??? borderLayer.lineWidth = 1.f;
??? borderLayer.strokeColor = [UIColor textf0f3f5Color].CGColor;
??? borderLayer.fillColor = [UIColor clearColor].CGColor;
??? UIBezierPath *bezierPath =? [UIBezierPath bezierPathWithRoundedRect:img.bounds cornerRadius:3.f];
??? maskLayer.path = bezierPath.CGPath;
??? borderLayer.path = bezierPath.CGPath;
??? [img.layer insertSublayer:borderLayer atIndex:0];
??? [img.layer setMask:maskLayer];
使用Masonry 時(shí)府怯,在創(chuàng)建view 時(shí)需要設(shè)置frame海洼,也可以直接創(chuàng)建CGRect,但要保證size與masonry設(shè)置的size相同富腊。