ボタンをタップまたはドラッグするときのタッチ力を測定したい。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);
}