Creating doubleClick code for bluetooth button - ios

I have received a bluetooth button which you can connect your program to, however the double click is not working for it. All the other events (up, down, click, hold) are working and calling the correct delegate.
I want somehow to manage double clicks from code.
How can I make it work, for example from the up event. (for example, if second click comes within 0.5s it behaves as a double click, if no then simple click)
this probably makes a 0.5s delay but thats acceptable...

devButton.addTarget(self, action: #selector(taps(_:event:)), for: UIControlEvents.touchDownRepeat)
func taps(_ sender: UIButton, event: UIEvent) {
let t: UITouch = event.allTouches!.first!
if (t.tapCount == 2) { devFunction() }
}

Connect your button to the VC and add a double click listener.
yourButton.addTarget(self, action: #selector(doubleTapped), for: .touchDownRepeat)
Then handle the double click event in a function like this
func doubleTapped() {
// do something cool here
}

Related

How to know when focused element is refocused in voice over (iOS)

I want to do something when the focused voice-over element is being tapped again.
Function accessibilityElementDidBecomeFocused() will only get called when the element is focused for first time.
When we are again single tapping on the same focused element then this function won't get called. Can anybody suggest some solution?
Can anybody suggest some solution?
Here are some ideas on the fly to detect a single-tap on the same focused element:
Create a variable nbSelections that will count the number of single-taps.
Create a tap gesture recognizer on your element to increment the number of taps for instance:
let tap = UITapGestureRecognizer(target: self,
action: #selector(addTapCounter(info:)))
tap.numberOfTapsRequired = 1
self.addGestureRecognizer(tap)
Add the trait that will allow to catch the simple tap directly on the element:
override var accessibilityTraits: UIAccessibilityTraits {
get { return .allowsDirectInteraction }
set { }
}
Set nbSelections = 0 when the element loses the focus:
override open func accessibilityElementDidLoseFocus() { nbSelections = 0 }
Combining these ideas with the UIAccessibilityFocus informal protocol could be a good line of research to reach your goal.
However, this technical solution assumes that the single-tap is made directly on the element itself (-trait specific) and not anywhere else (I don't see how to catch this event the top off my head).

iOS: Why does hidden button still receive tap events?

According to the Apple docs, hidden UIButtons should not receive tap events.
However, our app has a UIButton receiving tap events despite being hidden.
This function is the IB Action invoked when the button is tapped. When the button is removed from Storyboard, this function doesn't get invoked. When the button is added to Storyboard, the function gets invoked -- even though the button is hidden.
To verify that the button is hidden, we put a breakpoint inside the function and ran expr sender.hidden from the Xcode debugger. The result: true.
The stack trace shows the IB Action is triggered by code in UIApplicationMain, not our code.
Through the Connections Inspector, we confirmed there is no other trigger for the IB Action except the mysterious button.
Thoroughly confused. Suggestions?
#IBAction func buttonTapped(sender: UIButton) {
// If here, handle tap
...
}
Try to set enable = false like this:
button.enabled = false
For Swift 3 would be:
button.isEnabled = false
The problem was an incomplete UIButton extension that didn't account for visibility in determining hit tests.
This function correctly handles the case where UIButtons are hidden.
extension UIButton {
public override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
// Ignore if button hidden
if self.hidden {
return nil
}
// If here, button visible so expand hit area
let hitSize = CGFloat(56.0)
let buttonSize = self.frame.size
let widthToAdd = (hitSize - buttonSize.width > 0) ? hitSize - buttonSize.width : 0
let heightToAdd = (hitSize - buttonSize.height > 0) ? hitSize - buttonSize.height : 0
let largerFrame = CGRect(x: 0-(widthToAdd/2), y: 0-(heightToAdd/2), width: buttonSize.width+widthToAdd, height: buttonSize.height+heightToAdd)
return (CGRectContainsPoint(largerFrame, point)) ? self : nil
}
}
A situation I encountered was that I was toggling between two different buttons based on application state. When I created the second of these two buttons, I copied and pasted the first. This also copied the first button's outlets.
I thought that the first button was being pressed when only the second button was showing, but in reality the second button was sending events to both the outlet I intended AND the one that was set when I copied the first button.
To determine if this is the case in your situation, go to the interface builder, select your button, and check that the touch events are set up exactly how you want in the connections inspector. If any of the connections are wrong, you can remove them by clicking the little 'x'.

How to use tap gesture in Accessibility in swift? [duplicate]

I'm working on app for blind people first i needed to use swipe gesture and after search i can do it by using
override func accessibilityScroll( direction:
UIAccessibilityScrollDirection)-> Bool {
if direction == UIAccessibilityScrollDirection.Left
{
Do what you want to do
}
}
Now I need to use tap gesture and I can't find any function to use make custom tap in Accessibility can anyone help me pleasee!
Thanks
You shouldn't use tap gesture for UIAccessibility ot try to add tap gestures to UIAccessibility.UIAccessibility is meant for the UIElements where the people can see/hear what is on the screen. You cannot add UIAccessiility for tap gesture functions. What you can do is to post a accessibility notification to the user saying tap to perform something. Again, you need to use buttons to perform an action in UIAccessibility.
You can override accessibilityScroll method:
override func accessibilityScroll(_ direction:
UIAccessibilityScrollDirection) -> Bool {
super.accessibilityScroll(direction)
if direction == UIAccessibilityScrollDirection.left {
// do your stuff
}
if direction == UIAccessibilityScrollDirection.right {
// do your stuff
}
return true
}

Touch Up Inside not working properly

I have an app with some buttons, when those buttons are pressed the image on them should change. I assume that the TouchUpInside runs when you tap and remove the finger while still holding inside the area of the element, however it only works rarely and I'm not sure why.
The reason I use TouchUpInside instead of TouchDown is because I want the user to be able to cancel the action.
I'm sorry if I've misunderstood anything about those events and if this has already been asked. I couldn't find an answer to my problem searching the web.
//The IBAction is set to trigger on TouchUpInside
#IBAction func action11(sender: UIButton) {
setTile(sender)
}
func setTile(sender: UIButton) {
if turn {
print("O's turn")
sender.setImage(xTile, forState: .Normal)
turn = false
}
}
EDIT: Added the necessary code
There are some properties of UIButtons which you can use to achieve what you want.
You can use Default and selected state of uibutton to set two different images.
In XIB select state "Default" and assign default image to that state again select state to "Selected" and assign image which you want after button section.
and add following line in button selection method.
-(IBAction)buttonTapped:(UIButton *)sender{
sender.selected = !sender.selected;
}
Your understanding is correct, you need to use touchUpInside.
I assume you are trying to create a button that has a toggle function. On one touch you want the button to have the value Say "X" and when touched again the button has a value "O".
Take a look at this code below, this should do the job.
class ViewController: UIViewController {
var isButtonPressed = false{
// Adding a Property Observer, that reacts to changes in button state
didSet{
if isButtonPressed{
// Set the Value to X.
}else{
// Set the Value to O.
}
}
}
#IBAction func changeButtonValue(sender: UIButton) {
// Toggle the button value.
isButtonPressed = !isButtonPressed
}
}
If you don't set turn=true after the first time, this code is executed it will be executed only one.
if turn {
print("O's turn")
sender.setImage(xTile, forState: .Normal)
turn = false
}
Check if the button frame is large enough to get finger touch.
Apple says at least 35x35 pixel.

Is TouchUpOutside the correct control event for events outside of an element?

I'm three days new into swift development, even Xcode for that matter. I've created a UIView called EncounterMenu that appears over my app when i click the menu button. My goal is to have the EncounterMenu UIView close when I do anything outside of it.. This is what I have so far
override func viewDidLoad() {
super.viewDidLoad()
//create a button that spans the width & height of the encounter menu
let btnEncounterMenu = UIButton(type: UIButtonType.System) as UIButton
btnEncounterMenu.frame = CGRectMake(0, 0, EncounterMenu.frame.width, EncounterMenu.frame.height)
//(this is whats not working) If user clicks outside of button, call function "closeEncounter:"
btnEncounterMenu.addTarget(self, action: "closeEncounter:", forControlEvents: .TouchUpOutside)
//Add the button to the EncounterMenu
EncounterMenu.addSubview(btnEncounterMenu)
}
func closeEncounter(sender: UIButton!) {
EncounterMenu.hidden = true;
}
I tried changing it to TouchUpInside and it worked when i clicked inside the EncounterMenu UIView, so I figured it should be as easy as TouchUpOutside?
Any direction to what I'm doing wrong or how I can accomplish what I'm trying to do?
Touch up outside will not work because you need to be pressing on the menu first, then drag your finger outside for that to register. instead, the easiest option for you to do is to create a button that is the entire size of your view, and on that buttons touch down event, fire off close menu. just make sure that this big button is at the very back of the view, and everything else is built on top of it.

Resources