温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

简单理解将uiview切成任意形状

发布时间:2020-07-29 23:40:08 来源:网络 阅读:435 作者:xinji0702 栏目:开发技术

简单理解将uiview切成任意形状把正方形的图片处理为圆形,以前一直没想过,只是好奇,其实一些都是那么的简单

self.p_w_picpathView.layer.borderWidth = 2;
    self.p_w_picpathView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.p_w_picpathView.layer.cornerRadius = CGRectGetHeight(self.p_w_picpathView.bounds) / 2;
    self.p_w_picpathView.clipsToBounds = YES;

简单理解将uiview切成任意形状


教你把UIView切成任意形状

   有时候layer.cornerRadius并不能满足需求,自己实现drawRect又太麻烦,怎么办?

多的不说,直接上代码:


- (void)dwMakeBottomRoundCornerWithRadius:(CGFloat)radius
{
    CGSize size = self.frame.size;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFillColor:[[UIColor whiteColor] CGColor]];
         
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, size.width - radius, size.height);
    CGPathAddArc(path, NULL, size.width-radius, size.height-radius, radius, M_PI/2, 0.0, YES);
    CGPathAddLineToPoint(path, NULL, size.width, 0.0);
    CGPathAddLineToPoint(path, NULL, 0.0, 0.0);
    CGPathAddLineToPoint(path, NULL, 0.0, size.height - radius);
    CGPathAddArc(path, NULL, radius, size.height - radius, radius, M_PI, M_PI/2, YES);
    CGPathCloseSubpath(path);
    [shapeLayer setPath:path];
    CFRelease(path);
    self.layer.mask = shapeLayer;//layer的mask,顾名思义,是种位掩蔽,在shapeLayer的填充区域中,alpha值不为零的部分,self会被绘制;alpha值为零的部分,self不会被绘制,甚至不会响应touch
}
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self dwMakeBottomRoundCornerWithRadius:3.0];
    }
}



这样就能切出一个只有下半部圆角的View,更厉害的是,切去的部分不响应用户点击!

这种方法,只要稍作修改就能实现很多效果,比如一个多边形的按钮,把一张图片切成邮票。

UIView:

简单理解将uiview切成任意形状

layer.mask:

简单理解将uiview切成任意形状

最终绘制:

简单理解将uiview切成任意形状

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI