0

I want to measure the touch force when tapping or dragging a button. i have created a UITapGestureRecognizer (for tapping) and added it to myButton like this:

UITapGestureRecognizer *tapRecognizer2 = [[UITapGestureRecognizer      alloc] initWithTarget:self action:@selector(buttonPressed:)];

         [tapRecognizer2 setNumberOfTapsRequired:1];
        [tapRecognizer2 setDelegate:self];
        [myButton addGestureRecognizer:tapRecognizer2];

i have created a method called buttonPrssed like this:

-(void)buttonPressed:(id)sender 
{
    [myButton touchesMoved:touches withEvent:event];


   myButton = (UIButton *) sender;

    UITouch *touch=[[event touchesForView:myButton] anyObject];

    CGFloat force = touch.force;
    forceString= [[NSString alloc] initWithFormat:@"%f", force];
    NSLog(@"forceString in imagePressed is : %@", forceString);

}

I keep getting zero values (0.0000) for touch. any help or advice would be appreciated. i did a search and found DFContinuousForceTouchGestureRecongnizer sample project but found it too complicated. I use iPhone 6 Plus s that has touch. i can also measure touch when tapping on any other area in the screen but not on buttons using this code:

   - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    //CGFloat maximumPossibleForce = touch.maximumPossibleForce;
    CGFloat force = touch.force;
    forceString= [[NSString alloc] initWithFormat:@"%f", force];
    NSLog(@"forceString is : %@", forceString);




}
Piyush Gupta
  • 2,181
  • 3
  • 13
  • 28
Jeff Sab
  • 13
  • 1

1 Answers1

0

You are getting 0.0000 in buttonPressed because the user already lifted his finger when this is called.

You are right, that you need to get the force in the touchesMoved method, but you need to get it in the UIButton's touchesMoved method. Because of that you need to subclass UIButton and override its touchesMoved method:

Header file:

#import <UIKit/UIKit.h>

@interface ForceButton : UIButton

@end

Implementation:

#import "ForceButton.h"

@implementation ForceButton

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGFloat force = touch.force;
    CGFloat relativeForce = touch.force / touch.maximumPossibleForce;

    NSLog(@"force: %f, relative force: %f", force, relativeForce);
}

@end

Also, there is no need to use a UITapGestureRecognizer to detect a single tap on a UIButton. Just use addTarget instead.

joern
  • 27,354
  • 7
  • 90
  • 105
  • Joern, i followed your steps and created forceButton as subclass to UIButton. the button i want to measure the touch force for it is called myButton. i have added a target : [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; i created a method called -(void)buttonPressed:(id)sender but how do i call the method touchesMoved from forceButton inside buttonPressed method? – Jeff Sab Dec 23 '15 at 16:52
  • @JeffSab: You never call `touchesMoved` yourself. That method will automatically be called by the system whenever the user touches the button and the finger moves a little during that touch (which it always does on an actual device). – joern Dec 23 '15 at 17:00
  • joern, am missing something here. don't i need to call something in the added target buttonClicked so that when i click on myButton it gets me the force data? how should my buttonClicked method look like? – Jeff Sab Dec 23 '15 at 18:39
  • The `buttonClicked` method is called after the user lifted his finger. So you won't get any force value then. What are you trying to achieve? Do you want to trigger an action when a user taps the button or do you want to trigger an action when the user applies a certain force to the button? For the first option you use your `buttonClicked` method, for the second option you check the force in `touchesMoved` and do something with the force data. – joern Dec 23 '15 at 20:23
  • OK, i want to measure the force when touching the button and also trigger an action. so i declared forceButton *myButton in my viewController .h file. in the .m file i use the following: myButton = [[forceButtn alloc] initWithFrame:CGRectMake(xButtonPosition, yButtonPosition,120,120)]; [backGroundView addSubview:myButton]; [myButton setTag:i]; [myButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; so while it sends force data the problem is that the added action (buttonPressed) doesn't work, any idea? – Jeff Sab Jan 01 '16 at 14:28