2

我正在尝试在我的目标 c 代码中包含一个力组件,因为它现在应该可以从 ios9 开始。我的代码包含以下两种方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

在 touchesEnded 方法中,我尝试使用以下代码行来获取触摸事件的力量:

for (UITouch *touch in touches) {
    CGFloat force = touch.force;
    CGFloat percentage = force/touch.maximumPossibleForce;
    NSLog(@"Printing force : %f", force);
    NSLog(@"Printing percentage: %f", percentage);
    NSLog(@"Printing maximum possible force: %f",    touch.maximumPossibleForce);
    break;
}

我试图在 iphone 6s 上运行我的代码,我得到了非常奇怪和矛盾的值,例如 force : 2.650000 和 force : 0.066667 即使我认为我几乎以同样的力量按下按钮。

我不想实现 peek 和 pop,而只是让用户通过一个按钮来控制具有不同选项的应用程序。

非常感谢你!

4

1 回答 1

1

我自己找到了答案,我猜越来越多的人会尝试在他们的应用程序中使用 3d touch,所以我希望这个解决方案能有所帮助:

关键是我必须创建一个 touchesMoved 方法并将我的代码放在那里,如下所示:

   - (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;
CGFloat normalizedForce = force/maximumPossibleForce;
NSLog(@"Normalized force : %f", normalizedForce);

if (normalizedForce > 0.75)
{
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
    NSLog(@"Strong");
} else {

NSLog(@"Weak");
}

}

如果用力触摸按钮,我的代码只会振动,仅此而已。

于 2015-10-06T12:31:58.840 回答