PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label uitapgesturerecognizer. Show all posts
Showing posts with label uitapgesturerecognizer. Show all posts

Thursday, September 29, 2022

[FIXED] How to add a tap gesture to multiple UIViewControllers

 September 29, 2022     swift, tvos, uicollectionview, uitapgesturerecognizer     No comments   

Issue

I'd like to print a message when an user taps twice on the remote of the Apple TV. I got this to work inside a single UIViewController, but I would like to reuse my code so that this can work in multiple views.

The code 'works' because the app runs without any problems. But the message is never displayed in the console. I'm using Swift 3 with the latest Xcode 8.3.3. What could be the problem?

The code of a UIViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    _ = TapHandler(controller: self)

}

The code of the TapHandler class

class TapHandler {

    private var view : UIView?

    required init(controller : UIViewController) {

        self.view = controller.view

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.message))
        tapGesture.numberOfTapsRequired = 2
        self.view!.addGestureRecognizer(tapGesture)
        self.view!.isUserInteractionEnabled = true

    }

    @objc func message() {
        print("Hey there!")
    }

}

Solution

Your TapHandler just getting released. Try This:

var tapHandler:TapHandler? = nil
override func viewDidLoad() {
    super.viewDidLoad()

    tapHandler = TapHandler(controller: self)

}

I have tested the code and is working.



Answered By - Torongo
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to access Siri Remote play/pause button and override menu button

 September 29, 2022     siri-remote, swift, tvos, uitapgesturerecognizer     No comments   

Issue

How can I access the play/pause button with the Siri remote and override the menu button? I am currently using this, but it is not working for me. My program crashes when I use this code but only when I call it four example pressing the pause button The coders is currently positioned below didMoveToView next to touchesBegan

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

Solution

I solved my problem by moving the tapRecognizer selector into my previously set up touch handler function so the code looks like this now:

private func handleTouches(touches: Set<UITouch>) {
    for touch in touches {
        let touchLocation = touch.locationInNode(self)
        lastTouch = touchLocation

        let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
        self.view!.addGestureRecognizer(tapRecognizer)
    }
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Ended {
        print("Menu button released")
    }     
 }


Answered By - Ferdinand Lösch
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to listen to Apple TV Remote in Objective-C?

 September 29, 2022     avplayer, avplayerviewcontroller, objective-c, tvos, uitapgesturerecognizer     No comments   

Issue

This seems like it should be pretty straight-forward but I'm having trouble finding a working example, good documentation, or even many StackOverflow posts that are helpful.

I have a custom view which contains an AVPlayer, like so:

@implementation
{
    @private
    AVPlayerViewController *controller
}

- (id) init
{
    self = [super init];
    if (self)
    {
        controller = [[AVPlayerViewController alloc] init];
        controller.view.frame = self.view.bounds;
        [self.view addSubview:controller.view];
    }
    return self;
}

@end

(I have a few other views like a message that overlays the player, a poster that I display while swapping videos, etc - but this is the basic setup)

When I integrated the IMA SDK, I started to have issues. If you press the pause button on the remote control during an ad, it pauses the ad just fine. But if you press the pause button again it doesn't unpause the ad, but instead unpauses my content player behind the ad. I don't hear any audio, but I know the content player was unpaused because I have ID3 metadata in my video and an NSLog() statement when I hit it, and I begin to see these logs. If I press the pause button again, the logs pause. I press it a fourth time, the logs start up again.

To try and fix this I wanted to bind a listener to the remote's play/pause button and make sure that if I was playing an ad then the ad was resumed, not the content. So I tried adding the following to my init method on my view:

        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapped:)];
        [tapRecognizer setAllowedPressTypes:@[ [NSNumber numberWithInt:UIPressTypePlayPause] ]];
        [self.view addGestureRecognizer:tapRecognizer];

I then created the following method:

- (void) tapped: (UITapGestureRecognizer *) sender
{
    NSLog(@"Tapped");
}

This isn't being called. I'm pretty confident I made a simple mistake, but the documentation isn't very clear, so I'm not sure what I should be doing instead. The official documentation on detecting button presses uses Swift and says:

    let tapRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
    tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.PlayPause.rawValue)];
    self.view.addGestureRecognizer(tapRecognizer)

I believe I translated those three lines well. The documentation then doesn't show what the tapped method should look like, but instead goes on a tangent about working with low-level event handling. So to get the appropriate method signature I looked at the documentation on UITagGestureRecognizer which had the following (Swift) example for writing a handler:

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == .ended {
        // handling code
    }
}

This is why I went with - (void) tapped: (UITapGestureRecognizer *) sender

Still, after all of that, it's not working.

Quick Update

I tried replacing:

initWithTarget:self.view

With:

initWithTarget:controller.view

And:

self.view addGestureRecognizer

With:

controller.view addGestureRecognizer

And this time it looks like something actually happened when I pressed the play/pause button. The app crashed and Xcode gave me the following error:

2019-12-17 12:16:50.937007-0500 Example tvOS App[381:48776] -[_AVPlayerViewControllerContainerView tapped:]: unrecognized selector sent to instance 0x10194e060

So it seems like (correct me if I'm wrong):

  • The AVPlayerViewController has the focus, not my view
  • The gesture recognizer calls the selector on whatever class you register it to, rather than the class that did the registering

So I guess an alternative question to my original would be: How do I allow my class to handle a gesture on some other class (e.g. AVPlayerViewController)?


Solution

The target does not need to equal the view that the gesture recognizer is attached to. You can set the target to MyView but still attach the gesture recognizer to controller.view

To work around the unrecognized selector crash, you need to make sure you're providing the correct object as the target for your gesture recognizer. The way UIGestureRecognizer works is that when the gesture is recognized, it will invoke the given selector on the given target object. In other words, when gesture fires, it's going to perform the equivalent of:

[target selector:self];

(You seem to be treating the target as the view that the gesture will be attached to, which isn't how it works)

So if you implemented tapped: on your class MyView, then the target you pass to the gesture recognizer initializer should be an instance of MyView. You probably want to provide self, not self.view.



Answered By - Justin Voss
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing