gpt4 book ai didi

ios - slider 决定重力

转载 作者:行者123 更新时间:2023-11-28 16:11:21 24 4
gpt4 key购买 nike

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

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

    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’”

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

最佳答案

您的代码缺少一些东西。 sliderValue 是一个未解析的标识符,因为您只在 sliderChanged 中声明了它,但在 viewDidLoad 的主体中引用了它。另外,我认为您对值的使用不明确,因为您已将函数的参数声明为 AnyObject,其值可以是多种事物中的任何一种!

您的代码缺少将 slider 值的变化与重力行为的变化联系起来的机制。因此,我使用附加到 slider 对象的显式目标实现了这一点。我还贴上了显示引力大小的标签。这相当粗糙,但我认为它实现了您想要做的事情。

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

}

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

关于ios - slider 决定重力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39517053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com