0

ボタンをタップまたはドラッグするときのタッチ力を測定したい。UITapGestureRecognizer (タップ用) を作成し、次のように myButton に追加しました。

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

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

次のようなbuttonPrssedというメソッドを作成しました。

-(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);

}

タッチに対してゼロ値 (0.0000) を取得し続けます。ヘルプやアドバイスをいただければ幸いです。検索を行ったところ、DFContinuousForceTouchGestureRecongnizer サンプル プロジェクトが見つかりましたが、複雑すぎることがわかりました。タッチ機能のあるiPhone 6 Plus sを使っています。このコードを使用して、画面の他の領域をタップしたときのタッチを測定することもできますが、ボタンをタップすることはできません。

   - (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);




}
4

1 に答える 1

0

これが呼び出されたときにユーザーがすでに指を離しているため、あなたは0.0000入っています。buttonPressed

メソッドで力を取得する必要があることは正しいですがtouchesMoved、UIButton のtouchesMovedメソッドでそれを取得する必要があります。そのため、UIButton をサブクラス化し、その touchesMoved メソッドをオーバーライドする必要があります。

ヘッダー ファイル:

#import <UIKit/UIKit.h>

@interface ForceButton : UIButton

@end

実装:

#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

UITapGestureRecognizerまた、の 1 回のタップを検出するために を使用する必要はありませんUIButtonaddTarget代わりに使用してください。

于 2015-12-15T08:43:22.927 に答える