How to detect when the RPSystemBroadcastPickerView is tapped - ios

I am using RPSystemBroadcastPickerView to start a system-wide screen recording from my app. The RPSystemBroadcastPickerView is completely autonomous in starting the recording and everything, which I guess makes sense - only user can start the screen recording by explicitly tapping the button.
I need to know when the RPSystemBroadcastPickerView is tapped. Right now the UI is showing keyboard, which I want to keep showing (it is a chat app). However, the form showing the list of broadcast extensions to pick one is being shown under the keyboard. See following image:
This effectively prevents the user to start the broadcast. If I knew when the user tapped RPSystemBroadcastPickerView, I could manually hide the keyboard at that moment. Any suggestions?

I did not find any callbacks for that, but you can create your transparent button, add it above the RPSystemBroadcastPickerView. When user will tap on your button you will be able to hide the keyboard and than transfer the action to the RPSystemBroadcastPickerView using the code:
for subview in screenSharingProviderPickerView.subviews {
if let button = subview as? UIButton {
button.sendActions(for: UIControlEvents.allTouchEvents)
}
}

I found similar solution to accepted answer, but you don't need to create transparent button.
I just add additional target-action pair to the picker button dispatch table, with #selector which will be fired, when button is pressed.
// picker is an instance of RPSystemBroadcastPickerView
for subview in picker.subviews {
if let button = subview as? UIButton {
button.addTarget(self, action: #selector(pickerAction), for: .touchUpInside)
}
}
// You can also accept _ sender: UIButton as parameter, if you need to.
#objc func pickerAction() {
// called when user press on RPSystemBroadcastPickerView
}
Note: Keep in mind that, if Apple change hierarchy of this view in the future, you probably need to update application as well.

Related

Change bar button item programmatically after user presses it Swift

I am trying to make a feature where if the user presses the speaker button (a bar button item), then a speaker with a slash through it will replace the original button. Is there an easy way of doing this? I know that it must be done programatically but I can't figure out how to replace the speaker button with a different button.
[Here is a screenshot of my storyboard]
: https://i.stack.imgur.com/EL3df.png
Add an action for UIBarButtonItem on your UIViewController class by control dragging it into the ViewController's declaration. Then change the item
#IBAction func barButtonAction(_ sender: UIBarButtonItem) {
sender.image = UIImage(named: "myNewImage")
}

Swift Programmatically Launch a UILongPressGesture

I would like to programmatically launch a UILongPressGesture when a user touches a button. This was asked years ago at How can I send a UILongPressGesture programmatically? but I'm wondering if there's a cleaner or more modern solution.
My existing UILongPressGestureRecognizer code (which maps actual user interactions to functionality) works as follows:
view.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(longPress)))
where longPress is defined as:
#objc func longPress(sender: UILongPressGestureRecognizer) {
switch sender.state {
case .began:
// Display a 'hover' animation for a specific UIView
....
case .changed:
// Move existing hover animation
...
default:
// Complete the hover animation
...
}
}
Desired Functionality
I am using this long press to display a 'hovering' effect for whatever UIView is selected by the user. I also want to provide a button to start the long press automatically (bypassing the longPress where sender.state == .began). My projected solution is programmatically creating the .began long press, creating a gesture object that users can drag around the screen (possibly even translating the UILongPressGestureRecognizer to a UIPanGestureRecognizer if possible), and then continuing the hover animation logic with that new gesture.
I found a much cleaner solution to the problem at hand - Replacing the desired button with a UIView containing its own UILongPressGestureRecognizer. I set this gesture's minimumPressDuration to 0 so it behaves equivalently to a button's touchDown event. This new gesture uses the same longPress function from the original question without requiring any additional code to trigger.

How to add a button to a Tab Bar Controller

I mean, not a UITabBarItem, an UIButton. For example, in the Deezer app, the middle button shows a view with an animation that covers the entire screen. I don't want the button to be rounded. Just to execute the action.
This is a very simplistic example. You'll still have to modify it to fit your needs, but if you have a UIBarButtonItem called button that you've added to your navigationBar, you should be able to do something like this, in viewDidLoad
button.action = #selector(showView)
Then you just need to create a function to be called.
func showView() {
let myView = UIView()
self.view.addSubview(myView)
}
Of course this has no animations, but again this is just to point you in the right direction.

How to disable fast touches and double opening view?

Please help me with problem -
I have button, or label, or textfield at UiViewController view, which have method to open another view, if i touch it.
But if i touch button, label or textfield very fast, it can open two, or three, or more same views.
How to disable this opening? How to open only one view?
How i can do this for all project?
Thanks!
when you click on button then Calling method of the Button, Inside the button method you disable your button, and after when you dismiss open view then again you enable your button.
-(void)YourButtonMethod
{
YourBtn.enable = false;
}
// Enable your YourBtn when dismiss yourView.
It is usually easier to disable the UIButton when you enter the function and re-enable it before leaving. Would look something like this function
-(void)buttonTapped:(UIButton *)aButton
{
aButton.enabled = false;
// do your work here
// and before leaving
aButton.enabled = true;
}
But in your case you are pushing a UIViewController, so you should move the line aButton.enabled = true to your prepareForSegue function.

How do I use a switch statement to find if a UIButton is pressed?

I’m making an ios application and am having trouble using a switch statement to see if a UIButton element was pressed.
This is who I want the end product to work: I have multiple uncolored images (by uncolored I mean white, a UIImage). When an uncolored image is tapped a subview opens with colored boxes (UIButtons, 24 of them, each with individual colors.). When a colored box button is selected and the back button on the toolbar is pressed, the subview closes and the original view re-appears with the uncolored image (the one selected to open the subview) now colored with the desired color selected in the subview.
I want to use a switch statement to find which uncolored image and which color was selected (all UIButton elements). I do not know what to put as an expression in the switch statement because I’m dealing with UIButtons. The rest of the switch statement compares the value of the UIButton element to see if it’s equal to YES (when the button is pressed), and if it is, it returns a string. I also want to know how to connect an IBAction to a UIImage (so when the images are tapped a subview opens).
I'm a little rusty on iOS development but you could probably do the following:
Set the buttons to the same event handler and use the sender attribute to get to the tag element of the button which you can specify to each button.
- (IBAction) doStuff:(id) sender {
UIButton *button = (UIButton*) sender;
switch(button.tag)
{
//do stuff
}
If this doesn't work out for you, you can use any of the button properties you see fit to differentiate between them such as title, title color and so on.
For best practices i'd advise you to also check if the sender is of type UIButton before trying to cast it into an object.
For Swift 3.0, we do not need to observe the tags any more. Just keep a reference to your buttons (IBOutlet or some private variable), and switch on the button itself, using the Identifier Pattern.
import UIKit
class Foo {
// Create three UIButton instances - can be IBOutlet too
let buttonOne = UIButton()
let buttonTwo = UIButton()
let buttonThree = UIButton()
init() {
// Assign the same selector to all of the buttons - Same as setting the same IBAction for the same buttons
[buttonOne, buttonTwo, buttonThree].forEach{(
$0.addTarget(self, action: Selector(("buttonTapped")), for: .touchUpInside)
)}
}
func buttonTapped(sender: UIButton) {
// Lets just use the Identifier Pattern for finding the right tapped button
switch sender {
case buttonOne:
print("button one was tapped")
case buttonTwo:
print("button two was tapped")
case buttonThree:
print("button three was tapped")
default:
print("unkown button was tapped")
break;
}
}
}
// Example
let foo = Foo()
foo.buttonTapped(sender: foo.buttonOne)

Resources