Animation not animating in Swift 3 Xcode 8.1 - ios

When I run my application with the following code it only displays the first frame and doesn't loop through the other images.
class ViewController: UIViewController, UITextFieldDelegate {
var signUpMode = true
var activityIndicator = UIActivityIndicatorView()
#IBOutlet weak var emailText: UITextField!
#IBOutlet weak var passwordText: UITextField!
#IBOutlet weak var signIn: UIImageView!
//Animated sign in
var counter = 0
var animationStart = false
var timer = Timer()
func animateSignIn() {
signIn.image = UIImage(named: "frame_\(counter)_delay-0.04s.gif")
counter += 1
if counter == 128 {
counter = 0
}
}
func animation (){
if animationStart == true {
timer = Timer.scheduledTimer(timeInterval: 0.04, target: self, selector: #selector(ViewController.animateSignIn), userInfo: nil, repeats: true)
}
}
Then I set animationStart to true:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//remove root view controller navigation bar
self.navigationController?.navigationBar.isHidden = true
animationStart = true
}

Where did you call the function animation()?
I think viewDidLoad should look like this
override func viewDidLoad() {
super.viewDidLoad()
//remove root view controller navigation bar
self.navigationController?.navigationBar.isHidden = true
animationStart = true
animation() //call animation
}

Related

Is it possible to toss data to another view controller?

I just Make timer that can use in life. just like image that I push in here, if I go back to main ViewController then I wanna the number that I input in set view controller are tossed to viewController so when I go back to main ViewController and press restart then that number gonna be in text of CountTimeLabel.. but I really don't know how to toss data that I input in another view controller to root viewController... pleas help me.. if I write code like ViewController().variableName = 30 in setViewController, that dose not make things well..(I already know about prepare function but that is not what I am finding..because this is happen when I go back to ViewController(RootViewController)) I will put my code in below..
is it possible to toss data to another view controller from other view controller?
import UIKit
class ViewController: UIViewController{
#IBOutlet var AllTileLabel: UILabel!
#IBOutlet var SumTimeLabel: UILabel!
#IBOutlet var CountTimeLabel: UILabel!
#IBOutlet var StartButton: UIButton!
#IBOutlet var StopButton: UIButton!
#IBOutlet var ResetButton: UIButton!
var timeTrigger = true
var realTime = Timer()
var second : Int = 3000
var sum : Int = 14400
var allTime : Int = 14400
var IntSecond : Int = 0
var ifReset = false
override func viewDidLoad() {
StartButton.layer.cornerRadius = 10
StopButton.layer.cornerRadius = 10
ResetButton.layer.cornerRadius = 10
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func StartButtonAction(_ sender: UIButton) {
if timeTrigger { checkTimeTrigger() }
print("Start")
}
#IBAction func StopButtonAction(_ sender: UIButton) {
endGame()
}
#IBAction func ResetButtonAction(_ sender: UIButton) {
print(second)
getTimeData()
//second = 3000
//CountTimeLabel.text = "0:50:00"
CountTimeLabel.text = printTime(temp: second)
ifReset = true
}
#IBAction func Reset(_ sender: UIButton) {
endGame()
timeTrigger = true
realTime = Timer()
second = 3000
sum = 14400
allTime = 14400
IntSecond = 0
ifReset = false
AllTileLabel.text = "8:00:00"
SumTimeLabel.text = "0:0:0"
CountTimeLabel.text = "0:50:00"
}
#objc func updateCounter(){
// if String(format: "%.2f",second) == "0.00"{
if second < 1 {
endGame()
CountTimeLabel.text = "종료"
} else {
second = second - 1
sum = sum + 1
allTime = allTime - 1
AllTileLabel.text = printTime(temp: allTime)
SumTimeLabel.text = printTime(temp: sum)
CountTimeLabel.text = printTime(temp: second)
print("update")
}
}
func checkTimeTrigger() {
realTime = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
timeTrigger = false
}
func endGame() {
realTime.invalidate()
timeTrigger = true
}
func printTime(temp : Int) -> String
{
let S = temp%60
let H = temp/3600
let M = temp/60 - H*60
let returnString = String(H) + ":" + String(M) + ":" + String(S)
return returnString
}
func getTimeData() {
second = 20
sum = SetViewController().real.sum
allTime = SetViewController().real.allTime
print(second)
}
}
import UIKit
class SetViewController: UIViewController {
#IBOutlet var View1: UIView!
#IBOutlet var View2: UIView!
#IBOutlet var InputView1: UIView!
#IBOutlet var InputView2: UIView!
#IBOutlet var SetButton: UIButton!
#IBOutlet var H1TextField: UITextField!
#IBOutlet var M1TextField: UITextField!
#IBOutlet var H2TextField: UITextField!
#IBOutlet var M2TextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
H1TextField.keyboardType = .numberPad
M1TextField.keyboardType = .numberPad
H2TextField.keyboardType = .numberPad
M2TextField.keyboardType = .numberPad
View1.layer.cornerRadius = 14
View2.layer.cornerRadius = 14
InputView1.layer.cornerRadius = 10
InputView2.layer.cornerRadius = 10
SetButton.layer.cornerRadius = 10
// Do any additional setup after loading the view.
}
#IBAction func SetButton(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
enter image description here
If you're a hobbyist programmer and you just want to "get it done", simply use a static.
Let's say Bottom: UIViewController is the "main", root, view controller at the absolute "base" of your app. no matter what happens "Bottom" is always there.
Say Timer: UIViewController is (any) other view controller you put on top for some reason.
In Bottom, do this
class Bottom: UIViewController, etc ... {
static weak var current: Bottom? = nil
override func viewDidLoad() {
super.viewDidLoad()
Bottom.current = self
}
func testing() {
print("it works, WTH")
}
Note that in ViewDidLoad, you simply set it.
Next, say you are in Timer, try this:
class Timer: UIViewController, etc ... {
func someFunction() {
Bottom.current.testing() // it's that easy
}
It's that easy.
Note there is a huge amount of confusion about using statics, singletons, and similar approaches in iPhone programming.
(Just for example, many engineers will say "avoid singletons!" This is remarkably confused because in iOS engineering, almost everything is a singleton (notably the app itself (!!!!!), the screen, the GPS, etc etc.)
In any event, as a beginner hobbyist, learn how to use statics (it's simple .. Bottom.current. ... as above), and eventually you can learn about the pros and cons of such things.

Swift jitterclick game will not reset

I am new to learning swift and my jitterclick style game will not reset after it is complete. Despite already setting the isTimerRunning to false, nothing i have done seems to work. The game is supposed to begin when you press start, start a count down timer from 10, count your number of clicks, and give you a click per second score at the end. This is my first app without a tutorial and appreciate all the help i can get. Thanks
import UIKit
var score = 0
var timer = Timer()
var cps = score / 10
var time = 10.00
var isTimerRunning = false
var isGameComplete = true
var realTime = 0.0
class ViewController: UIViewController {
#IBOutlet weak var resetButton: UIButton!
#IBOutlet weak var clickingButton: UIButton!
#IBOutlet weak var CPSLabelTxt: UILabel!
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
#IBOutlet weak var scoreLabelTxt: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
resetButton.isHidden = true
resetButton.isEnabled = false
CPSLabelTxt.isHidden = true
scoreLabelTxt.isHidden = false
scoreLabelTxt.text = "Press Start to begin"
}
#IBAction func resetButtonDidTap(_ sender: Any){
resetButton.isEnabled = false
startButton.isEnabled = true
time = 10.0
timerLabel.text = "10.00"
isTimerRunning = false
}
#IBAction func clickingButtonDidTap(_ sender: Any) {
if !isGameComplete {
score += 1
scoreLabelTxt.text = "\(score)"
}
}
//Start button is pressed
#IBAction func startButtonDidTap(_ sender: Any) {
isGameComplete = false
startButton.isEnabled = false
//Count down timer begins
if !isTimerRunning{
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)
isTimerRunning = true
}
}
//logic for timer
#objc func runTimer(){
time -= 0.01
realTime = round(100 * time) / 100
timerLabel.text = "\(realTime)"
if time < 0.0 {
isTimerRunning = false
timerLabel.text = "0.00"
isGameComplete = true
resetButton.isHidden = false
CPSLabelTxt.isHidden = false
scoreLabelTxt.isHidden = false
scoreLabelTxt.text = "\(score)"
CPSLabelTxt.text = "\(cps)"
time = -2
}
}
}
Put timer.invalidate() in your resetButtonDidTap(_ sender: Any) function. This stops the timer from running.

How to set a timer to enable/disable buttons

I got an issue.
I want to set a timer that could disable the 6 large buttons of my ViewController2.
For example : Until the timer reach 100, it's not possible to click on the buttons Clue1Button,Clue2Button,Clue3Button,Clue4Button,Clue5Button,Clue6Button
Until the timer reach 200, it's not possible to click on the buttons 2,3,4,5,6 ...
How should I do it ? I tried several times, but I failed each time. Thanks for your help
Code of my ViewController2 :
// ViewController2.swift
// PROJET X
//
// Created by Alexis Decloedt on 22/12/2019.
// Copyright © 2019 Alexis Decloedt. All rights reserved.
//
import UIKit
class ViewController2: UIViewController {
#IBOutlet weak var Clue1Button: UIButton!
#IBOutlet weak var Clue2Button: UIButton!
#IBOutlet weak var Clue3Button: UIButton!
#IBOutlet weak var Clue4Button: UIButton!
#IBOutlet weak var Clue5Button: UIButton!
#IBOutlet weak var Clue6Button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func ChestButton(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
}
//
// ViewController2.swift
// PROJET X
//
// Created by Alexis Decloedt on 22/12/2019.
// Copyright © 2019 Alexis Decloedt. All rights reserved.
//
import UIKit
var currentCount : Int?
var maxCount : Int?
var mytimer : Timer?
let ValeurStock = "ValeurStock"
class ViewController2: UIViewController {
#IBOutlet weak var Clue1Button: UIButton!
#IBOutlet weak var Clue2Button: UIButton!
#IBOutlet weak var Clue3Button: UIButton!
#IBOutlet weak var Clue4Button: UIButton!
#IBOutlet weak var Clue5Button: UIButton!
#IBOutlet weak var Clue6Button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
currentCount = 0
self.mytimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(increase), userInfo: nil, repeats: true)
}
#IBAction func ChestButton(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
func increase() {
currentCount! += 1
}
func buttonStatus() {
if currentCount ?? <#default value#> >= 100 {
//I'm stuck ?? How to continue ?
}
}
}
Try to make my button disable/enable
What you may want to do is:
//Add variable that will count how many times Timer was repeated.
var timerCount = 0
//Create timer
Timer.scheduledTimer(withTimeInterval: 100, repeats: true) { t in
//After first 100 sec passed, timer will get triggered and enable the buttons you want.
button1.isEnabled = true
button2.isEnabled = true
//After another 100 passed, timer will enable other buttons
if timerCount == 1 {
button3.isEnabled = true
button4.isEnabled = true
t.invalidate()
}
//Adds 1 to timer count
timerCount += 1
}

work with UIPagecontrol using NSTimer in swift4

I am trying to add the UIPagecontrol using NSTimer for images sliding.
but not able to get it.
I have tried something which is given below.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var pagecontrol: UIPageControl!
#IBOutlet weak var imgview: UIImageView!
var tTime: Timer!
var index = 0
var items = ["1.png","11.png","111.png","1111.png"]
override func viewDidLoad() {
super.viewDidLoad()
configurePageControl()
let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
// Do any additional setup after loading the view, typically from a nib.
}
func configurePageControl() {
self.pagecontrol.numberOfPages = items.count
self.pagecontrol.currentPage = 0
self.pagecontrol.tintColor = UIColor.red
self.pagecontrol.pageIndicatorTintColor = UIColor.black
self.pagecontrol.currentPageIndicatorTintColor = UIColor.green
}
#objc func update() {
index = index + 1
}
}
If anyone helps,Would be great.Thank in advance.
Change your
#objc func update() {
index = index + 1
}
function to
#objc func update() {
index = index + 1
if index >= items.count {
index = 0
}
imgview.image = UIImage(named: items[index])
pagecontrol.currentPage = index
}
And it should work.

Swift Run An NSTimer Automatically

I'm making an app and using NSTimer to make the timer in my app.But I need the NSTimer to run when the scene start.I have two scene in my app a homescreen and the app it's self here is my code viewcontroller.swift (The second scene the first scene in empty) And btw it's my 3 day using swift and i'm in middle school ;).
import UIKit
var Number = 2
var Answer = Number * 2
var score = 0
var scorelabel = "Score: "
var Timer = NSTimer()
var Counter = 10
class SecondViewController: UIViewController {
#IBOutlet weak var RightAndWrongLabel: UILabel!
#IBOutlet weak var TimerLabel: UILabel!
#IBOutlet weak var RightAndWrong: UIImageView!
#IBOutlet weak var ScoreIabel: UILabel!
#IBOutlet weak var UserInputAnswer: UITextField!
#IBOutlet weak var Question: UILabel!
#IBOutlet weak var TimerOut: UIImageView!
var seconds = 0
var timeison = true
#IBAction func ConfirmAnswer(sender: AnyObject) {
let UserAnswer = Int(UserInputAnswer.text!)
if UserAnswer == Answer {
print("Your right")
Number += 2
score += 1
ScoreIabel.text = "Score: \(score)"
Question.text = "\(Number) x 2"
UserInputAnswer.text = ""
Answer = Number * 2
RightAndWrong.image = UIImage(named: "Label")
RightAndWrongLabel.hidden = false
RightAndWrongLabel.text = "Right!"
RightAndWrongLabel.textColor = UIColor(red: 0, green: 225, blue: 0, alpha: 1)
} else {
UserInputAnswer.text = ""
RightAndWrong.image = UIImage(named: "Label")
RightAndWrongLabel.hidden = false
RightAndWrongLabel.text = "Wrong!"
RightAndWrongLabel.textColor = UIColor(red: 225, green: 0, blue: 0, alpha: 1)
}
}
func DisplayTimer() {
Timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}
func updateTimer(){
TimerLabel.text = String(Counter--)
}
override func viewDidLoad() {
super.viewDidLoad()
UserInputAnswer.keyboardType = UIKeyboardType.NumberPad
RightAndWrongLabel.hidden = true
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
3
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Swift 2.x :
func DisplayTimer() {
Timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target:self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
func updateTimer() {
if Counter != 0 { TimerLabel.text = "\(Counter -= 1)"
} else {
Timer.invalidate()
// call a game over method here...
}
}
override func viewDidLoad() {
super.viewDidLoad()
...
// start the timer when this controller shows up
DisplayTimer()
TimerLabel.text = "\(Counter)"
...
}
If you want to start your timer when the scene loads call DisplayTimer() under
override func viewDidLoad() when the view is loaded
Also if you want to update your label every second, the selector in your NSTimer is calling updateCounter not updateTimer

Resources