2017-10-03 1 views
0

ich zu zeichnen ich versucht: O-O-O in der MIDY von UIView aber erhalten:Falsche Position von CAShapelayer in Subschichten von UIView

wrong position of layers

Hier ist eine func für Position für CAShapeLayer.frame Berechnung. Gerade Indizes sind Punkte und ungerade Indizes sind Linien. Anzahl der Zeilen ist immer minus eine Anzahl von Punkten.

func frameForShape(at index: Int) -> CGRect { 
    let dotWidth = dotRadius * 2 
    let lineWidth = (view.bounds.width - (CGFloat(numberOfDots) * dotWidth))/CGFloat(numberOfLines) 

    if index % 2 == 0 { 
     let count = CGFloat(index)/2 
     let x = (lineWidth * count) + (dotWidth * count) 
     return CGRect(x: x, y: 0, width: dotWidth, height: view.bounds.height) 
    } else { 
     let count = Double(index)/2 
     let x = (lineWidth * CGFloat(floor(count))) + (dotWidth * CGFloat(ceil(count))) 
     return CGRect(x: x, y: 0, width: lineWidth, height: view.bounds.height) 
    } 
} 

func setFrame() { 
    for (index, shape) in shapes.enumerated() { 
     shape.frame = frameForShape(at: index) 
    } 
} 

frameForShape (at :) richtig Rahmenposition und Größe

results of frameForShape(at:)

Diese funcs einen Weg in der Mitte des gegebenen Rahmens zeichnen

func linePath(rect: CGRect) -> UIBezierPath { 
    let newRect = CGRect(x: rect.minX, y: rect.midY - (lineHeight/2), width: rect.width, height: lineHeight) 
    let path = UIBezierPath(rect: newRect) 
    return path 
} 

func dotPath(rect: CGRect) -> UIBezierPath { 
    let newRect = CGRect(x: rect.midX - dotRadius, y: rect.midY - dotRadius, width: dotRadius * 2, height: dotRadius * 2) 
    let path = UIBezierPath(ovalIn: newRect) 
    return path 
} 

Hier zurückkehren, ich bin Hinzufügen von CAShapeLayers zu gelb UIView

override func awakeFromNib() { 
    super.awakeFromNib() 

    for shape in shapes { 
     view.layer.addSublayer(shape) 
    } 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 
    layoutIfNeeded() 

    setFrame() 
    build() 
} 

func build() { 
    for (index, shape) in shapes.enumerated() { 
     if index % 2 == 0 { 
      shape.path = dotPath(rect: shape.frame).cgPath 
     } else { 
      shape.path = linePath(rect: shape.frame).cgPath 
     } 
    } 
} 

Frames sind korrekt, warum also Punkte und Zeilen falsch platziert sind?

ich diese Antwort gefunden: https://stackoverflow.com/a/40194019/

falsche Position der Schicht - Rahmen sollte auf bestimmte Position Grenzen der übergeordneten Ebene in parentView

Aber wie zeichnen Pfad gleich sein, wenn UIBezierPath (Ovalin :) und UIBezierPath (rect :) zeichne den ganzen inneren Rahmen?

+0

Wie die Antwort besagt, ändern Sie in Ihrem Aufruf von build() shape.frame in shape.bounds. – Sparky

+0

@Sparky Es funktioniert. Bitte posten Sie eine Antwort, damit ich sie annehmen kann. –

Antwort

0

Wie die Antwort sagt, ändern Sie einfach shape.frame zu shape.bounds in Ihrem Aufruf zu bauen().

Beachten Sie, dass die Koordinaten Ihrer Formebene relativ zur umgebenden Ansicht sind, daher müssen Grenzen verwendet werden.

Verwandte Themen