今天剛好需要添加一個圓角的view。網上搜尋了一下。找到如下兩種方法。
一、直接設定(已通過測試)
必須先匯入標頭檔。
#import <QuartzCore/QuartzCore.h>
...
bgView.layer.cornerRadius = cornerRadiusInPixels;
bgView.layer.masksToBounds = YES;
bgView.opaque = NO;
二、在drawRect裡畫(未測試)
...
CGFloat radius = 20.0;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1);
CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect);
CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFill);