ios - 如何使用 swift3 弯曲 UIView 的顶部?

标签 ios swift uiview

我经历了How to curve the top of a UIView Controller in Swift

并发表看法。它附在下面。我想从我的 View 中删除该背景色。我在 Storyboard 中制作了一个 uiview 并为该 View 分配了一个自定义类。

want to remove the black color

我的自定义类是

class curvedView: UIView {

override func draw(_ rect: CGRect) {

    let color = UIColor(rgb: 0x285387)
    let y:CGFloat = 0
   // let curveTo:CGFloat = 50

    let myBezier = UIBezierPath()
    myBezier.move(to: CGPoint(x: 0, y: y))

    myBezier.addQuadCurve(to: CGPoint(x: rect.width, y: y), controlPoint: CGPoint(x: rect.width / 2, y: rect.height / 3))
    myBezier.addLine(to: CGPoint(x: rect.width, y: rect.height))
    myBezier.addLine(to: CGPoint(x: 0, y: rect.height))
    myBezier.close()
    let context = UIGraphicsGetCurrentContext()
    context!.setLineWidth(2.0)

    color.setFill()
    myBezier.fill()
}}

extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
    assert(red >= 0 && red <= 255, "Invalid red component")
    assert(green >= 0 && green <= 255, "Invalid green component")
    assert(blue >= 0 && blue <= 255, "Invalid blue component")

    self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}

convenience init(rgb: Int) {
    self.init(
        red: (rgb >> 16) & 0x28,
        green: (rgb >> 8) & 0x53,
        blue: rgb & 0x87
    )
}}

最佳答案

我们只需为该自定义 View 提供 backgroundColor。

喜欢:

class curvedView: UIView {

    override func layoutSubviews() {
        self.backgroundColor = .clear
     }

    override func draw(_ rect: CGRect) {
        let color = UIColor(rgb: 0x285387)
        let y:CGFloat = 0
        let myBezier = UIBezierPath()
        myBezier.move(to: CGPoint(x: 0, y: y))
        myBezier.addQuadCurve(to: CGPoint(x: rect.width, y: y), controlPoint: CGPoint(x: rect.width / 2, y: rect.height / 3))
        myBezier.addLine(to: CGPoint(x: rect.width, y: rect.height))
        myBezier.addLine(to: CGPoint(x: 0, y: rect.height))
        myBezier.close()
        color.setFill()
        myBezier.fill()
    }
}

或者最好在初始化时给出:

class curvedView: UIView {

    override func draw(_ rect: CGRect) {
        let color = UIColor(rgb: 0x285387)
        let y:CGFloat = 0
        let myBezier = UIBezierPath()
        myBezier.move(to: CGPoint(x: 0, y: y))
        myBezier.addQuadCurve(to: CGPoint(x: rect.width, y: y), controlPoint: CGPoint(x: rect.width / 2, y: rect.height / 3))
        myBezier.addLine(to: CGPoint(x: rect.width, y: rect.height))
        myBezier.addLine(to: CGPoint(x: 0, y: rect.height))
        myBezier.close()
        color.setFill()
        myBezier.fill()

    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.backgroundColor = UIColor.clear

    }
}

关于ios - 如何使用 swift3 弯曲 UIView 的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44494966/

相关文章:

iOS : Crop video weird green line left and bottom side in video

ios - 如何让 UITableView 忽略 UINavigationController

ios - UICollectionViewCompositionalLayout 最小项目大小

swift - 设置类的值,例如 UIButton

objective-c - 使用 InterfaceBuilder 配置的自定义 UIView

ios - 回到 Swift 2 中的 Facebook 问题

iOS 应用推送通知未显示在 Beta 测试用户的设备中

ios - 从 NSUserDefaults 检索自定义对象

ios - Realm :无法将主键属性 'id' 设置为现有值

ios - 如何在子类化 View 时减少代码冗余?