如何在 Swift 中弯曲 UIViewController 的顶部? [英] How to curve the top of a UIViewController in Swift?

查看:27
本文介绍了如何在 Swift 中弯曲 UIViewController 的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何使用 Swift 仅弯曲 UIViewController 的顶部.

I'm trying to figure out how to curve just the top of the UIViewController using Swift.

我希望它像这张照片中显示的那样弯曲.

I want it curved like it shows in this photo.

推荐答案

您可以使用一些贝塞尔曲线绘制背景,这应该不会太难.如果您还想在该示例的底部重新创建类似的内容,您尤其想这样做.

You could draw the background with some bezier curves which shouldn't be too hard to do. You especially want to do this if you want to recreate something like into the bottom of that example as well.

这是您可以使用的自定义视图.它只是覆盖绘图并使其背景具有圆顶的贝塞尔曲线

Here's a custom view you can use. it just overrides the drawing and makes its background with a bezier path that has a rounded top

您可以使用我插入的静态值调整高度和曲线.

You can just adjust the height and curve with the static values I inserted.

class CurvedView: UIView {
    override func drawRect(rect: CGRect) {

        let y:CGFloat = 20
        let curveTo:CGFloat = 0

        let myBezier = UIBezierPath()
        myBezier.moveToPoint(CGPoint(x: 0, y: y))
        myBezier.addQuadCurveToPoint(CGPoint(x: rect.width, y: y), controlPoint: CGPoint(x: rect.width / 2, y: curveTo))
        myBezier.addLineToPoint(CGPoint(x: rect.width, y: rect.height))
        myBezier.addLineToPoint(CGPoint(x: 0, y: rect.height))
        myBezier.closePath()
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 4.0)
        UIColor.whiteColor().setFill()
        myBezier.fill()
    }
}

另一种方法是制作一个比屏幕大得多的白色视图.我猜是屏幕宽度的 3 倍.然后将拐角半径设置为其宽度的一半,使其变圆.

Another way would be to just make a white view that is much bigger than the screen. I'm guessing 3 times the screen width. Then just set the corner radius to half of its width making it round.

这篇关于如何在 Swift 中弯曲 UIViewController 的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆