滑块决定重力

Slider determines gravity

到目前为止,我已经使用 UiDynamicAnimator 创建了一个从底部开始向上移动的矩形。我希望用户确定负重力的 "strength"。我希望用户通过滑块确定值。

到目前为止,这是我的代码:

    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.
    }


}

我得到两个错误:

"Ambiguous use of ´value´" 和 "Use of unresolved identifier ´sliderValue´"

如何将“sliderValue”转换为只有一位小数的浮点数?

您的代码缺少一些东西。 sliderValue 是一个未解析的标识符,因为您仅在 sliderChanged 中声明了它,但在 viewDidLoad 的主体中引用了它。另外,我认为您对 value 的使用有歧义,因为您已将函数的参数声明为 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))
    }

}

希望对您有所帮助。祝一切顺利!

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))
    }

}