首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯威夫特UIImage不能被重力动画化

斯威夫特UIImage不能被重力动画化
EN

Stack Overflow用户
提问于 2020-02-23 21:44:58
回答 1查看 168关注 0票数 0

我在谷歌上查过所有的东西,但这并不多。斯威夫特的社区真的这么小吗?

我有一张照片,在长时间的按压下,我希望它随着重力而下降,并击中屏幕的底部。

我得到的错误是

无法将“UIView”类型的值转换为预期的参数类型“UIDynamicItem”

我曾经尝试过UILabel,UIImage,UIImageView,Rect,UIView --无论我做什么,我都会犯这个错误。我的目标是使用UIImage或UIImageView。

这是我用于动画的代码:

代码语言:javascript
复制
    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
    }

我做错什么了?找不到更多的东西可以在谷歌上试用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-14 13:46:33

误差

无法将“UIView”类型的值转换为预期的参数类型“UIDynamicItem”

告诉您需要一个数组 of UIDynamicItem。很容易错过小方括号。

实际上,您正在使用UIGravityBehavior (一个对象)而不是[redBoxView] (一个数组)来配置您的redBoxViewUICollisionBehavior。这就是为什么你会犯错误。

您需要将您的配置更改为

代码语言:javascript
复制
gravity = UIGravityBehavior(items: [redBoxView!]) //redBoxView passed in an array

还有这个

代码语言:javascript
复制
collision = UICollisionBehavior (items: [redBoxView!]) //redBoxView passed in an array
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60367149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档