【问题标题】:Round Corners of resizable view可调整大小视图的圆角
【发布时间】:2017-02-14 04:41:57
【问题描述】:

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

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 中完成的。

有没有办法在 swift 中对选定的角进行圆角调整以调整视图大小?

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

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

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

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 中使用相同的代码(不检查中心索引),但结果是一样的。

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

【问题讨论】:

  • 不检查链接的问题(我期待答案),你能解释一下确切的问题吗?从 Obj-C 到 Swift 的转换非常简单。你试过什么?你的问题比较模糊。
  • 特别是在你提到的问题中,特别注意在viewDidLayoutSubviews中做resize

标签: ios swift uiview uibezierpath cashapelayer


【解决方案1】:

我也遇到过类似的问题,我用这个函数解决了

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

【讨论】:

  • 您是否对 uiView 进行了子类化以实现此目的,或者您是否正在为其提供 uiView?
猜你喜欢
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2011-09-14
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多