【问题标题】:Swift 4 - imageview with two rounded corners in swift?Swift 4 - 快速有两个圆角的图像视图?
【发布时间】:2018-01-30 10:43:15
【问题描述】:

我在 UICollectionView 单元内有一个 UIImageView,我想让它的顶角变圆。我无法解决我的问题,感谢任何帮助。

您可以看到单元格的角半径为 10px,我也希望在图像上应用相同的效果。

【问题讨论】:

  • 您为图像视图的上角编写的代码是什么?
  • 只要单元格的 clipsToBounds = true 和 imageview 是该单元格的 contentView 的子视图,您就可以使用了(假设您已使用 cornerRadius 获得圆角)
  • @Alladinian clipsToBound=true, self.layer.cornerRadius=10 和 imageview 是一个子视图,但仍然不工作
  • 听起来不错(我的意思是它应该工作)...您是否尝试过“调试视图层次结构”工具来检查您的单元格?另外,如果隐藏图像,单元格顶部是否会出现圆角?
  • @Alladinian 下面的答案工作正常。我从错误的地方调用它..新手错误。

标签: ios swift uicollectionview uiimageview swift4


【解决方案1】:

你可以试试UIRectCornerDocument here

这里是我的自定义类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
    }
}

代码:

1.调整视图大小时调用。

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

2。创建自定义类

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

【讨论】:

  • 调整视图大小时调用。
【解决方案2】:

在你的 UICollectionViewCell 中试试这个

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

【讨论】:

    【解决方案3】:

    斯威夫特 5

    在您的 collectionview 单元格中,输入以下两行代码即可。无需编写任何扩展:

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

    此处的角半径将应用 TopLeft 和 TopRight 角,其余角将保持不变。

    如果您想在 BottomLeft 和 BottomRight 角中应用角半径,则 ma​​skedCorners 应如下所示:

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

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      感谢AshvinGudaliyaSujewan

      以下代码适用于我的集合单元格的 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
          }
      

      扩展名是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多