-1

One of the things I need to do is make a fireball fall down. I can make it work with a square but how can I make it work with my image? The website that I used to get the square to fall down is here : https://www.raywenderlich.com/76147/uikit-dynamics-tutorial-swift

This is my attempt with the image:

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

With the square:

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
    }
}
Jay Patel
  • 2,642
  • 2
  • 18
  • 40
Nags
  • 15
  • 9

1 Answers1

1

Try passing the object with the 'self' marker (object property):

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
    }
}
  • This is the error message that I got on line 16 : " Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" – Nags Nov 02 '17 at 19:58
  • Mmm...can you try instead of using the Outlet to instantiate the image from code? var fireball: UIImageView = UIImageView(...) ? – Andrea Vultaggio Nov 06 '17 at 12:25
  • Just posted a new question related to this... could you possibly be able to answer it? – Nags Nov 29 '17 at 01:33