首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在objective-c和3dtouch中对UITouch使用不同的力

在objective-c和3dtouch中对UITouch使用不同的力
EN

Stack Overflow用户
提问于 2015-10-03 00:20:38
回答 1查看 1.4K关注 0票数 2

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

代码语言:javascript
复制
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

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

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

代码语言:javascript
复制
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,尽管我认为我几乎以相等的力量按下了按钮。

我不想实现偷看和弹出,但只是让用户控制应用程序的不同选项在一个按钮。

非常感谢您的提前!

EN

回答 1

Stack Overflow用户

发布于 2015-10-06 20:31:59

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

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

代码语言:javascript
复制
   - (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");
}

}

我的代码很简单,如果按钮被用力触摸,就会振动,仅此而已。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32911573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档