简体   繁体   English

Swift 4 - 快速带有两个圆角的图像视图?

[英]Swift 4 - imageview with two rounded corners in swift?

I have a UIImageView inside of a UICollectionView Cell and I wanna make it' s top corners rounded.我在 UICollectionView 单元格中有一个 UIImageView,我想让它的顶角变圆。 I couldn' t solve on my on, any help is appreciated.我无法解决我的问题,任何帮助表示赞赏。

You can see cell has 10px corner radius and I want same effect to be applied on image too.您可以看到单元格的角半径为 10 像素,我也希望在图像上应用相同的效果。

恩德克萨

You can try UIRectCorner Document here您可以在此处尝试UIRectCorner文档

here my custom class AGRoundCornersView这里是我的自定义类AGRoundCornersView

extension UIImageView {
    public func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: bounds,
                                    byRoundingCorners: corners,
                                    cornerRadii: CGSize(width: radius, height: radius))
        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        layer.mask = shape
    }
}

code :代码:

1. Call When View is resized. 1. 调整视图大小时调用。

uiimage.roundCorners([.topLeft, .topRight], radius: 10)

2. Create custom class 2. 创建自定义类

class CustomImageView: UIImageView {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.roundCorners([.topLeft, .topRight], radius: 10)
    }
}

Try this in your UICollectionViewCell在你的 UICollectionViewCell 中试试这个

 override func awakeFromNib() {
        super.awakeFromNib()
        DispatchQueue.main.async {
            self.image.roundCorners([.topRight,.topLeft], radius: 8)
            self.image.layer.masksToBounds = true
        }
    }

Thanks to AshvinGudaliya and Sujewan感谢AshvinGudaliyaSujewan

Following code worked for my collection cell's ImageView Top Left-Right corners以下代码适用于我的集合单元格的 ImageView 左上角

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:TUGBucketCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! TUGBucketCell
        //Rounded corner ImageView
        DispatchQueue.main.async {
            cell.imgVw.roundCorners([.topLeft, .topRight], radius: 10)
            cell.imgVw.layer.masksToBounds = true
        }

        return cell
    }

and the extension is和扩展是

extension UIImageView {
    public func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: bounds,
                                    byRoundingCorners: corners,
                                    cornerRadii: CGSize(width: radius, height: radius))
        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        layer.mask = shape
    }
}

Swift 5斯威夫特 5

In your collectionview cell, put the below two lines of code & you are good to go.在您的 collectionview 单元格中,放入以下两行代码,您就可以开始了。 No need to write any extension:无需编写任何扩展:

cell._imageView.layer.cornerRadius = 10
cell._imageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]

Here corner radius will apply TopLeft & TopRight corners & rest of corners will remain same.这里角半径将应用TopLeft 和 TopRight角,其余角将保持不变。

If you want to apply the corner radius in BottomLeft & BottomRight corners, then maskedCorners should look like the below:如果要在BottomLeft 和BottomRight角应用圆角半径,则maskedCorners应如下所示:

cell._imageView.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]

Hope it helps.希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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