类似于Instagram的强制触摸弹出模式

par*_*ent 10 cordova ionic-framework cordova-plugins 3dtouch ionic4

我正在尝试复制Instagram的强制触摸功能,其中

1)将手指放在图片上会变得更暗(悬停效果,简单)

2)稍微按一下,将显示内容的弹出模式预览

3)再按一次即可将模式扩展到全屏

我在使用Ionic 4 / Cordova“ 3d touch”插件时遇到问题,如果我先常规触摸屏幕,该插件不会注册强制触摸。

顺便说一句,当通过收听时,上述步骤2不会触发强制触摸 threeDeeTouch.watchForceTouches()

为了使听众触发,我必须先用力进入触摸,在“触摸”屏幕和“按下”屏幕之间没有延迟。如果我触摸屏幕但不按屏幕,则无法再按它来触发强制触摸而无需先抬起手指。

我正在真实的设备iPhone X上进行测试

如何解决此问题以在Instagram中复制强制触摸?

sug*_*oft 3

我也在离子科尔多瓦应用程序中尝试了相同的方法,尝试查看下面的内容以进一步进行

watchForceTouches() :当用户用力触摸 webview 时,您可以获得通知。该插件定义了当至少 75% 的最大力施加到屏幕上时的压力触摸。您的应用程序将收到 x 和 y 坐标,因此您必须弄清楚哪个 UI 元素被触摸。

我已经尝试过这种语法,并通过降级插件版本以及以下代码集来实现它

    @implementation ForceTouchRecognizer

double lastEvent = 0;

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch* touch in touches) {
        CGFloat percentage = (touch.force / touch.maximumPossibleForce) * 100;
        if (percentage >= 75) {
            // let's not flood the callback with multiple hits within the same second
            NSTimeInterval ts = touch.timestamp;
            int diff = ts - lastEvent;
            if (diff > 0) {
                lastEvent = ts;
                CGPoint coordinates = [touch locationInView:self.view];
                NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                               [NSString stringWithFormat:@"%d", (int)percentage]   , @"force",
                                               [NSString stringWithFormat:@"%d", (int)coordinates.x], @"x",
                                               [NSString stringWithFormat:@"%d", (int)coordinates.y], @"y",
                                               // no need to use the touch.timestamp really since it's simply 'now'
                                               [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]], @"timestamp",
                                               nil];

                CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
                pluginResult.keepCallback = [NSNumber numberWithBool:YES];
                [_commandDelegate sendPluginResult:pluginResult callbackId:_callbackId];
            }
        }
    }
}
@end
Run Code Online (Sandbox Code Playgroud)