divided(atDistance: from fromEdge: )

偶然看喵神的《swifter》对元组的讲解的时候,有一个divided方法,看方法的返回结果实在想不明白,就写了个demo研究了一下,也制作了一些图片

方法介绍

public func divided(atDistance: CGFloat,
                 from fromEdge: CGRectEdge) 
          -> (slice: CGRect, remainder: CGRect)

按照书中给介绍,第一个返回值是定义为small,第二个返回值定义为large,第二个参数是个枚举

public enum CGRectEdge : UInt32 {

    case minXEdge

    case minYEdge

    case maxXEdge

    case maxYEdge
}

通过第四个枚举的参数变化调用,结果如下

static func tupleTest() {
        let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
        print(rect.divided(atDistance: 20, from: .minXEdge))
        print(rect.divided(atDistance: 20, from: .minYEdge))
        print(rect.divided(atDistance: 20, from: .maxXEdge))
        print(rect.divided(atDistance: 20, from: .maxYEdge))
    }

// 控制台打印结果
(slice: (0.0, 0.0, 20.0, 100.0), remainder: (20.0, 0.0, 80.0, 100.0))
(slice: (0.0, 0.0, 100.0, 20.0), remainder: (0.0, 20.0, 100.0, 80.0))
(slice: (80.0, 0.0, 20.0, 100.0), remainder: (0.0, 0.0, 80.0, 100.0))
(slice: (0.0, 80.0, 100.0, 20.0), remainder: (0.0, 0.0, 100.0, 80.0))

然后我绘制了一张图,其中褐色是以slice为frame,红色是以remainder为fram


divided(atDistance: from fromEdge: )_第1张图片
minX.png
divided(atDistance: from fromEdge: )_第2张图片
minY.png
divided(atDistance: from fromEdge: )_第3张图片
maxX.png
divided(atDistance: from fromEdge: )_第4张图片
maxY.png

解析 min开头理解为最小的偏移量,max开头的理解为长度减去偏移量剩余的值

你可能感兴趣的:(divided(atDistance: from fromEdge: ))