iOS11新特性——改善view圆角

在iOS11之前,view展示圆角的处理过程为:

  //Objective-C
    UIView *view0 = [[UIView alloc]initWithFrame:CGRectMake(30, 100, 200, 40)];
    view0.backgroundColor = [UIColor grayColor];

    view0.layer.cornerRadius = 15;
    //view0.clipsToBounds = YES;
    view0.layer.masksToBounds = YES;
    [self.view addSubview:view0];
//swift
let view = UIView()
        view.clipsToBounds = true
        view.layer.cornerRadius = 10
        view.frame = .init(x: 30, y: 100, width: 200, height: 40)
        view.backgroundColor = UIColor.lightGray
        self.view.addSubview(view)

如此,便可以显示一个圆角的view:

image.png

iOS11对圆角功能进行了改善,layer新增了maskedCorners属性:

@property CACornerMask maskedCorners
  CA_AVAILABLE_STARTING (10.13, 11.0, 11.0, 4.0);

CACornerMask是一个结构体类型:

typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
  kCALayerMinXMinYCorner = 1U << 0,
  kCALayerMaxXMinYCorner = 1U << 1,
  kCALayerMinXMaxYCorner = 1U << 2,
  kCALayerMaxXMaxYCorner = 1U << 3,
};

该属性用于设置view哪个角显示圆角:

//Objective-C
if (@available(iOS 11.0, *)) {
        view2.layer.maskedCorners = kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner;
    } else {
        // Fallback on earlier versions
    }
//swift
view2.layer.maskedCorners = [.layerMaxXMaxYCorner,.layerMinXMaxYCorner]

设置该属性便可以指定view圆角显示:

image.png

此外,iOS11中,view圆角的圆角也支持动画:

//Objective-C
[UIView animateWithDuration:2 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        view1.layer.cornerRadius = 0;
    } completion:nil];
//swift
        
        UIViewPropertyAnimator.init(duration: 1.0, curve: .linear) {
            view3.layer.cornerRadius = 0
        }.startAnimation()
iOS11新特性——改善view圆角_第1张图片
311-3.gif

在iOS11之前,圆角是不支持UIView动画的。

你可能感兴趣的:(iOS11新特性——改善view圆角)