我已经研究了谷歌上的所有内容,但这并不多。Swift的社区真的这么小吗???
我有一张图片,长按后,我希望它在重力的作用下掉下来,然后撞到屏幕底部。
我得到的错误是
无法将类型为"UIView"的值转换为预期的参数类型'[UIDynamicItem]'
我尝试过UILabel、UIImage、UIImageView、Rect、UIView,但我在做任何事情时都会遇到这个错误。我的目标是使用UIImage或UIImageView。
这是我用于动画的代码:
var animator: UIDynamicAnimator!
var gravity: UIDynamicBehavior!
var collision : UICollisionBehavior!
var redBoxView: UIView?
@IBOutlet weak var detailsImageWeather: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
animator = UIDynamicAnimator(referenceView: self.view)
let imageTap = UILongPressGestureRecognizer(target: self, action: #selector(imageTapped))
detailsImageWeather.addGestureRecognizer(imageTap)
}
@objc func imageTapped() {
var frameRect = CGRect(x: 150, y: 20, width: 60, height: 60)
redBoxView = UIView(frame: frameRect)
redBoxView?.backgroundColor = UIColor.red
self.view.addSubview(redBoxView!)
let image = detailsImageWeather.image // This is what i want to use instead of redBoxView
gravity = UIGravityBehavior(items: redBoxView!)
animator.addBehavior(gravity)
collision = UICollisionBehavior (items: redBoxView!)
collision.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(collision)
let behavior = UIDynamicItemBehavior(items: [redBoxView!])
behavior.elasticity = 2
}
我做错了什么?在谷歌上找不到更多可以尝试的东西
错误
无法将类型为"UIView"的值转换为预期的参数类型"[UIDynamicItem]">
告诉您需要UIDynamicItem
的数组。很容易漏掉小方括号。
实际上,您使用redBoxView
(一个对象(而不是[redBoxView]
(一个数组(来配置UIGravityBehavior
和UICollisionBehavior
。这就是你出错的原因。
您需要将配置更改为此
gravity = UIGravityBehavior(items: [redBoxView!]) //redBoxView passed in an array
和这个
collision = UICollisionBehavior (items: [redBoxView!]) //redBoxView passed in an array