评估Javascript时,在UIWebview中启用方向

Enable Orientation in UIWebview when Evaluating Javascript

本文关键字:启用 方向 UIWebview Javascript 评估      更新时间:2023-09-26

当我开始评估javascript时,比如当用户选择文本时,高亮显示所选文本。对于这种情况,我无法在iPad中继续设备方向,并停止方向,直到javascript完全执行,它才处于活动状态。

有没有任何方法可以在不影响uiwebview方向的情况下处理uiwebview中javascript的评估。

当然。当您启动javascript时,您可以设置一个布尔var,并设置一个具有UIViewController当前方向的var(两个var都在UIViewController实例中),那么在UIViewController中,您必须实现以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

这是一种在用户旋转设备时告诉iPhone是否可以旋转应用程序的方法。

因此,当您的javascript启动时:

//BOOL停止旋转;//在您的ViewController.h中声明它stopRotation=TRUE;//UI界面方向*currentOrientationFrozen;//在您的ViewController.h中声明它currentOrientationFrozen=self.interfaceOrientation;

和:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if (stopRotation){
             if (interfaceOrientation == currentOrientationFrozen) {
                 return YES;
             } else {
                 return NO;
             }
        } 
        return YES;
    }

然后,当javaScript停止时,恢复BOOL和currentOrientationFrozen:

stopRotation = FALSE;
currentOrientationFrozen = nil;