<iOS小技巧>UIview指定设置控件圆角

 
一、用法:
 
众所周知,设置控件的圆角使用 layer . cornerRadius 属性即可,但是这样设置成的结果是4个边角都是圆角类型。
 
利用班赛尔曲线画角:
 
// 利用班赛尔曲线画角
UIBezierPath  *bezierPath = [ UIBezierPath  bezierPathWithRoundedRect :button. bounds   byRoundingCorners :( UIRectCornerBottomLeft  | UIRectCornerBottomRight cornerRadii : CGSizeMake ( 10 10 )];
CAShapeLayer  *shapeLayer = [[ CAShapeLayer   alloc init ];
shapeLayer. frame  = button. bounds ;
shapeLayer. path  = bezierPath. CGPath ;
button. layer . mask  = shapeLayer;
 
关于设置指定位置控件圆角的枚举:
 
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0, //左上
    UIRectCornerTopRight    = 1 << 1, //右上
    UIRectCornerBottomLeft  = 1 << 2, //左下
    UIRectCornerBottomRight = 1 << 3, //右下
    UIRectCornerAllCorners  = ~0UL    //全角
};
 
 
我的用法如下:
 
        UIBezierPath *maskPathA = [ UIBezierPath  bezierPathWithRoundedRect : _smsCodeTFiled . bounds byRoundingCorners : UIRectCornerBottomLeft | UIRectCornerTopLeft corner Radii : CGSizeMake ( self . smsCodeTFiled . bounds . size . height / 2.0 , self . smsCodeTFiled . bounds . size . height / 2.0 )];
        CAShapeLayer *maskLayerA = [[ CAShapeLayer  alloc ] init ];
        maskLayerA. frame = _smsCodeTFiled . bounds ;
        maskLayerA. path = maskPathA. CGPath ;
        _smsCodeTFiled . layer . mask = maskLayerA;
        _smsCodeTFiled . layer . masksToBounds = YES ;
        [ self . view  bringSubviewToFront : self . smsCodeLb ];
 
二、我的想法
 
这个用法主要用于在左边有圆角,右边没有,或者相反的状况,但是注意一下就是,当设置多个UIView时,要记得不要声明相同的名称,例如   UIBezierPath *maskPath  后面声明其他UIview的时候也这么写   UIBezierPath *maskPath    就会出错了,记得不要重复声明同一个名称的对象就好。如果此方法没有生效,很大可能是Xib文件加了约束导致此方法不能生效。
 
三、思考与行动
 
1.尝试不用纯代码,利用Xib 能否使这某个UIView 指定位置设置圆角?
 
2.尝试通过其他方法,来设置UIVIew指定位置圆角,你能想到几种方法?感觉那种最好用?哪种最不消耗内存。
 

转载于:https://www.cnblogs.com/firstrate/p/7137866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值