2017-11-23 17 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

関連する問題