Detect What Is The Location of The Tap/Press Inside UIbutton Inside Sender Action Method

Detect what is the location of the tap/press inside UIButton inside sender action method

Create the action function with sender and event params. Then you can get the touch location on the button.

@IBAction func buttonAction(sender: AnyObject, event: UIEvent) {
// downcast sender as a UIView
let buttonView = sender as UIView;

// get any touch on the buttonView
if let touch = event.touchesForView(buttonView)?.anyObject() as? UITouch {
// print the touch location on the button
println(touch.locationInView(buttonView))
}
}

Obtain location of UIButton press in swift

sender is a BNDrumPad which is (presumably) a subclass of a UIView, so in this case you will just use sender. In the answer you linked to, the sender parameter was defined as AnyObject so they had to cast it to a UIView first.

@IBAction func drumPadPressed(sender: BNDrumPad, event: UIEvent) {

//...code for function here

// get any touch on the buttonView
if let touch = event.touchesForView(sender)?.anyObject() as? UITouch {
// print the touch location on the button
println(touch.locationInView(buttonView))
}

//...more code for function here
}

Location of touch in UIButton action

Swift 2:

@IBAction func buttonPressed(button: UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches()?.first else { return }
let point = touch.locationInView(button)
print ("point: \(point)")
}

Swift 3:

@IBAction func buttonPressed(_ button: UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: button)
print ("point: \(point)")
}

To create an IBAction with event pick the right option in this popup in your storyboard:

Sample Image

UIButton TouchUpInside Touch Location

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

That's the right idea, except that this code is probably inside an action in your view controller, right? If so, then self refers to the view controller and not the button. You should be passing a pointer to the button into -locationInView:.

Here's a tested action that you can try in your view controller:

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
UIView *button = (UIView *)sender;
UITouch *touch = [[event touchesForView:button] anyObject];
CGPoint location = [touch locationInView:button];
NSLog(@"Location in button: %f, %f", location.x, location.y);
}

figure out which button was pressed while differ between long-press and tap. swift

The main issue, in this case, is both gestures will be added ONLY for the largeOption button! To clarify, the gesture is added only for one component, which in your case, it should be added only for the latest one (which is largeOption):

smallOption.addGestureRecognizer(tapGesture) <-- skipped
mediumOption.addGestureRecognizer(tapGesture) <-- skipped
largeOption.addGestureRecognizer(tapGesture) <-- added
smallOption.addGestureRecognizer(longGesture) <-- skipped
mediumOption.addGestureRecognizer(longGesture) <-- skipped
largeOption.addGestureRecognizer(longGesture) <-- added

Logically speaking, this might be the answer to your question:

Is it possible to find out what button was pressed in the tap and long function, or will I need to do two functions for each button?

you need to add two gestures for each button because a particular gesture can only be added to one view.

However, you don't have to declare new action methods in addition to @objc func tap(_ sender: UIGestureRecognizer) and @objc func long(_ sender: UIGestureRecognizer) existing ones. What you could do instead is to check the sender's view. Example:

Let's assume that we manually added tow gestures for each button:

// gestures:
let smallOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let smallOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
smallOptionLongGesture.minimumPressDuration = 0.5

let mediumOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let mediumOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
mediumOptionLongGesture.minimumPressDuration = 0.5

let largeOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let largeOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
largeOptionLongGesture.minimumPressDuration = 0.5

// adding them:
smallOption.addGestureRecognizer(smallOptionTapGesture)
mediumOption.addGestureRecognizer(mediumOptionTapGesture)
largeOption.addGestureRecognizer(largeOptionTapGesture)

smallOption.addGestureRecognizer(smallOptionLongGesture)
mediumOption.addGestureRecognizer(mediumOptionLongGesture)
largeOption.addGestureRecognizer(largeOptionLongGesture)

Therefore, what you could do is:

@objc func tap(_ sender: UIGestureRecognizer) {
// an example of how you could check the button
if sender.view == smallOption {
print("small short-press")
} else if sender.view == mediumOption {
print("medium short-press")
} else if sender.view == largeOption {
print("large short-press")
}
}
@objc func long(_ sender: UIGestureRecognizer) {
// you could apply the same above approach here
}

The other option is to create action methods for each button separately.

Detecting which UIButton was pressed in a UITableView

In Apple's Accessory sample the following method is used:

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

Then in touch handler touch coordinate retrieved and index path is calculated from that coordinate:

- (void)checkButtonTapped:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil)
{
...
}
}

How do I get the tap coordinates on a custom UIButton?

To get touch location you can use another variant of button action method: myAction:forEvent: (if you create it from IB interface note "sender and event" option in arguments field: Sample Image)

Then in your action handler you can get touch location from event parameter, for example:

- (IBAction)myAction:(UIButton *)sender forEvent:(UIEvent *)event {
NSSet *touches = [event touchesForView:sender];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:sender];
NSLog(@"%@", NSStringFromCGPoint(touchPoint));
}

swift identify which button was pressed

Do something like this,

var index:Int = 0

Now do this, FYI, set button tags accordingly 1,2 and 3...

@IBAction func btn1(_ sender: UIButton) {
index = sender.tag
}

@IBAction func btn2(_ sender: UIButton) {
index = sender.tag
}

@IBAction func btn3(_ sender: UIButton) {
index = sender.tag
}

now this,

@IBAction func idenfifywhichpressed(_ sender: UIButton) {
if index == 1 {
//Button 1 Pressed
}else if index == 2 {
//Button 2 Pressed
} else if index == 3{
//Button 3 Pressed
}
}

FYI. You can use Switch statement as well in idenfifywhichpressed method.

Update. Do not create three methods for btn1, btn2 and btn3, just create one method and assign tag accordingly for simplicity.

UIButton with single press and long press events swift

If you want to perform any action with single tap you and long press the you can add gestures into button this way:

@IBOutlet weak var btn: UIButton!

override func viewDidLoad() {

let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}

@objc func tap() {

print("Tap happend")
}

@objc func long() {

print("Long press")
}

This way you can add multiple method for single button and you just need Outlet for that button for that..



Related Topics



Leave a reply



Submit