How to access Siri Remote play/pause button and override menu button - swift

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)

Your issue is you're calling a function called handleTap: that receives a parameter but you don't have a function called handleTap:. That's what action represents in this line:
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
Change your func tapped() to:
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == UIGestureRecognizerState.Ended {
print("Menu button released")
}
}

I use the following for Swift 4 (as per question: #selector() in Swift?)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)
#objc func handleTap(sender: UITapGestureRecognizer) {
if sender.state == UIGestureRecognizerState.Ended {
print("Menu button released")
}
}

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")
}
}

Related

Swift - isUserInteractionEnabled = false but enable 2 gestures

I would like to block interactions with users, with the exception of the double tap for cancel.
Is it possible to put isUserInteractionEnabled = false and an exception rule for my double tap lines below ?
override func viewDidLoad() {
super.viewDidLoad()
let tapGR = UITapGestureRecognizer(target: self, action: #selector(PostlistViewController.handleTap(_:)))
tapGR.delegate = self
tapGR.numberOfTapsRequired = 2
view.addGestureRecognizer(tapGR)
}
--
extension MainBoardController: UIGestureRecognizerDelegate {
func handleTap(_ gesture: UITapGestureRecognizer){
print("doubletapped")
}
}
Thanks!

Passing uibutton tag through UILongPressGestureRecognizer?

I am trying to use the long press button function but am not sure how to pass the tag of the button to the function. I have an array of buttons called ChannelButton. The long button press works with the code below.
for button in ChannelButton {
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
button.addGestureRecognizer(longPressGestureRecognizer)
}
func handleLongPress(sender: UILongPressGestureRecognizer) {
doSomeFunction(NeedToPassTheButtonsTagHere)
}
But I need it to modify it to be something like this
for button in ChannelButton {
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:, button.tag)))
button.addGestureRecognizer(longPressGestureRecognizer)
}
func handleLongPress(sender: UILongPressGestureRecognizer, buttontag) {
doSomeFunction(buttontag)
}
I know this doesn't work but I'm not sure how to go about it.
A UIGestureRecognizer has a view property that is the view that it is attached to. In your case, it will be your button. Use it to get to your button's tag:
func handleLongPress(sender: UILongPressGestureRecognizer) {
guard let button = sender.view as? UIButton else { return }
doSomeFunction(button.tag)
}
You're trying to add an EXTRA parameter (an int) to an IBAction selector. You can't do that. IBActions have one of 3 selectors:
#IBAction func actionNoParams() {
}
#IBAction func actionWSender(_ sender: Any) {
}
#IBAction func actionWSenderAndEvent(_ sender: Any, forEvent event: UIEvent) {
}
One solution would be to look for the location of your long press gesture, and then check what button has that location.
func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began {
let location = sender.location(in: self.view)
for button in ChannelButton {
if button.frame.contains(location) {
//This is the button that is pressed
//Do stuff
}
}
}
}
Remember to conform to UIGestureRecognizerDelegate

swift UITapGestureRecognizer not working on view

I had to create a new thread bcoz it's driving me crazy and all the other answers online are exactly the same. I have done this countless of times but I cannot see what I am missing for the life of me. I am using a "test" view controller just to get the tap gesture working but it isn't working at all... I am fairly certain that I am setting this up correctly, as this is how I've always implemented it in the past: (yes, I have checked the box for isUserInteractionEnabled). I am even implementing this on a different viewcontroller this exact way and it is working...
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(tap)
}
let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))
#objc func wasTapped() {
print("tapped")
}
}
I have also tried adding the parameters to wasTapped:
#objc func wasTapped(gestureRecognizer: UITapGestureRecognizer) {
print("tapped")
}
You are saying:
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(tap)
}
let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))
The problem is the last line:
let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))
You cannot just say let tap like that in the middle of nowhere. You are implicitly making an instance property. But you cannot initialize an instance property with a target of self, because self does not exist at the time an instance property is initialized. (I regard the fact that that code even compiles as a bug, and have reported it as such.)
Move that line to the start of viewDidLoad, like this:
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))
view.addGestureRecognizer(tap)
}
Try something like this
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped(sender:)))
tap.numberOfTapsRequired = 1 // Default value
view.isUserInteractionEnabled = true
view.addGestureRecognizer(tap)
}
#objc func wasTapped(sender: UITapGestureRecognizer) {
print("tapped")
}
You have to enable interaction if you want to use gesture recognizers for standard UIView's
Add view.isUserInteractionEnabled = true in your viewDidLoad.
var tapGesture = UITapGestureRecognizer()
take a view and set IBOutlet like:
#IBOutlet weak var viewTap: UIView!
Write pretty code on viewDidLoad() like:
tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.myviewTapped(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
viewTap.addGestureRecognizer(tapGesture)
viewTap.isUserInteractionEnabled = true
this method is calling when tap gesture recognized:
#objc func myviewTapped(_ sender: UITapGestureRecognizer) {
if self.viewTap.backgroundColor == UIColor.yellow {
self.viewTap.backgroundColor = UIColor.green
}else{
self.viewTap.backgroundColor = UIColor.yellow
}
}

UILongPressGesture gets called twice

So when ever I long press on a button, it recognizes the long press, but "test" gets called twice. How do I prevent that from happening?
#IBOutlet weak var button2: UIButton!
func longPressMe(){
print("test")
}
func longPressGes(){
let longpress = UILongPressGestureRecognizer(target: self, action: "longPressMe")
longpress.minimumPressDuration = 1
button2.addGestureRecognizer(longpress)
}
override func viewDidLoad() {
super.viewDidLoad()
longPressGes()
}
You have to check the state of the gesture recognizer. Change longPressMe() to something like this:
func longPressMe(recognizer: UILongPressGestureRecognizer) {
guard recognizer.state == .Began else { return }
// do stuff here
}
Have a try, here is how to use #selector:
func longPressMe(recognizer: UILongPressGestureRecognizer) {
// do stuff here
}
func longPressGes(){
let longpress = UILongPressGestureRecognizer(target: self, action: #selector(yourViewController.longPressMe(_:)))
longpress.minimumPressDuration = 1
button2.addGestureRecognizer(longpress)
}

UIWebView and touch event

I want use a custom touch event in a view. There is a web view which is the subview of this view.
I override touchBegan and other functions but it does not run.
If you want to call a function while tapping a view you can use UITapGestureRecognizer
override func viewDidLoad() {
super.viewDidLoad()
let tapRecognizer = UITapGestureRecognizer(target: view, action: "handleSingleTap:")
tapRecognizer.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapRecognizer)
}
func handleSingleTap(recognizer: UITapGestureRecognizer) {
//Do something here with the gesture
}
For Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
let tapRecognizer = UITapGestureRecognizer(target: view, action: #selector(handleSingleTap))
tapRecognizer.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapRecognizer)
}
#objc func handleSingleTap(recognizer: UITapGestureRecognizer) {
//Do something here with the gesture
}