2016-09-29 3 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 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 
      } 
     }) 
    } 

내가 심각 층에 몇 가지 색상을 추가하는 것이 좋습니다 일부 알파, 배경 조회수 :

self.rod.layer.position = originPoint 

하기 때문에, 당신의 업데이트 FUNC처럼 보이는 시각적 디버그와 같이 시스템에서 수행중인 작업을 감지하는 데 유용합니다.

관련 문제