Swift UITableView 之字形边框

swi*_*v25 2 border layer uitableview ios swift

我正在尝试为 UITableView 实现这一方面:https ://www.dropbox.com/s/bcp86myyjgek1kt/Screenshot%202016-11-04%2014.04.14.png ? dl =0我被卡住了。
我遵循了 Atul Manwar 的回答:

    func applyZigZagEffect(givenView: UIView) {
    let width = givenView.frame.size.width
    let height = givenView.frame.size.height

    let givenFrame = givenView.frame
    let zigZagWidth = CGFloat(7)
    let zigZagHeight = CGFloat(5)
    let yInitial = height-zigZagHeight

    var zigZagPath = UIBezierPath()
    zigZagPath.moveToPoint(CGPointMake(0, 0))
    zigZagPath.addLineToPoint(CGPointMake(0, yInitial))

    var slope = -1
    var x = CGFloat(0)
    var i = 0
    while x < width {
        x = zigZagWidth * CGFloat(i)
        let p = zigZagHeight * CGFloat(slope)
        let y = yInitial + p
        let point = CGPointMake(x, y)
        zigZagPath.addLineToPoint(point)
        slope = slope*(-1)
        i++
    }
    zigZagPath.addLineToPoint(CGPointMake(width, 0))

    var shapeLayer = CAShapeLayer()
    shapeLayer.path = zigZagPath.CGPath
    givenView.layer.mask = shapeLayer
}  
Run Code Online (Sandbox Code Playgroud)

结果不是我正在寻找的结果,因为我只获得了底部边框:使用 Atul 的答案实现,我不知道如何为两个边框(底部和顶部)更改它。

尝试使用图像,但没有正确缩放,我发现这个解决方案更好,但我无法产生顶部边框的效果。
谢谢!

Rei*_*ian 6

我一直在研究你的问题,这是我的结果,使用这个代码,

在此处输入图片说明

func applyZigZagEffect(givenView: UIView) {
    let width = givenView.frame.size.width
    let height = givenView.frame.size.height
    
    let givenFrame = givenView.frame
    let zigZagWidth = CGFloat(7)
    let zigZagHeight = CGFloat(5)
    var yInitial = height-zigZagHeight
    
    var zigZagPath = UIBezierPath(rect: givenFrame)
    zigZagPath.move(to: CGPoint(x:0, y:0))
    zigZagPath.addLine(to: CGPoint(x:0, y:yInitial))
    
    var slope = -1
    var x = CGFloat(0)
    var i = 0
    while x < width {
        x = zigZagWidth * CGFloat(i)
        let p = zigZagHeight * CGFloat(slope)
        let y = yInitial + p
        let point = CGPoint(x: x, y: y)
        zigZagPath.addLine(to: point)
        slope = slope*(-1)
        i += 1
    }
    
    zigZagPath.addLine(to: CGPoint(x:width,y: 0))
    
    yInitial = 0 + zigZagHeight
    x = CGFloat(width)
    i = 0
    while x > 0 {
        x = width - (zigZagWidth * CGFloat(i))
        let p = zigZagHeight * CGFloat(slope)
        let y = yInitial + p
        let point = CGPoint(x: x, y: y)
        zigZagPath.addLine(to: point)
        slope = slope*(-1)
        i += 1
    }
    
    var shapeLayer = CAShapeLayer()
    shapeLayer.path = zigZagPath.cgPath
    givenView.layer.mask = shapeLayer
}
Run Code Online (Sandbox Code Playgroud)

我希望这对您有所帮助,此代码有效并经过测试

已编辑

使用这种方法,您可以获得弯曲的锯齿形而不是线条

class func pathSemiCirclesPathForView(givenView: UIView, ciclesRadius:CGFloat = 4, circlesDistance : CGFloat = 3, top:Bool = true, bottom:Bool = true ) ->UIBezierPath
    {
        let width = givenView.frame.size.width
        let height = givenView.frame.size.height
        
        let semiCircleWidth = CGFloat(ciclesRadius*2)
        
        let semiCirclesPath = UIBezierPath()
        semiCirclesPath.move(to: CGPoint(x:0, y:0))
        
        if(bottom) {
            var x = CGFloat(0)
            var i = 0
            while x < width {
                x = (semiCircleWidth) * CGFloat(i) + (circlesDistance * CGFloat(i))
                let pivotPoint = CGPoint(x: x + semiCircleWidth/2, y: height)
                semiCirclesPath.addArc(withCenter: pivotPoint, radius: ciclesRadius, startAngle: -180 * .pi / 180.0, endAngle: 0 * .pi / 180.0, clockwise: true)
                semiCirclesPath.addLine(to: CGPoint(x: semiCirclesPath.currentPoint.x + circlesDistance, y: height))
                i += 1
            }
        }
        else {
            semiCirclesPath.addLine(to: CGPoint(x: 0, y: height))
            semiCirclesPath.addLine(to: CGPoint(x: width, y: height))
        }
        
        semiCirclesPath.addLine(to: CGPoint(x:width,y: 0))
        
        if(top) {
            var x = CGFloat(width)
            var i = 0
            while x > 0 {
                x = width - (semiCircleWidth) * CGFloat(i) - (circlesDistance * CGFloat(i))
                let pivotPoint = CGPoint(x: x - semiCircleWidth/2, y: 0)
                semiCirclesPath.addArc(withCenter: pivotPoint, radius: ciclesRadius, startAngle: 0 * .pi / 180.0, endAngle: -180 * .pi / 180.0, clockwise: true)
                semiCirclesPath.addLine(to: CGPoint(x: semiCirclesPath.currentPoint.x - circlesDistance, y: 0))
                i += 1
            }
        }
        
        semiCirclesPath.close()
        
        return semiCirclesPath
    }
Run Code Online (Sandbox Code Playgroud)

结果

在此处输入图片说明