我正在尝试在我的目标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;
}我试图在iPhone6s上运行我的代码,我得到了非常奇怪和矛盾的值,例如力: 2.650000和力: 0.066667,尽管我认为我几乎以相等的力量按下了按钮。
我不想实现偷看和弹出,但只是让用户控制应用程序的不同选项在一个按钮。
非常感谢您的提前!
发布于 2015-10-06 20:31:59
我自己找到了答案,我猜越来越多的人会尝试在他们的应用程序中使用3d触摸,所以我希望这个解决方案能有所帮助:
关键是我必须创建一个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");
}}
我的代码很简单,如果按钮被用力触摸,就会振动,仅此而已。
https://stackoverflow.com/questions/32911573
复制相似问题