ios - 如何在UIImageView中实现底部自定义曲线和顶部圆角半径?

标签 ios iphone swift uibezierpath cornerradius

我正在从事配置文件设计。我想在图像的底部实现自定义曲线,并将角落 radio 应用于顶部。我已经用 UIBeziarPath 的 RoundCorners 方法试过了。但我没有得到正确的结果。

这是我的尝试:-

1) 我正在使用这种方法来实现我的结果。

//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) 在 vi​​ewDidLoad 方法中。

  override func viewDidLoad() {
        super.viewDidLoad()

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

3) 这是我的结果:-

enter image description here

但是,我想实现这个结果。比如,底部曲线

enter image description here

最佳答案

你可以给imageView添加任意形状的mask

例子:

-(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;
}

结果:

enter image description here

关于ios - 如何在UIImageView中实现底部自定义曲线和顶部圆角半径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983343/

相关文章:

ios - AdMob 给出 fatal error (展开可选时发现 nil)

java - 转发新 session 时出错找不到: Capabilities [{iPhone=Safari, browserName=Safari, deviceVersion=10.3.1, platformName=iOS}]

ios - 用于存储日期的 CoreData 推荐做法

iOS 可设计按钮背景颜色不起作用

iphone - 多个静态库、swizzling 和dispatch_once

swift - SQLite.swift 导致 Operator 不是已知的二元运算符

swift - TableView 内部的 CollectionView

swift - 带有字符串而不是文件的内容拦截器扩展

ios - 如何在基于大小类的通用 Storyboard中为 UItableview 模拟 2 个不同的行高?

iphone - 更改 UISegmentedControl 的布局?