【问题标题】:Why is corner radius applied only to top left and right corner for a custom view?为什么角半径仅应用于自定义视图的左上角和右上角?
【发布时间】:2018-06-01 12:15:33
【问题描述】:

我有我的自定义视图,我将其用作加载栏。它有一个不透明度为 0.1 的底层和一个不透明度为 1 的顶层。

设置如下:

override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }

    func configure() {
        animation.delegate = self
        bottomProgressBar.strokeColor = UIColor.white.cgColor
        bottomProgressBar.opacity = 0.1
        bottomProgressBar.lineWidth = self.frame.height
        bottomProgressBar.strokeStart = 0
        bottomProgressBar.strokeEnd = 1
        self.layer.addSublayer(bottomProgressBar)

        topProgressBar.strokeColor = UIColor.white.cgColor
        topProgressBar.lineWidth = self.frame.height
        topProgressBar.strokeStart = 0
        topProgressBar.strokeEnd = 1
        self.layer.addSublayer(topProgressBar)
        animate(fromValue: 0, toValue: 0)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: self.frame.width, y: 0))

        bottomProgressBar.lineWidth = self.frame.height // Update lineWidth
        topProgressBar.lineWidth = self.frame.height    // Update lineWidth

        bottomProgressBar.path = path.cgPath
        topProgressBar.path = path.cgPath

        self.clipsToBounds = true
        self.layer.cornerRadius = 4
    }

现在我也想应用圆角半径,所以我在layoutSubviews的底部添加了代码:

self.clipsToBounds = true
self.layer.cornerRadius = 4

这仅将角半径应用于左上角和右上部分,就是这样。我不明白为什么。

我还尝试将其设置在configure 方法中,用于bottomProgressBartopProgressBar 层,但根本没有用。

有人能指出这里的问题吗?

编辑:

这是完整的课程代码:

class ProgressBar: UIView {

    var bottomProgressBar = CAShapeLayer()
    var topProgressBar = CAShapeLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }

    func configure() {
        bottomProgressBar.strokeColor = UIColor.white.cgColor
        bottomProgressBar.opacity = 0.1
        bottomProgressBar.lineWidth = self.frame.height
        bottomProgressBar.strokeStart = 0
        bottomProgressBar.strokeEnd = 1
        self.layer.addSublayer(bottomProgressBar)

        topProgressBar.strokeColor = UIColor.white.cgColor
        topProgressBar.lineWidth = self.frame.height
        topProgressBar.strokeStart = 0
        topProgressBar.strokeEnd = 1
        self.layer.addSublayer(topProgressBar)

        animate(toValue: 1)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: self.frame.width, y: 0))

        bottomProgressBar.lineWidth = self.frame.height // Update lineWidth
        topProgressBar.lineWidth = self.frame.height    // Update lineWidth

        bottomProgressBar.path = path.cgPath
        topProgressBar.path = path.cgPath
    }

    func animate(toValue: Double) {
        let animation = CABasicAnimation(keyPath: "strokeEnd")

        animation.duration = 1.0
        animation.fromValue = 0
        animation.toValue = toValue

        topProgressBar.strokeEnd = CGFloat(toValue)
        topProgressBar.animation(forKey: "animate")
    }
}

编辑 2:

添加了更多关于正在发生的事情的屏幕截图:

这没有应用圆角半径 - 简单矩形

这就是我应用圆角半径时发生的情况 - 下半部分被切掉(矩形显然是半切)

这就是我想要的 - 具有四个角的圆角半径的简单矩形:

【问题讨论】:

  • 你设置没有框架的图层,在init中做动画,这是一个混乱的视图
  • init 中的动画只是为了有一个默认值。仍然没有解释圆角半径问题。
  • 发布完整课程 - 您显示的代码不能按原样使用,因此很难尝试帮助找出问题所在。
  • @DonMag:完成。我添加了 ProgressBar 类的代码。
  • @Kobe - 看起来您只是将一条线叠加在另一条线上...您能展示一张您想要它的外观的图片吗?

标签: ios swift calayer


【解决方案1】:

编辑:明确最终目标后,这里有一个更简单的方法。您可以直接在 Playground 页面中运行它...

import UIKit
import PlaygroundSupport

class ProgressBar: UIView {

    var progressView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        return v
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }

    func configure() {
        backgroundColor = UIColor(white: 1.0, alpha: 0.1)
        layer.cornerRadius = 8.0
        clipsToBounds = true

        progressView.frame = bounds
        progressView.frame.size.width = 0.0
        addSubview(progressView)
    }

    func animate(_ pct: CGFloat) -> Void {
        UIView.animate(withDuration: 1.0, animations: {
            self.progressView.frame.size.width = self.bounds.size.width * pct
        })
    }

}

class MyViewController : UIViewController {

    var theButton: UIButton = {
        let b = UIButton()
        b.setTitle("Tap Me", for: .normal)
        b.translatesAutoresizingMaskIntoConstraints = false
        b.backgroundColor = .red
        return b
    }()

    var myProgressBar = ProgressBar(frame: CGRect(x: 40, y: 300, width: 300, height: 30))

    // on button tap, set the progress bar to 80%
    @objc func didTap(_ sender: Any?) -> Void {
        myProgressBar.animate(0.8)
    }

    override func loadView() {
        let view = UIView()
        self.view = view
        view.backgroundColor = UIColor.purple

        view.addSubview(theButton)
        // constrain button to Top: 32 and centerX
        NSLayoutConstraint.activate([
            theButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 32.0),
            theButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0),
            ])

        // add an action for the button tap
        theButton.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)

        view.addSubview(myProgressBar)

    }

}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

点击按钮将使“进度条”从零变为 80%

结果:


原答案:

首先:您正在添加一条从 0,0width,0 的“线”...并且您将其 lineWidth 设置为视图的高度。

这会给你这个结果:

黄色矩形是您的视图,白色矩形是您的“线”。如您所见,线的中心沿着视图的顶部延伸,因此线的上半部分实际上是外部视图的框架。因此,当您圆角视图时,看起来好像只有顶角被圆角。如果您为视图设置背景颜色,您会看到底角也被圆角化,但那里没有“线”。

如果这是你真正想要的(顶栏是白色,底栏是绿色):

您想将线宽设置为视图高度的 1/2,并将线中心设置为 1/4 和 3/4:

class ProgressBar: UIView {

    var bottomProgressBar = CAShapeLayer()
    var topProgressBar = CAShapeLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }

    func configure() {
        bottomProgressBar.strokeColor = UIColor.green.cgColor
        bottomProgressBar.opacity = 1
        bottomProgressBar.lineWidth = self.frame.height
        bottomProgressBar.strokeStart = 0
        bottomProgressBar.strokeEnd = 1
        self.layer.addSublayer(bottomProgressBar)

        topProgressBar.strokeColor = UIColor.white.cgColor
        topProgressBar.lineWidth = self.frame.height
        topProgressBar.strokeStart = 0
        topProgressBar.strokeEnd = 1
        self.layer.addSublayer(topProgressBar)

        //      animate(toValue: 1)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        var path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: self.frame.height * 0.25))
        path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height * 0.25))

        topProgressBar.path = path.cgPath

        path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: self.frame.height * 0.75))
        path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height * 0.75))

        bottomProgressBar.path = path.cgPath

        bottomProgressBar.lineWidth = self.frame.height / 2.0 // Update lineWidth
        topProgressBar.lineWidth = self.frame.height / 2.0    // Update lineWidth

        self.clipsToBounds = true
        self.layer.cornerRadius = 8

    }

    func animate(toValue: Double) {
        let animation = CABasicAnimation(keyPath: "strokeEnd")

        animation.duration = 3.0
        animation.fromValue = 0
        animation.toValue = toValue

        topProgressBar.strokeEnd = CGFloat(toValue)
        topProgressBar.animation(forKey: "animate")
    }
}

当然,如果你想要的只是一个圆角矩形,那么你不需要任何一个子层......只需创建一个白色背景的视图,并设置其层的角半径...... .

【讨论】:

  • 谢谢。很好的解释。嗯,我想要的是是的,只是一个带圆角的简单矩形,但我还需要底层,因为它是一个进度条,它从 0 到 1,所以当它的值是 0.5 时,然后是视图的前半部分(左侧)应为白色,下半部分应显示底层(不透明度为 01。)。所以就像一个进度条。在我的示例中,所有圆角的视图的值为 1,因此底层是隐藏的。
  • 啊...好吧,你可以让它更简单一些...背景颜色为不透明度 0.1 的 UIView,添加不透明度为 1.0 的子视图...为子视图的宽度设置动画。
  • 好的,我想我会试试这个简单的解决方案
  • @Kobe - 我用一个简单的例子编辑了我的答案,使用带有子视图的视图......不是“完成的代码”,而是一个不错的起点。
猜你喜欢
  • 2017-01-18
  • 1970-01-01
  • 2016-06-08
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
相关资源
最近更新 更多