2017-11-23 7 views
2

크기가 커지면 UIView에 지그재그 모양을하고 있습니다. 제대로 보이지 않습니다. iOS 앱의 모양

enter image description here

enter image description here

난 당신이 프레임 초기화하지 않는 UIBezierPath를 사용하여 그리는 때, 당신의 코드를 수정 한이 코드

func zigZagShape() { 
     let width = self.frame.size.width 
     let height = self.frame.size.height 
     let givenFrame = self.frame 

     let zigZagWidth = CGFloat(6) 
     let zigZagHeight = CGFloat(4) 

     var yInitial = height-zigZagHeight 
     let 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 
     } 

     let shapeLayer = CAShapeLayer() 
     shapeLayer.path = zigZagPath.cgPath 
     self.layer.mask = shapeLayer 
    } 
+0

줌 코드 – Baig

+0

을 공유하면 작은보기를 추가하고 위의 코드를 추가했거나 뷰 컨트롤러보기를 사용하고 있습니까? – Myaaoonn

+0

아래의 솔루션을 확인하십시오 ..... – Dhiru

답변

1

을 사용하고 있습니다. 또한

@IBDesignable 
class ZigZiagView: UIView { 

    override func draw(_ rect: CGRect) { 
     super.draw(rect) 

     zigZagShape() 
    } 

    func zigZagShape() { 

     let width = self.frame.size.width 
     let height = self.frame.size.height 
     //let givenFrame = self.frame 

     let zigZagWidth = CGFloat(6) 
     let zigZagHeight = CGFloat(4) 

     var yInitial = height-zigZagHeight 
     let zigZagPath = UIBezierPath() 
     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)) 

     // draw the line from top right to Bottom 
     zigZagPath.addLine(to: CGPoint(x:width,y:height)) 

     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 
     } 

    // Now Close the path 
     zigZagPath.close() 

     let shapeLayer = CAShapeLayer() 
     shapeLayer.path = zigZagPath.cgPath 
     self.layer.mask = shapeLayer 
    } 

} 

enter image description here

enter image description here

이 함께 시도하고이 일을하거나하지 않을 경우 알려주세요?

+0

감사합니다! 그 작업 :-) – pooja