How to remove focus from UIButton after Timer has expired (tvOS) - swift

I'm setting up a simple tvOS application with a menu dock (Stack view containing buttons) at the bottom. After 5 seconds of inactivity I want the dock to hide (move down, out of view) and remove the focus from the currently focused button. The dock should reappear and re-focus on remote control activity. With the following code the dock hides, but just once, after the app has launched. Any help would be much appreciated!
#IBOutlet weak var menuDock: UIStackView!
var timer = Timer()
func resetTimer() {
timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ViewController.hideDock), userInfo: nil, repeats: true)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.showDock))
self.view.addGestureRecognizer(tapRecognizer)
}
#objc func hideDock() {
UIView.animate(withDuration: 0.5, animations: {
self.menuDock.frame.origin.y += 240
}, completion: nil)
timer.invalidate()
}
#objc func showDock() {
UIView.animate(withDuration: 0.5, animations: {
self.menuDock.frame.origin.y -= 240
}, completion: nil)
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(ViewController.hideDock), userInfo: nil, repeats: true)
timer.invalidate()
}

#IBOutlet weak var menuDock: UIStackView!
var timer = Timer()
func resetTimer() {
timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ViewController.hideDock), userInfo: nil, repeats: true)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.showDock))
self.view.addGestureRecognizer(tapRecognizer)
}
#objc func hideDock() {
UIView.animate(withDuration: 0.5, animations: {
self.menuDock.frame.origin.y += 240
self.menuDock.arrangedSubviews.forEach{
($0 as? UIButton)?.isEnabled = false
}
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
}, completion: nil)
timer.invalidate()
}
#objc func showDock() {
UIView.animate(withDuration: 0.5, animations: {
self.menuDock.frame.origin.y -= 240
self.menuDock.arrangedSubviews.forEach{
($0 as? UIButton)?.isEnabled = true
}
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
}, completion: nil)
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(ViewController.hideDock), userInfo: nil, repeats: true)
timer.invalidate()
}

Related

Get Keyboard Toolbar Height

i have the same question as this :
iPhone Keyboard with accessory view height problems
but the answer has no new solution and does not solve anything!
I found a good solution Here
Made few changes and updated it to swift 4.2.
Few points to be mentioned
Created an outlet of textfield and bottom constraint from Storyboard to ViewController
Bottom constraint is used for moving the textfield up and down.
class ViewController: UIViewController {
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var textFieldBottomContraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
self.setUpKeyBoardNotifications()
self.addToolBarTo(uiElement: self.inputField)
}
func setUpKeyBoardNotifications()
{
NotificationCenter.default.addObserver(self,
selector: #selector(self.keyboardNotification(notification:)),
name: UIResponder.keyboardWillChangeFrameNotification,
object: nil)
}
func addToolBarTo(uiElement element:UITextField)
{
let numberToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 45))
numberToolbar.barStyle = .black
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ViewController.cancelAction)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.doneAction))]
numberToolbar.sizeToFit()
element.inputAccessoryView = numberToolbar
}
#objc func keyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
if endFrameY >= UIScreen.main.bounds.size.height {
self.textFieldBottomContraint?.constant = 0.0
} else {
self.textFieldBottomContraint?.constant = endFrame?.size.height ?? 0.0
}
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
#objc func cancelAction()
{
self.inputField.resignFirstResponder()
}
#objc func doneAction()
{
self.inputField.resignFirstResponder()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}

Timer only executing function once

So the timer works once but after the first time it doesnt work anymore... What's the problem and how can it be fixed? Thanks!
override func viewDidLoad() {
super.viewDidLoad()
let myTimer : Timer = Timer.scheduledTimer(timeInterval: 7, target: self, selector: (selector: "functionOne"), userInfo: nil, repeats: true)
}
}
func functionOne()
{
print("hello")
}
Try this code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Timer.scheduledTimer(timeInterval: 7.0, target: self, selector: #selector(functionOne), userInfo: nil, repeats: true)
}
func functionOne () {
print("hello")
}
Output:

Toggle Play/Pause UIBarButtonItem in Toolbar using Swift

I am trying to toggle my button between a play and pause image when I start and stop a ticker using Swift. My code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var btnPlayPause: UIBarButtonItem!
var isPlaying = false
var timer = NSTimer()
var count = 0
#IBOutlet weak var lblTime: UILabel!
#IBOutlet var myToolbar: UIToolbar!
#IBAction func btnPlay(sender: UIBarButtonItem)
{
//set the button to animate
self.myToolbar.setItems([self.btnPlayPause], animated: true)
if !isPlaying //if the ticker is not ticking
{
//change the button to a pause button
println("worked")//start the ticker
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
isPlaying = true
}else{ //if the ticker is ticking
//change the pause button to a play button
self.btnPlayPause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: nil)
//pause the ticker
timer.invalidate()
//btnPlayPause.enabled = true
isPlaying = false
}
}
#IBAction func btnReset(sender: UIBarButtonItem)
{
//reset and restart the ticker
timer.invalidate()
count = 0
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
#IBAction func btnStopit(sender: UIBarButtonItem)
{
//stop and reset the ticker to "0"
timer.invalidate()
count = 0
lblTime.text = String(count)
isPlaying = false
}
func updateTime()
{
//displays ticker label with count
lblTime.text = String(count++)
}
override func viewDidLoad()
{
super.viewDidLoad()
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "btnStopit:")
self.btnPlayPause = button
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
The Play and all the other buttons on the toolbar are clearing and the toolbar is creating the Pause button alone by itself like this:
I want to just toggle my Play button with the Pause button without removing any of the other buttons from the toolbar. Is this possible?
If anyone could help me out with this I'd greatly appreciate it!
Thanks
Casting your button to a new instance of UIBarButtonItem isn't gonna do the needful. First create an outlet for your toolbar and retrieve the existing items, then change the play button item according to its position on the toolbar.
In my case the toggle button is on the left so I access it with index 0 and replace it with my corresponding toggle. And then calling the setItems() on your toolbar will update your toolbar. Set animation to true for a nice little fade animation.
#IBOutlet weak var toolBar: UIToolbar!
#IBAction func playPauseToggle(sender: UIBarButtonItem) {
var toggleBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPauseToggle:")
if playing {
player.pause()
playing = false
} else {
player.play()
toggleBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPauseToggle:")
playing = true
}
var items = toolBar.items!
items[0] = toggleBtn
toolBar.setItems(items, animated: true)
}
slightly more dense version:
#IBAction func playPauseToggle(sender: UIBarButtonItem) {
var barButtonItems = toolBar.items!
barButtonItems[0] = UIBarButtonItem(barButtonSystemItem: player.rate == 1.0 ? .Pause : .Play,
target: self, action: "playPauseButtonWasPressed:")
toolBar.setItems(barButtonItems, animated: true)
}
//This code is tested and working with Swift 2
#IBOutlet weak var navigationBar: UINavigationBar!
//playToPause()
#IBAction func playButton(sender: UIBarButtonItem) {
let newBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButton:")
navigationBar.topItem?.rightBarButtonItem = newBarButton
}
// pauseToPlay()
#IBAction func pauseButton(sender: UIBarButtonItem){
let pauseBtnItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButton:")
navigationBar.topItem!.rightBarButtonItem = pauseBtnItem
}

Swift - How to toggle between Play and Pause with UIBarButtonItem

I am trying with the below Swift code in my timer application to start and pause with the same button I have. But it is does not toggle. The button is set with Identifier as "Play" in the IDE. When I run the app, Timer starts and pauses correctly. But it does not toggle button. It always remain as "Play"
#IBAction func pressPausePlay(sender: AnyObject) {
if playPause == false
{
timer.invalidate()
self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "pressPausePlay")
playPause = true
}
else
{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pressPausePlay")
playPause = false
}
}
You can use this Example scenarios to toggle between Play and Pause, for Button Titles, Image, Navigation Bar, ToolBar
import UIKit
import AVFoundation
class ViewController: UIViewController {
var myPlayer = AVAudioPlayer()
#IBOutlet var myNaviBar: UINavigationBar! //OUTLET TO NAVIGATION BAR IMPORTANT TO WORK WITH ITS BARBUTTON ITEMS
#IBOutlet var myToolBar: UIToolbar!
//**************START :Bottom LEFT TOOLBAR Bar Button Item***********/
#IBAction func toolBarItemPlay(sender: AnyObject) {
myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "toolBarItemPause:")], animated: true)
myPlayer.play()
}
#IBAction func toolBarItemPause(sender: AnyObject) {
myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "toolBarItemPlay:")], animated: true)
myPlayer.pause()
}
//**************END :Bottom LEFT TOOLBAR Bar Button Item***********/
//**************START :Top LEFT Naivation Bar Button Item***********/
#IBAction func leftNavgationbarPlay(sender: UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "leftNavigationbarPause:")
myPlayer.play()
}
//no need of control drag to this one below because we are calling from the above leftNavgationbarPlay's Selector/action
#IBAction func leftNavigationbarPause(sender : UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "leftNavgationbarPlay:")
myPlayer.pause()
}
//**************END :Top LEFT Naivation Bar Button Item***********/
//**************START :Top Right Naivation Bar Button Item***********/
#IBAction func naviPlay(sender: UIBarButtonItem) {
//change the topright navigation bar item from SystemItem Play '>' to systemitem pause '||'
// here I am assigning the top left item Play to Bar button item Pause
/* ACTION SELECTOR IMPORTANT. IT MUST CALL THE IB ACTION FUNCTION OF PAUSE AND VICEVERSA, WITH ":" */
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "naviPause:")
myPlayer.play()
}
#IBAction func naviPause(sender: UIBarButtonItem) {
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "naviPlay:")
myPlayer.pause()
}
//**************END : Top Right Naivation Bar Button Item***********/
//************START :BUTTON IMAGE CHANGE - ">"to "||"************//
#IBAction func imagePlayButton(sender: UIButton) {
let chkImage = sender.currentImage!
if chkImage == UIImage(named: "play.png"){
sender.setImage(UIImage(named: "pause.png"), forState: .Normal)
myPlayer.play()
} else {
sender.setImage(UIImage(named: "play.png"), forState: .Normal)
myPlayer.pause()
}
}
//************END :BUTTON IMAGE CHANGE - ">"to "||"************//
//************START BUTTON TITLE CHANGE - PLAY to PAUSE************//
#IBAction func playButton(sender: UIButton) {
let chkTitle = sender.currentTitle
if chkTitle == "Play" {
sender.setTitle("Pause", forState: .Normal)
myPlayer.play()
} else {
sender.setTitle("Play", forState: .Normal)
myPlayer.pause()
}
}
//************END BUTTON TITLE CHANGE - PLAY to PAUSE************//
#IBAction func naviStop(sender: UIBarButtonItem) {
myPlayer.stop()
myPlayer.currentTime = 0
}
#IBOutlet var sliderUI: UISlider!
#IBAction func mySlider(sender: UISlider) {
myPlayer.volume = sliderUI.value
}
override func viewDidLoad() {
super.viewDidLoad()
let audioFilePath = NSBundle.mainBundle().pathForResource("ARRehman", ofType: "mp3")
//need NSURL from string
do {
myPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioFilePath!))
}
catch {
print("Some error")
}
}
}
You need to replace bar button items hold in UIToolbar after you assign a new instance to self.playPauseButtonVar.
If you have toolBar or navigationBar outlets:
self.toolBar.setItems([self.playPauseButtonVar], animated: true)
// Or
self.navigationBar.setItems([self.playPauseButtonVar], animated: true)
If your class inherits UIViewController:
self.setToolbarItems([self.playPauseButtonVar], animated: true)
// Or
self.navigationItem.setRightBarButtonItem(self.playPauseButtonVar, animated: true)
Yoichi, thanks for pointing me to the correct place. I removed the references on the Tool bar Outlet variable (bottomToolBar) under "Referencing Outlets". That resolved the error. However I am not able to add the tool bar outlet again, if I add, the error comes back!
it worked with me by creating bar button by code this is my code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationBar!
#IBOutlet weak var displayCountLabel: UILabel!
var buttonPause = UIBarButtonItem()
var buttonPlay = UIBarButtonItem()
var count=0
var timer=NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
buttonPause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseAction")
buttonPlay = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playAction")
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func stopTimer(sender: AnyObject) {
timer.invalidate()
count = 0
displayCountLabel.text = "\(count)"
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
}
func pauseAction() {
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
timer.invalidate()
}
func playAction() {
self.navigationBar?.topItem?.leftBarButtonItem = buttonPause
timer=NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("displayCount"), userInfo: nil, repeats: true)
}
func displayCount()
{
count++
displayCountLabel.text = "\(count)"
}
}

NSTimer() - timer.invalidate not working on a simple stopwatch?

I'm kind of at a wall of what to do, the program runs, but the timer.invalidate() does not work? Any tips are welcome.
I worked with this program in the past and worked through it and the timer.invalidate() worked flawlessly. I do not believe that it has to do with the fact that I put it into a function because before it wasn't working when I was just typing "timer.invalidate()" instead of "stop()"
Whenever I click the button Cancel on the iOS simulator it just resets it back to 0, but keeps counting.
Thanks in advance.
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var count = 0
#IBOutlet weak var timeDisplay: UILabel!
#IBAction func playButton(sender: AnyObject) {
//play button
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
}
#IBAction func resetTimer(sender: AnyObject) {
//cancel button
stop()
count = 0
timeDisplay.text = "0"
}
#IBAction func pauseButton(sender: AnyObject) {
stop()
}
func result() {
count++
timeDisplay.text = String(count)
}
func stop () {
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.
}
}
In playButton() you are defining another timer, that can't be invalidated from outside this function - so by calling timer.invalidate() you invalidate just var timer = NSTimer() which doesn't carry any set timer in it.
So replace
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
with
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)