zoukankan      html  css  js  c++  java
  • <iOS小技巧>UIview指定设置控件圆角

     
    一、用法:
     
    众所周知,设置控件的圆角使用layer.cornerRadius属性即可,但是这样设置成的结果是4个边角都是圆角类型。
     
    利用班赛尔曲线画角:
     
    //利用班赛尔曲线画角
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:(UIRectCornerBottomLeft |UIRectCornerBottomRightcornerRadii:CGSizeMake(1010)];
    CAShapeLayer *shapeLayer = [[CAShapeLayer allocinit];
    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.boundsbyRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeftcorner 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指定位置圆角,你能想到几种方法?感觉那种最好用?哪种最不消耗内存。
     
  • 相关阅读:
    图表算法—有向图
    图表算法—无向图
    搜索算法—哈希表
    红黑树的删除
    搜索算法—红黑树
    搜索算法—二叉搜索树
    排序算法—堆排序
    快速排序改进——3区快速排序(3-way quicksort)
    数论——约数:算数基本定理及推论,欧几里得算法
    数论——乘法逆元(快速幂求法)及模运算
  • 原文地址:https://www.cnblogs.com/firstrate/p/7137866.html
Copyright © 2011-2022 走看看