UIBezierPath无法正确呈现

UIBezierPath无法正确呈现

本文介绍了UIBezierPath无法正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带uiview的tablviewcell.根据某种逻辑,我更改了背景颜色,并使左右角变圆了.

I have a tablviewcell which has uiview in it.Based on some logic I change the background color and make the left and right corner rounded.

我通过cellForRowat indexPath函数使这些视图变成圆角.

I make these view corner round from cellForRowat indexPath function.

这是我的扩展名.

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

以及我的使用方式

cell?.myCustomView.roundCorners(corners: [.bottomRight,.bottomLeft], radius: 10.0)

当iPhone的宽度为375时,它可以正常工作,但无法更新宽度大于375的设备.

Its working fine when width of iphones is 375,but it fails to update for device with width greater than 375.

滚动表格视图后,它会再次正确地拉回到所需的宽度.

And after scrolling the tableview, it again stretches back correctly to the desired width.

如何解决这个问题?

推荐答案

您要在视图更改大小时更新路径.在cellForRowAt中,该单元尚未通过自动布局完全布局.

You want to update the path when the view changes size. In cellForRowAt the cell has not yet been fully laid-out by auto-layout.

所以...

为圆角"视图创建一个UIView子类(简单示例):

Create a UIView subclass for your "rounded corners" view (simple example):

class RoundedCornersView: UIView {

    var corners: UIRectCorner = UIRectCorner()
    var radius: CGFloat = 0.0

    override func layoutSubviews() {
        super.layoutSubviews()
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        layer.mask = mask
    }
}

现在,无论何时视图更改大小(例如,首次使用或设备旋转时),视图都会自动更新路径.

Now, anytime the view changes size - such as on first use or device rotation, for example - the view will automatically update the path.

这是在表格单元格中使用它的方式.在情节提要中,将背景视图"的类设置为RoundedCornersView

Here is how you would use it in a table cell. In Storyboard, set the class of the "background view" to RoundedCornersView

class RoundedCornersView: UIView {

    var corners: UIRectCorner = UIRectCorner()
    var radius: CGFloat = 0.0

    override func layoutSubviews() {
        super.layoutSubviews()
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        layer.mask = mask
    }
}

class MyTestCell: UITableViewCell {

    @IBOutlet var myCustomView: RoundedCornersView!

}

然后,在cellForRowAt中:

        let cell = tableView.dequeueReusableCell(withIdentifier: "MyTestCell", for: indexPath) as! MyTestCell

        if shouldBeRounded {
            cell.myCustomView.corners = [.bottomRight, .bottomLeft]
            cell.myCustomView.radius = 10.0
            cell.myCustomView.backgroundColor = .green
        } else {
            cell.myCustomView.corners = []
            cell.myCustomView.radius = 0.0
            cell.myCustomView.backgroundColor = .white
        }

        return cell

这篇关于UIBezierPath无法正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:12