tvOS - catch MENU button presses during push-view-controller transition - uiviewcontroller

I have a UIViewController in my tvOS app which will only appear for a few seconds, and needs totally customizable MENU button handling. I create it like so:
- (void)viewDidLoad
{
[super viewDidLoad];
// Add a tap gesture recognizer to handle MENU presses.
UITapGestureRecognizer *tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tapGestureRec.allowedPressTypes = #[#(UIPressTypeMenu)];
[self.view addGestureRecognizer:tapGestureRec];
}
- (void)handleTap:(UITapGestureRecognizer *)sender
{
// Code to process the MENU button is here.
}
I display the view controller using pushViewController:animated::
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
[self pushViewController:controller animated:isAnimated];
I've found that if the user presses MENU as soon as the screen starts to appear, while the cross-fade transition effect is still displaying, they are able to dodge the UITapGestureRecognizer and go back to a previous screen, which is not intended. They can also cause issues by mashing MENU over and over again--eventually they will escape out of things that they should not be able to.
How can I ensure that the MENU press always reaches my override? Is there a way to specify an app-encompassing MENU button handler and still use a UINavigationController?

A solution that worked for me was to install a UITapGestureRecognizer onto self.navigationController.view. Any taps which get missed by the regular UIViewControllers end up getting caught by the navigation controller's recognizer instead. If you install UITapGestureRecognizers on each view controller you create, the only taps which will fall through the cracks to the navigation controller are taps that occur mid-transition, and it is safe to just ignore those taps completely.
Note that when you want MENU to go back to the home screen, you will need to temporarily remove this tap recognizer and let tvOS handle the tap on its own, as I could find no clean way to escape to the home screen in my code (short of exit(0)).

I have very similar problem because of this behaviour.
In my case I have custom focus management depending on MENU button click detected on pressesEnded and managed by preferredFocusedView. But when navigation controller is poping ViewController and user at this time make second click on MENU button, then UINavigationController class detect pressesEnded and then call preferredFocusedView on my destination ViewController where is my management but pressesEnded will be not called, because was "stolen" by UINavigationController.
Solution for this problem in my case is to create "dummy" class that will be used by UINavigationController like this:
class MenuPhysicalHackNavigationController: UINavigationController {
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
}
override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
}
override func pressesCancelled(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
}
override func pressesChanged(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
}
}

Related

Animate UINavigationBar when pushing view controller onto stack

The Apple Music app has a nice transition when a view controller with a visible navigation bar ("My Music" view controller) has a view controller with a transparent navigation bar (An artist's view controller) pushed on to the stack.
I'm looking to recreate this transition myself.
I've set the navigationbar to be transparent with the following code:
private func _setNavigationBarVisible(isVisible isVisible: Bool)
{
title = nil
/*
Update the navigation bar's visibility.
Create a helper method to prevent running the same code twice.
*/
func _updateNavigationBarVisibility(isVisible isVisible: Bool, userViewController: UserViewController?)
{
// Create a dummy navigation bar we can rip the default values out of if it should be visible
let navigationBar = UINavigationBar()
let backgroundImage = isVisible ? navigationBar.backgroundImageForBarMetrics(.Default) : UIImage()
let shadowImage = isVisible ? navigationBar.shadowImage : UIImage()
userViewController?.navigationController?.navigationBar.setBackgroundImage(backgroundImage,
forBarMetrics: .Default)
userViewController?.navigationController?.navigationBar.shadowImage = shadowImage
}
// Animate alongside the view controller's presentation transition if there is one
let isTransitionAnimationRun = transitionCoordinator()?.animateAlongsideTransition({ [weak self] context in
_updateNavigationBarVisibility(isVisible: isVisible, userViewController: self)
}, completion: nil)
// Or just update the values if there's no transition
if isTransitionAnimationRun == false
{
_updateNavigationBarVisibility(isVisible: isVisible, userViewController: self)
}
}
This function is run in my viewWillAppear: and viewWillDisappear: methods passing false and true to the isVisible parameter respectively.
This code does achieve a similar effect, but despite the code doing what it should, I want a different effect.
Currently, this code animates the shadow image making them visible on both view controllers rather than like Apple Music where only the one that presented it should have the shadow. As for the background image, there's a visible "swipe" animation as the navigation bar's background animates from transparent to translucent.
There's also an issue with the swipe gesture which I am looking in to, but currently if during the swipe gesture you keep your finger in one position so no swiping occurs and then lift the finger, the navigationbar updates as though the swipe finished, but the view controllers become glitched and the stack is never pushed or popped despite using the fully useable navigation bar.

Apple TV – How to only change focus when "Menu" button is tapped

I'm working on a simple board game. I have buttons in the left column like "New Game", "Settings" etc.(let's call it "Menu View"). The rest of the screen is the "Board View" with the current state of the game.
Is it possible to handle when user taps the "Menu" button on the Apple TV Remote control just to navigate from the "Board View" to my "Menu View" instead of closing my app? I have both "Menu View" and "Board View" in the same controller, maybe I need to adjust the controller hierarchy?
Thanks for help!
Eventually, I noticed overriding pressesEnded won't cancel the Menu button press for the first time after launching the app. Using UITapGestureRecognizer on the other hand does:
let menuTapGestureRegocnizer = UITapGestureRecognizer(target: self, action: "menuTapped")
menuTapGestureRegocnizer.allowedPressTypes = [UIPressType.Menu.rawValue]
view.addGestureRecognizer(menuTapGestureRegocnizer)
You can override the pressesEnded method and trigger the navigation segues manually
override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
for press in presses {
if(press.type == UIPressType.Menu) {
// Do what you want
// ...
} else {
super.pressesEnded(presses, withEvent: event)
}
}
}

Running Cocos2D Scene from UIKit/Cocoa-Touch, but Scene Is Not Receiving Touch Input

Figured out how to get UIKit and Cocos2D to work together. I'm launching my game play scene (which is supposed to be using Cocos2D) from a UITableViewCell. Problem is, after this method is run...
// MainViewController.h
MainViewController : UIViewController <CCDirectorDelegate,
UITableViewDelegate,
UITableViewDataSource,
GKGameCenterControllerDelegate>
// MainViewController.m
-(void)launchGamePlay {
[self.tableView removeFromSuperview];
// ... also removing buttons and other assets with removeFromSuperview
[mainNavBar removeFromSuperview];
[underView removeFromSuperview];
CCTransitionRotoZoom *transition = [CCTransitionRotoZoom transitionWithDuration:1.0 scene:[CCGamePlayLayer node]];
[[CCDirector sharedDirector] runWithScene:transition];
}
...I'm not able to receive touch input on the presented CCGamePlayLayer. I've tried messing around with zOrders for both the presenting view controller and the presented cocos scene to no avail. What am I doing wrong?
Most likely your MainViewController is receiving the touch input.
Override the various touchesBegan/Moved/Ended methods in the MainViewController and forward them to cocos2d's view:
[[CCDirector sharedDirector].view touchesBegan:touches event:event];

Remove ViewController from stack

In our App we have a log-in ViewController A. On user log-in, a request navigate is automatically called to navigate to the next ViewController B. However when this is done we want to remove the log-in ViewController A from the stack so the user cannot "go back" to the log-in view but goes back the previous ViewController before the log-in instead.
We thought about removing the ViewController A from the stack when ViewController B is loaded, but is there a better way?
In the Android version of the App we've set history=no (if I recall correctly) and then it works.
Is there an similar way to achieve this in MonoTouch and MvvmCross?
I ended up with removing the unwanted viewcontroller from the navigation controller. In ViewDidDisappear() of my login ViewController I did the following:
public override void ViewDidDisappear (bool animated)
{
if (this.NavigationController != null) {
var controllers = this.NavigationController.ViewControllers;
var newcontrollers = new UIViewController[controllers.Length - 1];
int index = 0;
foreach (var item in controllers) {
if (item != this) {
newcontrollers [index] = item;
index++;
}
}
this.NavigationController.ViewControllers = newcontrollers;
}
base.ViewDidDisappear(animated);
}
This way I way remove the unwanted ViewController when it is removed from the view. I am not fully convinced if it is the right way, but it is working rather good.
This is quite a common scenario... so much so that we've included two mechanisms inside MvvmCross to allow this....
a ClearTop parameter available in all ViewModel navigations.
a RequestRemoveBackStep() call in all ViewModels - although this is currently NOT IMPLEMENTED IN iOS - sorry.
If this isn't enough, then a third technique might be to use a custom presenter to help with your display logic.
To use : 1. a ClearTop parameter available in all ViewModel navigations.
To use this, simply include the ClearTop flag when navigating.
This is a boolean flag - so to use it just change:
this.RequestNavigate<ChildViewModel>(new {arg1 = val1});
to
this.RequestNavigate<ChildViewModel>(new {arg1 = val1}, true);
For a standard simple navigation controller presenter, this will end up calling ClearBackStack before your new view is shown:
public override void ClearBackStack()
{
if (_masterNavigationController == null)
return;
_masterNavigationController.PopToRootViewController (true);
_masterNavigationController = null;
}
from https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Touch/Views/Presenters/MvxTouchViewPresenter.cs
If you are not using a standard navigation controller - e.g. if you had a tabbed, modal, popup or split view display then you will need to implement your own presentation logic to handle this.
You can't: 2. RequestRemoveBackStep().
Sadly it proved a bit awkward to implement this at a generic level for iOS - so currently that method is:
public bool RequestRemoveBackStep()
{
#warning What to do with ios back stack?
// not supported on iOS really
return false;
}
from https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Touch/Views/MvxTouchViewDispatcher.cs
Sorry! I've raised a bug against this - https://github.com/slodge/MvvmCross/issues/80
3. You can always... Custom ideas
If you need to implement something custom for your iOS app, the best way is to do this through some sort of custom Presenter logic.
There are many ways you could do this.
One example is:
for any View or ViewModel which needs to clear the previous view, you could decorate the View or ViewModel with a [Special] attribute
in Show in your custom Presenter in your app, you could watch for that attribute and do the special behaviour at that time
public override void Show(MvxShowViewModelRequest request)
{
if (request.ViewModelType.GetCustomAttributes(typeof(SpecialAttribute), true).Any())
{
// do custom behaviour here - e.g. pop current view controller
}
base.Show(request);
}
Obviously other mechanisms might be available - it's just C# and UIKit code at this stage
I don't know about mvvm but you can simply Pop the viewcontroller (AC A) without animation and then push the new viewcontoller (AC B) with animation
From within AC A:
NavigationController.PopViewControllerAnimated(false);
NavigationController.PushViewController(new ACb(), true);

Need right direction for reload application by action

My scenario is a UIView Controller displays different subview programmatically... Like a game board. Now I'm in a incredible situation:
I can't find a way to reload application after the game is finish!? What I would like to do is after terminate scenario clic on IBAction for star new game. My doubt is about ViewDidLoad or call Main.
try moving your code from viewDidLoad into a new function like -(void)startGame or -(void)refreshViews: and call that function whenever you want to reload your view. even in viewDidLoad you can load your view using this function.
-(void)viewDidLoad
{
[super viewDidLoad];
[self startGame];
}
remember if you are creating allocating and add some of your subviews outside this function then they will remain visible untill and unless yu remove them from superView before calling startGame.
I solved it with this code:
(IBAction)newGame:(id)sender{
UIView *parent = self.view.superview;
[self.view removeFromSuperview];
self.view = nil; // unloads the view
[parent addSubview:self.view];
}