如何将 3dtouchforce 添加到 UIButton?
how to add 3dtouchforce to UIButton?
我想测量点击或拖动按钮时的触摸力。我创建了一个 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 示例项目,但发现它太复杂了。我用的是有触摸的iPhone6 Plus。我也可以在点击屏幕上的任何其他区域而不是按钮时使用此代码测量触摸:
- (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);
}
您在 buttonPressed
中得到 0.0000
,因为用户在调用时已经抬起手指。
你是对的,你需要在touchesMoved
方法中获取force,但是你需要在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
来检测 UIButton
上的单击。只需使用 addTarget
即可。
我想测量点击或拖动按钮时的触摸力。我创建了一个 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 示例项目,但发现它太复杂了。我用的是有触摸的iPhone6 Plus。我也可以在点击屏幕上的任何其他区域而不是按钮时使用此代码测量触摸:
- (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);
}
您在 buttonPressed
中得到 0.0000
,因为用户在调用时已经抬起手指。
你是对的,你需要在touchesMoved
方法中获取force,但是你需要在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
来检测 UIButton
上的单击。只需使用 addTarget
即可。