Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering a UIButton's method when user drags finger into button's area?

I want to have a button that triggers its method under the following conditions:

  • When the user taps the button
  • When the user presses the button and drags his/her finger out of the button's area
  • When the user drags his/her finger from outside the button's area to inside the button's area

Basically, anytime any part of the button is touched, regardless of origin of touch, I want the method triggered, but want to accomplish this by manipulating UIButton properties, like touchedUpInside and such.

Thanks for the suggestions!

like image 842
jake9115 Avatar asked Jun 13 '13 01:06

jake9115


People also ask

How do I trigger a button from the input field?

Press the "Enter" key inside the input field to trigger the button: // Get the input field. var input = document.getElementById("myInput"); // Execute a function when the user releases a key on the keyboard.

How to trigger a button on keyboard with JavaScript?

Trigger a button click on keyboard "enter" with JavaScript. Press the "Enter" key inside the input field to trigger the button: // Get the input field. var input = document.getElementById("myInput"); // Execute a function when the user releases a key on the keyboard. input.addEventListener("keyup", function(event) {.

How to make one swift button action process?

How To Make One Swift Button Action Process Method Handle Multiple Button On-Click Event. If there are multiple swift buttons in one iOS app, we can also use one method to process all those buttons’ on-click events. For example, if there are 3 buttons in the above example, btn1, btn2, and btn3.

How to connect all 3 buttons to the onclick Swift button action?

The first method is to connect all the 3 buttons to the onClick swift button action process method follow section 2.1 Add Button onClick Event Handler Method. Then distinguish each button in the onClick method by the sender parameter.


2 Answers

Regarding:

  • When the user presses the button and drags his/her finger out of the button's area

I recently have done something similar. Here is my code in Swift.

(In this example you subclassed UIButton, like: class FadingButton: UIButton)

...

// The user tapped the button and then moved the finger around.
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)

    // Get the point to which the finger moved
    if let point: CGPoint = touches.first?.location(in: self) {

        // If the finger was dragged outside of the button...
        if ((point.x < 0 || point.x > bounds.width) ||
            (point.y < 0 || point.y > bounds.height)) {

            // Do whatever you need here
        }
    }
}
...

I hope it helps someone.

like image 181
backslash-f Avatar answered Oct 13 '22 01:10

backslash-f


Make you method such as -(IBAction)btnTap and connect to these properties

  • When the user taps the button. - Use Touch Down method for this
  • When the user presses the button and drags his/her finger out of the button's area - Use Touch Up Inside for this purpose
  • When the user drags his/her finger from outside the button's area to inside the button's - Touch Drag Inside
like image 36
Divyu Avatar answered Oct 13 '22 01:10

Divyu