网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

iOS中如何切圆角

这篇文章给大家分享的是有关iOS中如何切圆角的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

成都创新互联是一家集网站建设,盐都企业网站建设,盐都品牌网站建设,网站定制,盐都网站建设报价,网络营销,网络优化,盐都网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

前言

圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受。但很多人并不清楚如何设置圆角的正确方式和原理。

iOS 客户端开发中,经常碰到圆角视图的需求,本文简单总结一下 UIView 及其子类的一些切圆角方法,并且保证避免出现离屏渲染。

UIView(不包括其子类)

UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blackColor];
view.layer.cornerRadius = 3.f;
// 以下两行,任写一行
view.layer.masksToBounds = NO;
view.clipToBounds = NO;
// 以下两行,千万不要加!
view.layer.masksToBounds = YES;
view.clipToBounds = YES;

注意点:UIView 只要设置图层的 cornerRadius 属性即可(不明白的话,可以看看官方文档里对 cornerRadius 的描述),如果设置 layer.masksToBounds = YES ,会造成不必要的离屏渲染。

文本类视图

UITextField

UITextField有两种实现方法

// 天然支持设置圆角边框
UITextField *textField = [[UITextField alloc] init];
textField.borderStyle = UITextBorderStyleRoundedRect;
// 与 UIView 类似
UITextField *textField = [[UITextField alloc] init];
textField.layer.cornerRadius = cornerRadius;

UITextView

// 与 UIView 类似
UITextView *textView = [[UITextView alloc] init];
textView.layer.cornerRadius = cornerRadius;

UILabel

UILabel *label = [[UILabel alloc] init];
// 重点在此!!设置视图的图层背景色,千万不要直接设置 label.backgroundColor
label.layer.backgroundColor = [UIColor grayColor].CGColor;
label.layer.cornerRadius = cornerRadius;

其它

UIButton

说明:UIButton 的背景图片,如果是复杂的图片,可以依靠 UI 切图来实现。如果是简单的纯色背景图片,可以利用代码绘制带圆角的图片。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 设置 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;
 // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数是屏幕密度
 UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
 [view.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return image;
}

UIImageView

UIImageView 有四种方式实现圆角:

截取图片方式(性能较好,基本不掉帧)

@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

贝塞尔曲线切割圆角(不推荐,掉帧严重)

- (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;
}

绘制四个角的遮罩(使用场景受限)

在 UIImageView 上添加一个四个角有内容,其它部分是透明的视图,只对 UIImageView 圆角部分进行遮挡。但要保证被遮挡的部分背景色要与周围背景相同,避免穿帮。所以当 UIImageView 处于一个复杂的背景时,是不适合使用这个方法的。

最不推荐做法(当一个页面只有少量圆角图片时才推荐使用)

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

感谢各位的阅读!关于“iOS中如何切圆角”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


网站栏目:iOS中如何切圆角
文章分享:http://bjjierui.cn/article/pdsisj.html

其他资讯