【问题标题】:How to set UIView in semi circle in Swift 5如何在 Swift 5 中将 UIView 设置为半圆形
【发布时间】:2021-05-28 17:53:34
【问题描述】:

我想将我的 UIView 裁剪为半圆形,这将支持所有 iPhone 设备。在这里,我们不能设置高度和宽度来修复。我正在使用乘数。

这是我检查的一些 SO 答案。

How to crop the UIView as semi circle?

Draw a semi-circle button iOS

它可以工作,但不是用乘数来修复高度和宽度,我的目标是通用设备。 下面是半圆形 UIView 的图片。

提前致谢。

【问题讨论】:

  • 如果这件事是由 UIImageView 或 UIButton 之类的任何其他对象完成的,则无需使用 UIView 即可。完成后我会更新问题。谢谢!

标签: ios swift uiview swift5


【解决方案1】:

我刚刚修改了this链接码的圈码。问题是在原始代码中,当再次调用 layoutSubviews 方法时,它使用了初始帧 semiCirleLayer == nil 这种情况被阻止更新贝塞尔路径。

class SemiCirleView: UIView {
    
    var semiCirleLayer: CAShapeLayer = CAShapeLayer()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let arcCenter = CGPoint(x: bounds.size.width / 2, y: bounds.size.height)
        let circleRadius = bounds.size.width / 2
        let circlePath = UIBezierPath(arcCenter: arcCenter, radius: circleRadius, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 2, clockwise: true)
        
        semiCirleLayer.path = circlePath.cgPath
        semiCirleLayer.fillColor = UIColor.red.cgColor
        
        semiCirleLayer.name = "RedCircleLayer"
        
        if !(layer.sublayers?.contains(where: {$0.name == "RedCircleLayer"}) ?? false) {
            layer.addSublayer(semiCirleLayer)
        }
        
        // Make the view color transparent
        backgroundColor = UIColor.clear
    }
}

【讨论】:

  • 感谢您的回答!请在 11Pro 中检查它有一些尾随空间,我将 UIView 尾随设置为零。
  • @YogeshPatel 检查我的 ss 在所有尺寸下都能正常工作。你能添加你的代码和截图吗?
  • 糟糕的是,我的代码覆盖了您的代码,我完美地检查了它,现在它工作正常。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 2019-10-17
  • 2017-10-30
  • 1970-01-01
相关资源
最近更新 更多