iOS 開發之為視圖繪製單(多)個圓角實例代碼
前言:
為視圖繪製圓角時,可以選擇左上角、左下角、右下角、右上角,或全部圓角。以下示範以 Core Animation 相關 API 實作:
//Core Raduias UIView *actionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:actionView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = actionView.bounds; maskLayer.path = maskPath.CGPath; actionView.layer.mask = maskLayer;
UIRectCorner 列舉值如下:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL };
透過 UIBezierPath 搭配 CAShapeLayer 遮罩,即可靈活控制要顯示圓角的邊。以上就是本文的全部內容,希望對大家有所幫助。