How to repeat the animation when scrolling the TextView with Swift? - ios

Swift 4: I'm trying to scroll the textView when tap an Button. Here are my codes:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var txtViewStory: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
}
var scrollingTimer = Timer()
#IBAction func scroll(_ sender: UIButton) {
scrollingTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(ViewController.startScrolling), userInfo: nil, repeats: true)
scrollingTimer.fire()
}
#objc func startScrolling(){
var currentOffset = txtViewStory.contentOffset
currentOffset = CGPoint(x: currentOffset.x, y: currentOffset.y + 1)
txtViewStory.setContentOffset(currentOffset, animated: false)
}
}
Here is how it looks like:
The animation will stop when the TextView scrolls to the bottom. But I would like it to repeat scrolling again and again. Is there any way to get it work? Any suggestions would be appreciated.

Very dirty code but should work ;>
#IBAction func scroll(_ sender: UIButton) {
scrollingTimer.invaldiate()
scrollingTimer = nil
txtViewStory.setContentOffset(CGPoint(x: currentOffset.x, y: 0), animated: false)
scrollingTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(ViewController.startScrolling), userInfo: nil, repeats: true)
scrollingTimer.fire()
}

Related

UIButton setTitle not working inside timer after tapping on button

The timer starts and countdowns while updating the title of the button when the view is loaded. I've called startTimer in viewDidLoad. This works fine as expected.
However, the problem I faced is when I call startTimer from the IBAction. I've verified the timer is working and the selector is being called. The button title doesn't change tho. What could be wrong in this case?
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton?
var timer: Timer?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
startTimer()
}
func startTimer() {
countdownSeconds = 60
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
#objc func updateTimer() {
countdownSeconds -= 1
let countdown = countdownFormatter.string(from: TimeInterval(countdownSeconds))!
if countdownSeconds > 0 {
UIView.performWithoutAnimation {
button?.setTitle(countdown, for: .normal)
}
} else {
timer?.invalidate()
}
}
#IBAction func buttonTapped(_ sender: UIButton) {
startTimer()
}
}

Using timers in swift

I am a complete beginner and am starting to learn the Swift programming language. I'm following a tutorial on Udemy and am having some problems with setting up a timer.
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var timer1 = Timer()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.text = String(counter)
}
#IBAction func start(_ sender: Any) {
timer1 = Timer(timeInterval: 1, target: self, selector: #selector(tim), userInfo: nil, repeats: true)
}
#IBAction func pause(_ sender: Any) {
timer1.invalidate()
}
#IBAction func restart(_ sender: Any) {
timer1.invalidate()
counter = 0
label.text = String(counter)
}
#objc func tim() {
counter += 1
label.text = String(counter)
}
}
This is my code but the timer is not working. Please tell me where im going wrong.
You need to add timer to RunLoop
RunLoop.current.add(timer1, forMode: .commonModes)
Or use scheduledTimer
timer1 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(tim), userInfo: nil, repeats: true)
Also in your restart function you need to do it again, because now you are just invalidating it and not starting again.

If statement not working with timer

When 10 seconds hit the timer. I would like the background color to be change. Right now my code is not working. Nothing is being changed when the code hits 10 seconds. I also dont think the timer is working.
#IBOutlet var espn: UILabel!
var TIMER = Timer()
var seconds = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if seconds == 10{self.view.backgroundColor = UIColor.red
}
}
#IBAction func startTimer() {
TIMER = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(threeVC.clocker), userInfo: nil, repeats: true)
}
#objc func clocker() {
seconds += 1
espn.text = String( seconds)
}
}
Try this code:
#IBOutlet var espn: UILabel!
var TIMER = Timer()
var seconds = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
#IBAction func startTimer() {
TIMER = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(threeVC.clocker), userInfo: nil, repeats: true)
}
#objc func clocker() {
seconds += 1
espn.text = String( seconds)
if seconds == 10{
self.view.backgroundColor = UIColor.red
print("soda")
}
}
if statement should be written in #objc func clocker() not in viewDidAppear

Make button tapped only once - Swift

I am making timer. I cannot figure out how to make start button tapped only once as it is starting to count. And at stop button timer.invalidate() does not work
#IBAction func start(_ sender: Any) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
#IBOutlet weak var stopOutlet: UIButton!
#IBAction func stop(_ sender: Any) {
seconds = 30
label.text = "\(seconds)"
timer.invalidate()
audioPlayer.stop()
}
Just disable the button
#IBAction func start(_ sender: Any) {
stopOutlet.isEnabled = false
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
Then re-enable when needed:
stopOutlet.isEnabled = true
UIButton extends UIControl. UIControl provides this and alot more functionality to all controls.

start and pause buttons enabled/disabled swift

I am a beginner so I may not see obvious things at this point. So please, help. I need my startButton enabled again when pauseButton is pressed and vice versa. I cannot create a var that can be accessed by both UIButton functions though. What should I do? Here's part of my code:
#IBAction func Start(_ sender: AnyObject) {
let disableMyStartButton = sender as? UIButton
disableMyStartButton?.isEnabled = false
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
#IBAction func pauseTimer(_ sender: AnyObject) {
timer.invalidate()
WarningLabel.text = "Paused"
}
You need to create Outlet of your buttons and use them in your methods.
#IBOutlet var btnStart: UIButton!
#IBOutlet var btnPause: UIButton!
#IBAction func start(_ sender: UIButton) {
sender.isEnabled = false
btnPause.isEnabled = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
#IBAction func pauseTimer(_ sender: UIButton) {
sender.isEnabled = false
btnStart.isEnabled = true
timer.invalidate()
WarningLabel.text = "Paused"
}
You can also done this thing using single action for buttons like this.
#IBAction func onStartOrPause(_ sender: UIButton) {
if (sender == btnStart) {
sender.isEnabled = false
btnPause.isEnabled = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
else {
sender.isEnabled = false
btnStart.isEnabled = true
timer.invalidate()
WarningLabel.text = "Paused"
}
}
Note: You can also make same thing using single button by changing title or image of it in the action.

Resources