2016-09-15 58 views
0

到目前爲止,我已經創建了一個從底部開始並使用UiDynamicAnimator向上移動的矩形。我希望用戶確定負重力的「強度」。我希望用戶通過滑塊確定值。滑塊決定重力

這是到目前爲止我的代碼:

import UIKit 

class ViewController: UIViewController { 

    var orangeSquare: UIView? 
    var animator: UIDynamicAnimator? 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     func sliderChanged(sender: AnyObject) { 

      var sliderValue = sender.value 

     } 

       //Create animation 
      let dim = CGRectMake(100, 500, 200, 100) 
      orangeSquare = UIView(frame: dim) 
      orangeSquare?.backgroundColor = UIColor.orangeColor() 

       //Add item to the screen 
      self.view.addSubview(orangeSquare!) 

       //Initialize the animator 
      animator = UIDynamicAnimator(referenceView: self.view) 

       //Add gravity 
      let gravity = UIGravityBehavior(items: [orangeSquare!]) 
      let direction = CGVectorMake(0.0, sliderValue) 
      gravity.gravityDirection = direction 

       //Collision 
      let boundries = UICollisionBehavior(items: [orangeSquare!]) 
      boundries.translatesReferenceBoundsIntoBoundary = true 

       //Add animations 
      animator?.addBehavior(boundries) 
      animator?.addBehavior(gravity) 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

我得到兩個錯誤:

「曖昧使用'value'的」 和

「的懸而未決的標識符'sliderValue'使用」如何將'liderValue'轉換爲只有一個小數點的浮點數?

回答

1

你的代碼缺少一些東西。 sliderValue是一個未解析的標識符,因爲您只在sliderChanged中聲明它,但在viewDidLoad的主體中引用它。另外,我認爲你對值的使用是不明確的,因爲你已經將該參數聲明爲AnyObject,它的值可能是許多事情中的任何一個!

您的代碼缺少將滑塊的值更改與重力行爲更改關聯的機制。因此,我已經使用附加到滑塊對象的顯式目標實現了這一點。我還在一個顯示引力大小的標籤中出現。這很粗糙,但我認爲它實現了你想要做的事情。

import UIKit 

class ViewController: UIViewController { 

    var dynamicAnimator : UIDynamicAnimator! 
    var gravityBehaviour : UIGravityBehavior! 
    var orangeSquare : UIView! 
    var slider : UISlider! 
    var sliderLabel : UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Subviews 
     self.orangeSquare = { 
      let oS : UIView = UIView(frame: CGRect(origin: CGPoint(x: (self.view.frame.width/2) - 100, y: 500), size: CGSize(width: 200, height: 200))) 
      oS.backgroundColor = UIColor.orange 
      return oS 
     }() 

     self.slider = UISlider(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: self.view.frame.width - 400, height: 50))) 
     self.slider.addTarget(self, action: #selector(self.sliderValueDidChange), for: UIControlEvents.allTouchEvents) 
     self.slider.minimumValue = -5 
     self.slider.maximumValue = 5 
     self.slider.value = 0 

     self.sliderLabel = UILabel(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 100), size: CGSize(width : 50, height: 50))) 
     self.sliderLabel.backgroundColor = UIColor.red 
     self.sliderLabel.textAlignment = NSTextAlignment.center 
     self.sliderLabel.textColor = UIColor.white 
     self.sliderLabel.text = String(self.slider.value) 

     // Assemble 
     self.view.addSubview(self.orangeSquare) 
     self.view.addSubview(self.slider) 
     self.view.addSubview(self.sliderLabel) 

     // Configure dynamic behaviours 

     self.dynamicAnimator = UIDynamicAnimator(referenceView: self.view) 
     self.gravityBehaviour = UIGravityBehavior(items: [self.orangeSquare]) 
     self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: 0) 

     // Configure boundaries 
     let boundaries : UICollisionBehavior = UICollisionBehavior(items: [self.orangeSquare]) 
     boundaries.translatesReferenceBoundsIntoBoundary = true 

     self.dynamicAnimator.addBehavior(self.gravityBehaviour) 
     self.dynamicAnimator.addBehavior(boundaries) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func sliderValueDidChange() { 
     // When the slider value changes, update the label text and the gravity vector 
     self.sliderLabel.text = String((round(self.slider.value) * 10)/10) 
     self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: CGFloat(-1 * self.slider.value)) 
    } 

} 

希望有幫助。一切順利!

+0

非常感謝您的建議和解釋! :)我固定了幾件事情。我已經在下面上傳了新的代碼。我注意到滑塊有點過於敏感(它從-10到-9到-8等)。我希望它不太敏感(從-10到-9.9到-9.8等)。我之前的代碼也遇到了這個困難。有沒有類似「self.slider.sensitivity」的東西?試圖查找它。找不到任何相關的東西。 – Saud

+0

沒有問題 - 我喜歡建造它。順便說一句,如果你認爲它解決了你的問題,那麼如果你能接受我的答案,那將是非常棒的!至於滑塊靈敏度,似乎有一個關於此問題的討論http://stackoverflow.com/questions/22147068/uislider-too-sensitive-when-releasing-touch所有最好的 – Sparky

0
import UIKit 

class ViewController: UIViewController { 

    var dynamicAnimator : UIDynamicAnimator! 
    var gravityBehaviour : UIGravityBehavior! 
    var orangeSquare : UIView! 
    var slider : UISlider! 
    var sliderLabel : UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Subviews 
     self.orangeSquare = { 
      let oS : UIView = UIView(frame: CGRect(origin: CGPoint(x: (self.view.frame.width/2) - 100, y: 500), size: CGSize(width: 200, height: 200))) 
      oS.backgroundColor = UIColor.orangeColor() 
      return oS 
      }() 

     self.slider = UISlider(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 100), size: CGSize(width: self.view.frame.width - 300, height: 150))) 
     self.slider.addTarget(self, action: #selector(self.sliderValueDidChange), forControlEvents: UIControlEvents.AllTouchEvents) 
     self.slider.minimumValue = -10 
     self.slider.maximumValue = 10 
     self.slider.value = 0 

     self.sliderLabel = UILabel(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 200), size: CGSize(width : 50, height: 50))) 
     self.sliderLabel.backgroundColor = UIColor.redColor() 
     self.sliderLabel.textAlignment = NSTextAlignment.Center 
     self.sliderLabel.textColor = UIColor.whiteColor() 
     self.sliderLabel.text = String(self.slider.value) 

     // Assemble 
     self.view.addSubview(self.orangeSquare) 
     self.view.addSubview(self.slider) 
     self.view.addSubview(self.sliderLabel) 

     // Configure dynamic behaviours 

     self.dynamicAnimator = UIDynamicAnimator(referenceView: self.view) 
     self.gravityBehaviour = UIGravityBehavior(items: [self.orangeSquare]) 
     self.gravityBehaviour.gravityDirection = CGVector(dx: 0.0, dy: 0.0) 

     // Configure boundaries 
     let boundaries : UICollisionBehavior = UICollisionBehavior(items: [self.orangeSquare]) 
     boundaries.translatesReferenceBoundsIntoBoundary = true 

     self.dynamicAnimator.addBehavior(self.gravityBehaviour) 
     self.dynamicAnimator.addBehavior(boundaries) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func sliderValueDidChange() { 
     // When the slider value changes, update the label text and the gravity vector 
     self.sliderLabel.text = String((round(self.slider.value) * 10)/10) 
     self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: CGFloat(-1 * self.slider.value)) 
    } 

}