dark-walrus
dark-walrus

Reputation: 111

Moving UIImage Up and Down with Animation in Swift

I have just recently started with Xcode and Swift and I am trying to get my image to move up and down and have it switch directions when the user taps the screen. My image just will not move at all and the app will either crash or just freeze.

import UIKit
import SpriteKit
import Darwin

let startButton = UIButton(frame: CGRect(x: 200, y: 100, width: 100, height: 50))
let screenSize = UIScreen.main.bounds
let gameButton = UIButton(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))

var dot: UIImage = UIImage(named: "dot")!
var dotPic = UIImageView(image: dot)

public var goingUp = true

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = .white

    startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside)
    startButton.setImage(UIImage.init(named: "startButton"), for: .normal)

    self.view.addSubview(startButton)
}

func startButtonAction(sender: UIButton!) {
    print("button clicked")
    startButton.isEnabled = false
    startButton.setImage(UIImage.init(named: "nothing"), for: .normal)
    self.view.backgroundColor = .gray
    dotPic.frame = CGRect(x: 50, y: 300, width: 20, height: 20)

    self.view.addSubview(dotPic)
    self.view.addSubview(gameButton)

    playGame()
}

func playGame(){
    gameButton.addTarget(self, action: #selector(gameButtonAction), for: .touchUpInside)
    gameButton.setImage(UIImage.init(named: "nothing"), for: .normal)
}

func gameButtonAction(sender: UIButton!) {
    goingUp = !goingUp
    print(goingUp)
    print("clicked up/down")

    func rep(){
        repeat{
            dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
            sleep(UInt32(0.3))
        }while goingUp==true
        repeat{
            dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
            sleep(UInt32(0.3))
        }while goingUp==false
    }
    rep()
}

}

I have tried using animateWithDuration() too but it has always frozen the app. What am I doing wrong?

Upvotes: 1

Views: 4554

Answers (2)

Aman Gupta
Aman Gupta

Reputation: 179

Tried to use this code in swift...

self.hintImageView.frame = CGRect(x: x, y: 55, width: 200, height: 40)
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat, .autoreverse] , animations: {
        self.hintImageView.frame = CGRect(x: x, y: 65, width: 200, height: 40);
    }) { (completed) in

    }

Upvotes: 5

Sahil
Sahil

Reputation: 202

Here, Try this, instead of func rep() in your gameButtonAction :

[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear  animations:^{
        //code with animation
dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
    } completion:^(BOOL finished) {
        //code for completion
dotPic.frame = CGRect(x: 50, y: 300, width: 20, height: 20)
    }];

and avoid using nested function rep() in your target. Probably it is running continuously causing your application to hang.

Upvotes: 0

Related Questions