简体   繁体   中英

How to Achieve bottom custom Curve and top Corner Radius in UIImageView?

I am working on Profile Design. I want to achieve custom curve on bottom side of image and apply corner radios to Top side. I have tried it with RoundCorners method of UIBeziarPath. but i didn't got proper result.

Here is my try :-

1) I am using this method to achieve my result.

//MARK : - Extension set UIImageView Corner Radius
extension UIImageView {

    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {

        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask

    }
}

2) In viewDidLoad Method.

  override func viewDidLoad() {
        super.viewDidLoad()

        self.imgView.roundCorners([.bottomLeft, .bottomRight], radius: 36)
        self.imgView.layer.cornerRadius = 10
}

3) Here is my Result :-

在此处输入图片说明

But, I want to achieve this result. Like, bottom Curve

在此处输入图片说明

You can add mask with any shape to imageView

Example:

-(void) viewDidAppear:(BOOL)animated {
self.view.backgroundColor = [UIColor lightGrayColor];
UIImageView* yourView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
yourView.center = self.view.center;
yourView.image = [UIImage imageNamed:@"testImg"];

yourView.clipsToBounds = YES;
[self.view addSubview: yourView];

CGFloat roundedHeight = yourView.frame.size.height * 0.2;

CGFloat bottomOvalHeight = roundedHeight * 2;

//First part of mask - oval in the bottom
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,
                                                                           yourView.frame.size.height - bottomOvalHeight, yourView.frame.size.width,
                                                                           bottomOvalHeight)];

//Second part of mask - rectangle from top to the middle of the oval
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:CGRectMake(0,
                                                                     0,
                                                                     yourView.frame.size.width,
                                                                     yourView.frame.size.height - roundedHeight)];
//Make one mask from two parts
[rectPath appendPath:ovalPath];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = rectPath.CGPath;
yourView.layer.mask  = maskLayer;
}

Result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM