如何使图像落入 iOS

How to make images fall in iOS

我需要做的一件事就是让一个火球掉下来。我可以让它与正方形一起使用,但如何让它与我的图像一起使用?我以前弄方块掉下来的网址在这里:https://www.raywenderlich.com/76147/uikit-dynamics-tutorial-swift

这是我对图片的尝试:

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var fireball: UIImageView!

    var animator: UIDynamicAnimator!
    var gravity: UIGravityBehavior!
    var collision: UICollisionBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background23.png")!)

        animator = UIDynamicAnimator(referenceView: view)
        gravity = UIGravityBehavior(items: [fireball])
        animator.addBehavior(gravity)

        collision = UICollisionBehavior(items: [fireball])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
    }

    @IBOutlet weak var imageToMove: UIImageView!

    @IBAction func buttonmoveleft(_ sender: Any) {
        self.imageToMove.frame.origin.x += 20
    }

    @IBAction func buttonmoveright(_ sender: Any) {
        self.imageToMove.frame.origin.x -= 20
    }
}

用正方形:

import UIKit

class SecondViewController: UIViewController {

    var animator: UIDynamicAnimator!
    var gravity: UIGravityBehavior!
    var collision: UICollisionBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background23.png")!)

        let square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        square.backgroundColor = UIColor.gray
        view.addSubview(square)

        animator = UIDynamicAnimator(referenceView: view)
        gravity = UIGravityBehavior(items: [square])
        animator.addBehavior(gravity)

        collision = UICollisionBehavior(items: [square])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
    }

    @IBOutlet weak var imageToMove: UIImageView!

    @IBAction func buttonmoveleft(_ sender: Any) {
        self.imageToMove.frame.origin.x += 20
    }

    @IBAction func buttonmoveright(_ sender: Any) {
        self.imageToMove.frame.origin.x -= 20
    }
}

尝试传递带有 'self' 标记的对象(对象 属性):

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var fireball: UIImageView!

    var animator: UIDynamicAnimator!
    var gravity: UIGravityBehavior!
    var collision: UICollisionBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background23.png")!)

        animator = UIDynamicAnimator(referenceView: self.view)
        gravity = UIGravityBehavior(items: [self.fireball])

        collision = UICollisionBehavior(items: [self.fireball])
        collision.translatesReferenceBoundsIntoBoundary = true

        animator.addBehavior(collision)
        animator.addBehavior(gravity)
    }

    @IBOutlet weak var imageToMove: UIImageView!

    @IBAction func buttonmoveleft(_ sender: Any) {
        self.imageToMove.frame.origin.x += 20
    }

    @IBAction func buttonmoveright(_ sender: Any) {
        self.imageToMove.frame.origin.x -= 20
    }
}