開發中常用到圓角,圓角邊框色等,現在記錄一種高效繪制方式:
主要用到類有: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;
此處比較簡單,不再附截圖,具體效果,可直接粘貼代碼查看效果
圓角+邊框色:
??? 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 時,在創建view 時需要設置frame,也可以直接創建CGRect,但要保證size與masonry設置的size相同。