Search code examples
iosswiftuiimageviewuikit-dynamics

How to make images fall in iOS


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

Solution

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