ios - 可调整大小 View 的圆角

标签 ios swift uiview uibezierpath cashapelayer

我一直在使用下面显示的代码来圆化 View 的选定角,但是现在在作为层的可调整大小的 View 上实现这个有困难吗?不会在每次调整 View 大小时更新。

extension UIView {
    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
    }
}

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

有个类似的问题here但它很旧,而且都是用 objC 完成的。

对于可调整大小的 View ,是否有一种快速圆化选定角的方法?

----- 编辑 -----

基本上我拥有的是一个文本表格,我已将其设置为根据文本的大小调整大小。

在大多数情况下我可以只使用:

myTextLabel.layer.cornerRadius = 10

然而,这会处理所有 4 个角。因此,如果我只想舍入前 2 名,那么我需要使用上面的扩展名。现在因为我正在使用 scrollViewDidEndDecelerating 来设置标签的内容(我需要获取 collectionView 中心单元格的 indexPath 以便我可以设置文本标签)

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    GeneralFunctions.getIndexOfCell(sender: self) { (index) in
        self.titleLabel.text = self.partArray[index].title
        self.titleLabel.roundCorners(corners: [.topLeft, .topRight], radius: 10)
        self.descriptionLabel.text = self.partArray[index].description
        self.descriptionLabel.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)
        self.backgroundLabelView.layer.cornerRadius = 16
        self.backgroundLabelView.layoutIfNeeded()
        self.backgroundLabelView.layoutSubviews()
    }

}

在这种情况下使用 viewDidLayoutSubViews 不起作用,因为加速结束和布局之间存在滞后。我尝试在 viewDidLayoutSubViews 中使用相同的代码(不检查中心索引),但结果是一样的。

enter image description here

当我不使用任何圆角时,标签会正确调整大小。

enter image description here

最佳答案

我遇到了类似的问题,我已经通过使用这个函数解决了它

DispatchQueue.main.async(execute: {
    self.roundCorners(.allCorners, radius: 6, borderColor: nil, borderWidth: nil)
})

我在 UITableViewCell 中经常使用这个函数,我的整个代码看起来像

private var didLayoutSubview = false {
    didSet {
        DispatchQueue.main.async(execute: {
            self.roundCorners(.allCorners, radius: 6, borderColor: nil, borderWidth: nil)
        })
    }
}

override func layoutSubviews() {
    if !self.didLayoutSubview {
        self.didLayoutSubview = true
    }
    super.layoutSubviews()
}

所以基本上,在主线程中调用这个函数很有帮助,我在 layoutSubivews 中调用它,因为我认为它是这个地方。

我的函数

func roundCorners(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor?, borderWidth: CGFloat?) {
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))

    let mask = CAShapeLayer()
    mask.frame = self.bounds
    mask.path = path.cgPath
    self.layer.mask = mask

    if borderWidth != nil {
        addBorder(mask, borderWidth: borderWidth!, borderColor: borderColor!)
    }
}

关于ios - 可调整大小 View 的圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42217997/

相关文章:

ios - swift 如何显示除非在到达底部后进一步向上滚动才能看到的页脚?

objective-c - 检查条件不起作用并返回无法识别的选择器

ios - UICollectionViewController Thumbnail ImageView 全屏和滚动手势

objective-c - iPad Kiosk sleep 唤醒

ios - 相当于一个M文件的storyboard是什么

ios - UICollectionViewCell 内 ScrollView 内的缩放图像

ios - swift 中奇怪的泛型行为

swift - 在其他函数中使用 swift func vars

ios - 在 Apple 状态栏上显示 UIView

ios - UIButton 类似于 UIView 的自定义子类的行为