I would like to ask about the screen transition by button.

Asked 2 years ago, Updated 2 years ago, 85 views

import UIKit

classViewController:UIViewController {

    var counter = 1

    vartimer = NSTimer()


    varisAnimating=true

    @IBOutletweakvaralienImage:UIImageView!
    @ IBAction func updateImage(sender:AnyObject){

        if isAnimation == true {

            timer.invalidate()

            isAnimating=false

        } else{

            timer=NSTimer.scheduledTimerWithTimeInterval (0.1, target:self, selector:Selector("doAnimation"), userInfo:nil, repeat:true)

            isAnimation = true

        }


    }
    override func viewDidLoad(){
        super.viewDidLoad()

       timer=NSTimer.scheduledTimerWithTimeInterval (0.1, target:self, selector:Selector("doAnimation"), userInfo:nil, repeat:true)

    }

    func doAnimation(){

        if counter == 5 {

            counter = 1
        } else{

            counter++

        }

        alienImage.image=UIImage(named: "frame\(counter).png")

    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

We are currently creating an animation with 5 images using this code.

However, even if you press the button, the image does not change at all.
What is the cause?

ios swift

2022-09-30 21:11

1 Answers

I didn't add any code at all, I just copied and paced it, but when I ran it with a simulator, it was animated from the start.There was no problem with button posing and resuming.
Therefore, you can infer that not animation is a problem other than program code, such as editing on a storyboard or connection between a storyboard and a code.Please verify it.

There is one more thing I would like you to look into.
The UIImageView property image is optional.In other words, replacing nil does not result in errors.Failure to change the image does not cause an error and results in 起こらないNothing Happens 」

alienImage.image=UIImage(named: "frame\(counter).png")
let result=(alienImage.image!=nil)? "Success": "Failure"
print(result)

Please check if you are able to change the image by outputting it like this.


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.