Rounded UIView using CALayers - only some corners - How?

后端 未结 14 2016
南方客
南方客 2020-11-22 13:53

In my application - there are four buttons named as follows:

  • Top - left
  • Bottom - left
  • Top - right
  • Bottom - right

Abov

相关标签:
14条回答
  • 2020-11-22 14:05

    Thanks for sharing. Here I'd like to share the solution on swift 2.0 for further reference on this issue. (to conform the UIRectCorner's protocol)

    let mp = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.bottomLeft, .TopLeft], cornerRadii: CGSize(width: 10, height: 10))
    let ml = CAShapeLayer()
    ml.frame = self.bounds
    ml.path = mp.CGPath
    self.layer.mask = ml
    
    0 讨论(0)
  • 2020-11-22 14:11

    Half a decade late, but I think the current way people do this isn't 100% right. Many people have had the issue that using the UIBezierPath + CAShapeLayer method interferes with Auto-layout, especially when it is set on the Storyboard. No answers go over this, so I decided to add my own.

    There is a very easy way to circumvent that: Draw the rounded corners in the drawRect(rect: CGRect) function.

    For example, if I wanted top rounded corners for a UIView, I'd subclass UIView and then use that subclass wherever appropriate.

    import UIKit
    
    class TopRoundedView: UIView {
    
        override func drawRect(rect: CGRect) {
            super.drawRect(rect)
    
            var maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: UIRectCorner.TopLeft | UIRectCorner.TopRight, cornerRadii: CGSizeMake(5.0, 5.0))
    
            var maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds
            maskLayer.path = maskPath.CGPath
    
            self.layer.mask = maskLayer
        }
    }
    

    This is the best way to conquer the issue and doesn't take any time at all to adapt to.

    0 讨论(0)
  • 2020-11-22 14:13

    Wrapping up Stuart's answer, you can have rounding corner method as the following:

    @implementation UIView (RoundCorners)
    
    - (void)applyRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
    
        self.layer.mask = maskLayer;
    }
    
    @end
    

    So to apply rounding corner, you simply do:

    [self.imageView applyRoundCorners:UIRectCornerTopRight|UIRectCornerTopLeft radius:10];
    
    0 讨论(0)
  • 2020-11-22 14:14

    See this related question. You'll have to draw your own rectangle to a CGPath with some rounded corners, add the CGPath to your CGContext and then clip to it using CGContextClip.

    You can also draw the rounded rect with alpha values to an image and then use that image to create a new layer which you set as your layer's mask property (see Apple's documentation).

    0 讨论(0)
  • 2020-11-22 14:15

    I have used this code in many places in my code and it works 100% correctly. You can change any corder by changed one property "byRoundingCorners:UIRectCornerBottomLeft"

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(10.0, 10.0)];
    
                    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
                    maskLayer.frame = view.bounds;
                    maskLayer.path = maskPath.CGPath;
                    view.layer.mask = maskLayer;
                    [maskLayer release];
    
    0 讨论(0)
  • 2020-11-22 14:16

    In iOS 11, we can now round some corners only

    let view = UIView()
    
    view.clipsToBounds = true
    view.layer.cornerRadius = 8
    view.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
    
    0 讨论(0)
提交回复
热议问题