This question already has an answer here:
Swift Version 2 Bool [closed]
(1 answer)
Closed 7 years ago.
I'm having some issues running an App using Swift v2. Stopping the tmrRun is one issue. I know how to [tmrRun invalidate] in Objective C but not in Swift v2. Any help would be appreciated.
class ViewController: UIViewController {
#IBOutlet var imvWolf: UIImageView!
#IBOutlet var btnGo: UIButton!
#IBOutlet var btnStop: UIButton!
#IBOutlet var sliSpeed: UISlider!
var pic = 0
var tmrRun: NSTimer?
#IBAction func startRunnng(sender: UIButton)
{
tmrRun = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
btnGo.userInteractionEnabled = false
btnStop.userInteractionEnabled = true
sliSpeed.userInteractionEnabled = false
}
#IBAction func stopRunnng(sender: UIButton)
{
[tmrRun invalidate]
btnGo.userInteractionEnabled = true
btnStop.userInteractionEnabled = false
sliSpeed.userInteractionEnabled = true
}
func takeABound() -> ()
{
pic += 1;
if (pic == 8){
pic = 0;
}
}
override func viewDidLoad() {
super.viewDidLoad()
pic = 0;
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
To stop the timer is definitely timername.invalidate(), but remember you have to also stop the animation:
timername.invalidate()
isAnimating = false
If doing this you still have issues I think the problem it's where you're typing it.
Try it using this code:
#IBAction func playButton(sender: AnyObject) {
moveWolf()
}
#IBAction func pauseButton(sender: AnyObject) {
timername.invalidate()
isAnimating = false
}
func moveWolf(){
//Here instead of 0.1 you set the slider value
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("doAnimation"), userInfo: nil, repeats: true)
isAnimating = true
}
func doAnimation(){
if counter == 5 {
counter = 1
}
else{
counter++
}
imatge.image = UIImage(named: "picture\(counter).png")
}
Hope that helps you! Good luck!
Related
I have a label. And I have 3 strings. I need to display text of 3 strings in the same label with delay of 10 seconds over a infinite loop. How can i solve this with simple animations in swift 3?
That's my solution. Just connect a UILabel to the IBOutlet
class ViewController: UIViewController {
#IBOutlet weak var textLabel: UILabel!
let messages = ["PROFESSIONAL AND BEST LEARNING CENTER","LEARNING TECHNOLOGY AND DESIGN IN A SMART WAY","EXPLORE YOUR SKILLS"]
let delayTime = 10.0
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: delayTime, target: self, selector: #selector(changeDisplayedText), userInfo: nil, repeats: true)
timer.fire()
}
func changeDisplayedText() {
textLabel.text = messages[counter % messages.count]
counter += 1
}
}
This will work for your, connect your outlet properly and declare those string in an array and load it with timer change.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var array = ["Aaaaaaaaaaa", "Bbbbbbbbbbb", "Ccccccccccc"]
var scrollIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.myDelayedFunction), userInfo: nil, repeats: true)
timer.fire()
}
func myDelayedFunction()-> Void {
let count = self.array.count
if scrollIndex == count {
scrollIndex = 0
}
if scrollIndex < count {
if count > 1{
self.label.text = array[scrollIndex]
self.scrollIndex += 1
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Start of app (Main storyboard but it's fine in simulator to.)
After clicking play button in mutates into something that isn't a 1,2,3....
Here is the code in the viewcontroller and it's not much. Pretty simple stopwatch app.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
var counter = 0
var clock : Timer = Timer()
override func viewDidLoad() {
timeLabel.text = String(counter)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playButton(_ sender: Any) {
clock = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
func updateTimer(){
timeLabel.text = String(describing: counter += 1)
}
#IBAction func pauseButton(_ sender: Any) {
clock.invalidate()
}
#IBAction func resetButton(_ sender: Any) {
clock.invalidate()
counter = 0
timeLabel.text = String(counter)
}
}
I've tried setting constraints but that just shoots out "ambiguous" warnings all over the place. Any suggestions? Ideas?
The problem is at your updateTimer method. You need to move your variable increment to the line above and then create a string representation from the variable:
func updateTimer() {
counter += 1
timeLabel.text = String(counter)
}
This question already has answers here:
NSTimer - how to delay in Swift
(4 answers)
Closed 5 years ago.
I have a label that gets hidden when a button is pressed. After a certain time period like 60 secs I want the label to reappear. I'd assume I do that in viewDidAppear, How would i do that?
#IBOutlet weak var myLabel: UILabel!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//after 60 secs myLabel should reappear
//self.myLabel.isHidden = false
}
#IBAction func buttonTapped(_ sender: UIButton){
self.myLabel.isHidden = true
}
#IBAction func buttonTapped(_ sender: UIButton){
self.myLabel.isHidden = true
DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
self.myLabel.isHidden = false
}
}
You can do this by scheduling a timer:
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBAction func buttonTapped(sender: UIButton) {
if !myLabel.isHidden {
myLabel.isHidden = true
Timer.scheduledTimer(timeInterval: 15.0, target: self, selector: #selector(showLabel), userInfo: nil, repeats: false)
}
}
func showLabel() {
myLabel.isHidden = false
}
}
I´m trying to update my #IBOutlet weak var gameclockLabel: UILabel! from my class Gameclock with delegate.
I have read and tested about a million different ways but can't make it work. I think that the more I read about it the more confused I get.
You can read more about what I'm trying to do here:
swift invalidate timer in function
From the answers in that question I added this: var gameClock = Gameclock() so I was able to start a function in class Gameclock and first I tried to do the same with my class ViewController: UIViewController but that didn't work so that's why I decided to try with delegate instead. Do you think delegate is the right way to go with this?
I'm going to add several timers in separate classes to this later on so perhaps there's a better way.
Would be nice if someone could point me in the right direction. At first I thought this would be not to complicated but seems I was mistaking :)
The complete code is as follows:
import UIKit
protocol test1: class {
func updateLabel()
}
class ViewController: UIViewController, test1 {
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLoad() {
super.viewDidLoad()
gameclockLabel.text = "00:00"
}
var gameClock = Gameclock()
var startstopPushed: Bool = false
#IBOutlet weak var gameclockLabel: UILabel!
#IBOutlet weak var startstop: UIButton!
#IBAction func startStopbutton(sender: AnyObject) {
if startstopPushed == false {
gameClock.startGameclock()
startstop.setImage(UIImage(named: "stop.png"), forState: UIControlState.Normal)
startstopPushed = true
}
else
{
gameClock.stopGameclock()
startstop.setImage(UIImage(named: "start.png"), forState: UIControlState.Normal)
startstopPushed = false
}
}
func updateLabel() {
print("updated")
gameclockLabel.text = gameClock.timeString
}
}
class Gameclock : NSObject {
var gameclockTimer = NSTimer()
var timeString: String = ""
var seconds = 0
var minutes = 0
weak var delegate: test1?
func startGameclock() {
print("start")
gameclockTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateGameclock"), userInfo: nil, repeats: true)
}
func stopGameclock() {
self.gameclockTimer.invalidate()
print("stopp")
}
func updateGameclock() {
seconds += 1
if seconds == 60 {
minutes += 1
seconds = 0
}
let secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"
timeString = "\(minutesString):\(secondsString)"
print(timeString)
delegate?.updateLabel()
}
}
You haven't actually set your ViewController instance as your GameClocks delegate, so your updateLabel method won't be called;
override func viewDidLoad() {
super.viewDidLoad()
gameclockLabel.text = "00:00"
self.gameClock.delegate=self
}
I have a working countdown which you can increase by pushing the add button and thats what the time counts down from (so basically a user can set the countdown)
I would like to display the starting time as 00:00 as it does in my label.
When I click button to increase the countdown it just begins at 1 obviously because at the moment its just an Int
So i had a thought of create a dictionary something like this
var timeDictionary : [Double : Double] = [00 : 00]
I just want to increase to 00:01, 2, 3 when the + button is pressed and start from 00:00. Can anyone help with this if possible please?
this is my full code for the countdown
var timeDictionary : [Double : Double] = [00 : 00]
var timer = NSTimer()
var countdown = 0
func runTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self,
selector:Selector("updateTimer"), userInfo: nil, repeats: true)
}
func updateTimer() {
if countdown > 0 {
countdown--
TimerLabel.text = String(countdown)
} else {
countdown = 0
TimerLabel.text = String(countdown)
}
}
#IBOutlet weak var TimerLabel: UILabel!
#IBAction func IncreaseCountdown(sender: AnyObject) {
countdown++
TimerLabel.text = String(countdown)
}
#IBAction func StartCountdown(sender: AnyObject) {
runTimer()
}
#IBAction func StopCountdown(sender: AnyObject) {
timer.invalidate()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The simplest approach is just to format the counter as you display it -
#IBOutlet weak var timerLabel: UILabel!
var timer = NSTimer()
var countdown = 0
func runTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("updateTimer"), userInfo: nil, repeats: true)
}
func updateTimer() {
if --countdown < 1 {
timer.invalidate()
countdown=0;
}
self.updateTimerLabel();
}
#IBAction func IncreaseCountdown(sender: AnyObject) {
countdown++
self.updateTimerLabel()
}
#IBAction func StartCountdown(sender: AnyObject) {
runTimer()
}
#IBAction func StopCountdown(sender: AnyObject) {
timer.invalidate()
}
func updateTimerLabel() {
TimerLabel.text =NSString(format:"%02d:%02d",seconds/60,seconds%60)
}
}
Note that I also changed TimerLabel to timerLabel as the convention is that variables should start with a lowercase letter