2016-09-29 2 views
0

私はその端点を中心にして線(細い矩形)を回転しようとしています。私はそれが正常に動作しているが、手順2は、行を短縮し、それが終点(回転の中心)の周りにまだ回転させることです。Swift 3のCGRect正しい起点で回転する

// Create and add a colored square 
var rod = UIView() 
var countsteps = 0 
var Mass = 1 
var Radius = 1.00 
let rectWidth = screenWidth * 0.5 
let rectVertical = screenHeight * 0.5 
let rectLeft = screenWidth * 0.5 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    // set background color to blue 
    rod.backgroundColor = UIColor(red: 145/255, green: 170/255, blue: 157/255, alpha: 1) 
    //rod Line 
    rod.frame = CGRect(x: rectLeft, y: rectVertical, width: 3, height: rectWidth) 
    // finally, add the square to the screen 
    self.view.addSubview(rod) 

    //Create Timer 
    _ = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(Acceleration.update), userInfo: nil, repeats: true) 
} 


func update() { 
    UIView.animate(withDuration: 0.1, animations: { 

     //Slope Line 
     let rectSize = CGFloat(self.Radius) * self.rectWidth/100 
     let amountRotation = CGFloat(CGFloat(self.countsteps)/57.3) 
     let originPoint = CGPoint(x: 0,y: 0) 
     self.rod.layer.anchorPoint = originPoint 
     self.rod.transform = CGAffineTransform(rotationAngle: amountRotation) 
     self.countsteps = self.countsteps + 1 
     if (self.countsteps > 359) { 
      self.countsteps = 0 
     } 
    }) 
} 

私はこの行を追加しようとした:異なる軸周りの比率を変化させながらボックスを回転させること、

self.rod.frame.size = CGSize(width: 3.00, height: rectSize) 

しかし。この行を追加すること

答えて

0

試してください。そう、あなたの更新funcがのように見える

self.rod.layer.position = originPoint 

func update() { 
     UIView.animate(withDuration: 0.1, animations: { 

      //Slope Line 
      let rectSize = CGFloat(self.Radius) * self.rectWidth/100 
      let amountRotation = CGFloat(CGFloat(self.countsteps)/57.3) 
      let originPoint = CGPoint(x: 0,y: 0) 
      self.rod.layer.anchorPoint = originPoint 
      self.rod.layer.position = originPoint 
      self.rod.transform = CGAffineTransform(rotationAngle: amountRotation) 
      self.countsteps = self.countsteps + 1 
      if (self.countsteps > 359) { 
       self.countsteps = 0 
      } 
     }) 
    } 

私は真剣にいくつかのアルファで、レイヤーとビューの背景にいくつかの色を追加することをお勧めします、それはかなりありますビジュアルデバッグのように、システムが何をしているのかを検出するのに便利です。

関連する問題