美文网首页
ios设置控件圆角

ios设置控件圆角

作者: 小傑 | 来源:发表于2016-08-19 12:57 被阅读124次

0.最简单实用,不影响帧数的设置全圆角的方法,是自定义一个类继承于UIimageview,在它上面在加一个image,图片为中间一个透明的圆,四周为所需要底色的颜色,最好还设置下highlightedImage

self.image_icon.frame = CGRectMake(-1, -1, h + 2, h + 2);//尺寸

/**
 *  设置全部圆角(注意:需要首先设置控件的bounds或者frame,否则会加载不出来,因为它需要根据child的尺寸来设置)
 *
 *  @param child 设置圆角的控件
 */
+(void)setUpAllCornerWithChildView:(UIView *)child

{

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:child.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:child.bounds.size];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];

//设置大小

maskLayer.frame = child.bounds;

//设置图形样子

maskLayer.path = maskPath.CGPath;

child.layer.mask = maskLayer;

child.layer.shouldRasterize = YES; //圆角缓存

child.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度

}

2.设置局部圆角

/**
 *  设置圆角
 *
 *  @param rect   控件的尺寸
 *  @param corner 圆角的尺寸
 *  @param radio  圆角位置
 *  @param child  控件
 */
+(void)setUpCornerWithRect:(CGRect )rect withUIRectCorner:(UIRectCorner )corner withSize:(CGSize )radio withView:(UIView *)child
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
    CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer
    masklayer.frame = child.bounds;
    masklayer.path = path.CGPath;//设置路径
    child.layer.mask = masklayer;
    
    child.layer.shouldRasterize = YES; //圆角缓存
    
    child.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
}

3.设置图片圆角

/** 设置圆形图片(放到分类中使用) */
- (UIImage *)cornerWithImage {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    // 获取上下文
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    // 设置圆形
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctr, rect);
    // 裁剪
    CGContextClip(ctr);
    // 将图片画上去
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

相关文章

网友评论

      本文标题:ios设置控件圆角

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