xcode - 具有 LeftTop 圆角和 RightTop 角角的 MapView

标签 xcode swift

这就是我目前所拥有的:

    let indentTopToLeft: CGFloat    = 50
    let indentTopToBottom: CGFloat  = 100

    let path: CGMutablePathRef = CGPathCreateMutable()
    CGPathMoveToPoint(path, nil, 3, 0)
    CGPathAddLineToPoint(path, nil, 200-indentTopToLeft, 0)
    CGPathAddLineToPoint(path, nil, 200, indentTopToBottom)
    CGPathAddLineToPoint(path, nil, 200 , 150)//4
    CGPathAddLineToPoint(path, nil, 0, 150)
    CGPathAddLineToPoint(path, nil, 0, 3)
    //The rounded goes here. But I don't know how.
    CGPathAddLineToPoint(path, nil, 3, 0)
    CGPathCloseSubpath(path)

    let maskLayer = CAShapeLayer()
    maskLayer.path = path
    self.mapView.layer.mask = maskLayer

enter image description here

现在我需要在这两点之间做一个圆角,但我不知道怎么做,请帮忙。

CGPathAddLineToPoint(path, nil, 0, 3)
//HERE
CGPathAddLineToPoint(path, nil, 3, 0)

最佳答案

有很多方法可以实现这一点,但最直接的可能是使用 CGPathAddArc 函数。请注意,这样做会将您留在点 0,3,因此不需要向该点添加一行的代码。我希望修改后的绘图代码看起来像这样:

let indentTopToLeft: CGFloat     = 50
let indentTopToBottom: CGFloat   = 100
let topLeftCornerRadius: CGFloat = 3

let path: CGMutablePathRef = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, topLeftCornerRadius, 0)
CGPathAddLineToPoint(path, nil, 200-indentTopToLeft, 0)
CGPathAddLineToPoint(path, nil, 200, indentTopToBottom)
CGPathAddLineToPoint(path, nil, 200 , 150)//4
CGPathAddLineToPoint(path, nil, 0, 150)
CGPathAddLineToPoint(path, nil, 0, topLeftCornerRadius)
CGPathAddArc(path, nil, topLeftCornerRadius, topLeftCornerRadius, topLeftCornerRadius, CGFloat(M_PI + M_PI), CGFloat(M_PI + M_PI_2), false)
CGPathCloseSubpath(path)

CGPathAddArc 的参数都有详细的文档;在 nil 之后,我们有圆弧中心点的 x 和 y 位置、圆弧半径、起始角度和结束角度(以弧度为单位),然后是一个指示顺时针还是逆时针绘制的值。值得注意的是,我总是发现顺时针值以及开始和结束角度不是我所期望的 - 这可能是因为 CoreGraphicsUIKit(左上角),但我不能肯定地解释它。

关于xcode - 具有 LeftTop 圆角和 RightTop 角角的 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32894227/

相关文章:

json - 'NSInvalidArgumentException',原因 : 'Invalid number value (infinite) in JSON write'

SwiftUI ListView 没有访问 ForEach 循环

ios - Xcode、 swift : How to center popover box

iphone - 阵列清理方法错误

iphone - FB 返回崩溃应用程序 xcode ios iphone

iOS App Update 在迁移多个数据库版本时删除数据库

swift - 在 Swift 2.2 中,如何合并 for 循环语句?

ios - UITableview 的动态高度

xcode - 如何在 Xcode 中使用 LLDB 调试时更改变量值?

cocoa - 如何在单击我的 NIB 文件时弹出 Xcode Interface Builder?