国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS常用組件之高效切圓角的方法匯總

2019-10-21 18:42:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

圓角(RounderCorner)是一種很常見(jiàn)的視圖效果,相比于直角,它更加柔和優(yōu)美,易于接受。但很多人并不清楚如何設(shè)置圓角的正確方式和原理。

iOS 客戶(hù)端開(kāi)發(fā)中,經(jīng)常碰到圓角視圖的需求,本文簡(jiǎn)單總結(jié)一下 UIView 及其子類(lèi)的一些切圓角方法,并且保證避免出現(xiàn)離屏渲染。下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。

UIView(不包括其子類(lèi))

UIView *view = [[UIView alloc] init];view.backgroundColor = [UIColor blackColor];view.layer.cornerRadius = 3.f;// 以下兩行,任寫(xiě)一行view.layer.masksToBounds = NO;view.clipToBounds = NO;// 以下兩行,千萬(wàn)不要加!view.layer.masksToBounds = YES;view.clipToBounds = YES;

注意點(diǎn):UIView 只要設(shè)置圖層的 cornerRadius 屬性即可(不明白的話,可以看看官方文檔里對(duì) cornerRadius 的描述),如果設(shè)置 layer.masksToBounds = YES ,會(huì)造成不必要的離屏渲染。

文本類(lèi)視圖

UITextField

UITextField有兩種實(shí)現(xiàn)方法

// 天然支持設(shè)置圓角邊框UITextField *textField = [[UITextField alloc] init];textField.borderStyle = UITextBorderStyleRoundedRect;
// 與 UIView 類(lèi)似UITextField *textField = [[UITextField alloc] init];textField.layer.cornerRadius = cornerRadius;

UITextView

// 與 UIView 類(lèi)似UITextView *textView = [[UITextView alloc] init];textView.layer.cornerRadius = cornerRadius;

UILabel

UILabel *label = [[UILabel alloc] init];// 重點(diǎn)在此!!設(shè)置視圖的圖層背景色,千萬(wàn)不要直接設(shè)置 label.backgroundColorlabel.layer.backgroundColor = [UIColor grayColor].CGColor;label.layer.cornerRadius = cornerRadius;

其它

UIButton

說(shuō)明:UIButton 的背景圖片,如果是復(fù)雜的圖片,可以依靠 UI 切圖來(lái)實(shí)現(xiàn)。如果是簡(jiǎn)單的純色背景圖片,可以利用代碼繪制帶圓角的圖片。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];// 設(shè)置 UIButton 的背景圖片。[button setBackgroundImage:image forState:UIControlStateNormal];

背景圖片繪制方法

+ (UIImage *)pureColorImageWithSize:(CGSize)size color:(UIColor *)color cornRadius:(CGFloat)cornRadius { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, size.height)]; view.backgroundColor = color; view.layer.cornerRadius = cornerRadius; // 下面方法,第一個(gè)參數(shù)表示區(qū)域大小。第二個(gè)參數(shù)表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個(gè)參數(shù)是屏幕密度 UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}

UIImageView

UIImageView 有四種方式實(shí)現(xiàn)圓角:

截取圖片方式(性能較好,基本不掉幀)

@implementation UIImage (extend)- (UIImage *)drawRectWithRoundedCorner{ CGRect rect = CGRectMake(0.f, 0.f, 150.f, 150.f);  UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:rect.size.width * 0.5]; UIGraphicsBeginImageContextWithOptions(rect.size, false, [UIScreen mainScreen].scale); CGContextAddPath(UIGraphicsGetCurrentContext(), bezierPath.CGPath); CGContextClip(UIGraphicsGetCurrentContext()); [self drawInRect:rect]; CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke); UIImage *output = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();  return output;}@end

貝塞爾曲線切割圓角(不推薦,掉幀嚴(yán)重)

- (UIImageView *)roundedRectImageViewWithCornerRadius:(CGFloat)cornerRadius { UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius]; CAShapeLayer *layer = [CAShapeLayer layer]; layer.path = bezierPath.CGPath; self.layer.mask = layer; return self;}

繪制四個(gè)角的遮罩(使用場(chǎng)景受限)

在 UIImageView 上添加一個(gè)四個(gè)角有內(nèi)容,其它部分是透明的視圖,只對(duì) UIImageView 圓角部分進(jìn)行遮擋。但要保證被遮擋的部分背景色要與周?chē)尘跋嗤苊獯汀K援?dāng) UIImageView 處于一個(gè)復(fù)雜的背景時(shí),是不適合使用這個(gè)方法的。

最不推薦做法(當(dāng)一個(gè)頁(yè)面只有少量圓角圖片時(shí)才推薦使用)

UIImageView *imageView = [[UIImageView alloc] init];imageView.layer.cornerRadius = 5.f;imageView.layer.masksToBounds = YES;

總結(jié)

以上是開(kāi)發(fā)過(guò)程常用組件的切圓角方法總結(jié),如果有更好的方法或者文中有不對(duì)的地方,還請(qǐng)指正提出,謝謝。

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洛扎县| 尚义县| 襄城县| 怀仁县| 陵川县| 麦盖提县| 碌曲县| 尼玛县| 巴东县| 高青县| 康平县| 安溪县| 枝江市| 东安县| 梁平县| 宣化县| 象州县| 永春县| 奈曼旗| 兴业县| 农安县| 华池县| 长子县| 安图县| 青州市| 秭归县| 襄汾县| 耿马| 香港 | 淮滨县| 三台县| 绥宁县| 汾西县| 丰县| 博野县| 甘孜县| 中方县| 大名县| 平南县| 潼关县| 双辽市|