2017-09-23 4 views
0

Diese Einstellung sind mein Code:Probleme gleichen Abstandes zwischen den Ansichten in einem UIBezierPath

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    let amountOfViews = 3 
    let difference: CGFloat = 360/CGFloat(amountOfViews) 
    for i in 0...amountOfViews - 1{ 
     print((CGFloat(i) * difference)) 
     let view2 = UIView() 
     view2.translatesAutoresizingMaskIntoConstraints = false 
     view2.backgroundColor = .red 
     self.view.addSubview(view2) 
     NSLayoutConstraint(item: view2, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0).isActive = true 
     NSLayoutConstraint(item: view2, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0).isActive = true 
     NSLayoutConstraint(item: view2, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.09, constant: 0).isActive = true 
     NSLayoutConstraint(item: view2, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.35, constant: 0).isActive = true 
    let orbit = CAKeyframeAnimation(keyPath: "position") 
    let circlePath = UIBezierPath(arcCenter: self.view.frame.origin, radius: CGFloat(self.view.frame.height * 0.3), startAngle: CGFloat(i) * difference, endAngle: CGFloat.pi * 2 + CGFloat(i) * difference, clockwise: true) 
    orbit.path = circlePath.cgPath 
    orbit.duration = 10 
    orbit.isAdditive = true 
    orbit.repeatCount = Float.greatestFiniteMagnitude 
    orbit.calculationMode = kCAAnimationPaced 
    orbit.rotationMode = kCAAnimationRotateAuto 
    view2.layer.add(orbit, forKey: "orbit") 
    } 
} 

Dies ist das Ergebnis:

enter image description here

Dies ist der Druck:

0.0 
120.0 
240.0 

Für mich sollte der Unterschied zwischen den Ansichten stimmen, ist es aber nicht. Die Ansichten berühren sich, während sie einen gleichen Abstand zueinander haben sollten. Wenn die amountOfViews bis 4 einstellen, ist dies das Ergebnis:

enter image description here

das auch seltsam aussieht. Wie kann ich es zum Laufen bringen?

Antwort

1

Dies ist ein Problem von Grad vs Radiant Die UIBezierPath erfordert Winkelwerte im Bogenmaß. Ihre i Variable ist in Grad angegeben.

let radians = (CGFloat(i) * difference)/180 * CGFloat.pi 
let circlePath = UIBezierPath(arcCenter: self.view.frame.origin, radius: CGFloat(self.view.frame.height * 0.3), startAngle: radians, endAngle: CGFloat.pi * 2 + radians, clockwise: true) 
Verwandte Themen