美文网首页
iOS开发 | 关于圆角

iOS开发 | 关于圆角

作者: _冇毒 | 来源:发表于2018-02-23 14:13 被阅读0次

本文参考: http://www.baidu.com

一说到圆角, 我们最先想到的可能就是

[view.layer setCornerRadius:3];
[view.layer setMasksToBounds:YES];

但这种方法会造成离屏渲染,对性能影响较大, 设置的少了也能用. 但如果是在tableView上使用的话. 对性能的考验还是很大的. 不推荐使用

  • 先说几个简单的
1. UITextField
  • textField 自身有设置圆角的方法
textField.borderStyle = UITextBorderStyleRoundedRect;
2. UIView(不包括其子类)
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
view.layer.cornerRadius = 3.0f;
// 以下两行,任写一行
view.layer.masksToBounds = NO;
view.clipToBounds = NO;
// 以下两行,千万不要加!
view.layer.masksToBounds = YES;
view.clipToBounds = YES;

*注:UIView 只要设置图层的 cornerRadius 属性即可,如果设置 layer.masksToBounds = YES,会造成不必要的离屏渲染。

3. UITextView
// 与 UIView 类似
UITextView *textView = [[UITextView alloc] init];
textView.layer.cornerRadius = cornerRadius;
4. UILabel
UILabel *label = [[UILabel alloc] init];
// 重点在此!!设置视图的图层背景色,千万不要直接设置 label.backgroundColor
label.layer.backgroundColor = [UIColor grayColor].CGColor;
label.layer.cornerRadius = cornerRadius;
5. UIButton
  • button 能够设置背景图片, 一般单一背景色的button圆角设置可以通过设置背景图片来实现
    背景图片可以是找美工要的, 也可以是自己画的.
/**
*  背景图片绘制方法
*/
+ (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;
}
6. UIImageView

imageView 可以通过图片上的圆角来实现或者使用贝塞尔曲线的方法来实现, 这种方法比较麻烦一点, 但是用的多了就好了

- (UIImageView *)roundedRectImageViewWithCornerRadius:(CGFloat)cornerRadius image:(UIImageView *)imageView 
{
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imageView;
}

还有一种方法 cashapelayer和uibezierpath设置圆角, 也是我比较常用的一种

#import <AVFoundation/AVFoundation.h>

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"icon"];
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
CAShapeLayer * layer = [[CAShapeLayer alloc] init];
layer.frame = imageView.bounds;
layer.path = path.CGPath;
imageView.layer.mask = layer;
[self.imageView addSubview:imageView];

相关文章

  • iOS开发 | 关于圆角

    本文参考: http://www.baidu.com 一说到圆角, 我们最先想到的可能就是 但这种方法会造成离屏...

  • 视图指定位置圆角

    mark:iOS开发之指定UIView的某几个角为圆角ios中设置view固定方向的圆角 iOS View 指定圆...

  • 在StoryBoard中设置圆角,边框宽度等

    在iOS开发中,很多时候我们需要设置圆角,下面就介绍一种用storyBoard开发快速设置圆角的方式,废话不多说,...

  • iOS 圆角问题

    一般在iOS开发的过程中这样设置圆角 亲测在TableView 中一屏60个圆角, iOS 9.0 之后FPS能保...

  • ###iOS开发之UIView的指定角为圆角

    iOS开发之UIView的指定角为圆角 在开发中,当一个UIView需要4个角都是圆角的时候我们可以通过Corne...

  • 关于UIView切圆角的两种方式

    在 iOS App 开发中,切圆角的场景有很多。很多图片或者 UIView 都需要切圆角。 切圆角的方式一般有两种...

  • iOS-UIImageView圆角设置

    iOS开发中图片圆角设置是最常见的需求,圆角符合人类视觉安全体验,让人感觉舒适,设置圆角也是非常简单,有五种方式来...

  • iOS关于圆角

    1.简单的view.cornerRadius这里就不说了缺点:性能消耗大,只能同时定义四个角。2.看看好用的方法使...

  • IOS UIImageview切圆角的两种方式

    UIImageview切圆角 在ios开发中,UIImageview切圆角的方式据我所知有三种,在这里介绍两种我个...

  • UIView,UIButton,UIImageView等设立

    UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法 在iOS开发中,任何可...

网友评论

      本文标题:iOS开发 | 关于圆角

      本文链接:https://www.haomeiwen.com/subject/nlztxftx.html