2011-03-07 3 views
4

아이폰의 uiwebview를 방문 할 때 누구나 자바 스크립트에서 방향 변경 이벤트를 처리하는 방법을 알려줄 수 있습니까?uiwebview에서 javacript onorientationchange 이벤트를 처리하는 방법

나는 onorientationchange 이벤트를 캡처하려고합니다. 나는 다음과 같은 코드를 시도했다.

<style type="text/css" media="only screen and (max-device-width: 480px)"> 
    .portrait 
    { 
     width: 300px; 
     padding: 0 15px 0 5px; 
    } 
    .landscape 
    { 
     width: 460px; 
     padding: 0 15px 0 5px; 
    } 
</style> 

<script type="text/javascript"> 
    window.onload = orientationchange; 
    window.onorientationchange = orientationchange; 
    function orientationchange() { 
     if (window.orientation == 90 
      || window.orientation == -90) { 
      document.getElementById('contents').className = 'landscape'; 
     } 
     else { 
      document.getElementById('contents').className = 'portrait'; 
     } 
    } 
</script> 

// and I have a div inside html 

<div id="contents"> 
    my contents and description... e.t.c, and e.t.c... 
</div> 

그것은 사파리에서 잘 작동하지만 방문 할 때 방향 변경 이벤트를있는 UIWebView 사용하여 페이지가 포착되지 않고 내 원하는 기능이 acheived하지 않을 수 있습니다.

답변

4

나는 this post: "iphone-uiwebview-local-resources-using-javascript-and-handling-onorientationchang"을 가리키고 싶습니다. 허용 된 답변 바로 아래의 답변이 귀하의 질문에 대한 찬사를 제공합니다. 응용 프로그램 코드의 방향 변경을 처리하고 자바 스크립트로 변경 사항을 주입하십시오. 나는 저자가 생각하지 않습니다 희망을 인용하고있다 :

"하지있는 UIWebView에서 호출되는 onorientationchange 핸들러의 두 번째 문제에 대한 답변 : 나는 window.orientation 속성이 제대로 작동하지 않는 것으로 나타났습니다

및 window.onorientationchange 처리기가 호출되지 않습니다. 대부분의 응용 프로그램은이 문제를 겪고 있습니다.이 문제는 window.orientation 속성을 설정하고 UIWebView가 포함 된보기 컨트롤러의 willRotateToInterfaceOrientation 메서드에서 window.onorientationchange를 호출하여 해결할 수 있습니다. "

+0

내가 필요가 없습니다 귀하의 웹보기를 호출 할 수 있습니다 로컬 리소스, 난 그냥 내 HTML의 자바 스크립트에서 오리 엔테이션 변경 이벤트를 캡처하고 싶습니다 –

+0

내가 실패한 당신을 보여 주려고 게시물에서 인용문을 추가했습니다. 실제로 당신이 진술 한 문제입니다. –

+0

예 감사합니다; 내 문제가 해결되었습니다. 그러나 응용 프로그램에서 방향이 설정된 html 페이지를 사용하는 것은 이상합니다. –

1

고마워.

이를 작성하는 또 다른 방법은 다음과 같습니다

var onorientationchange = function() { 
    document.getElementById('contents').className = 
     Math.abs(window.orientation == 90)? "landscape" : "portrait"; 
} 
window.onload = function() { 
    window.onorientationchange = window.onorientationchange 
} 
+0

잘 보입니다. 나중에 확인하고 작동하는지 알려 드리겠습니다. –

+0

답변 주셔서 감사합니다. 그러나 작동하지 않았습니다. –

+0

답변 주셔서 감사합니다.하지만 코드에 괄호 버그가 있습니다. 당신은 Math.abs (window.orientation) == 90이 아니라 비교의 절대 값이 필요합니다 ... – tyler

1

viewDidLoad이 넣어 :

[[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hasOrientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

- (void)hasOrientationChanged:(NSNotification *)notification { 
    UIDeviceOrientation currentOrientation; 
    currentOrientation = [[UIDevice currentDevice] orientation]; 
    if(currentOrientation == UIDeviceOrientationLandscapeLeft){ 
     NSLog(@"left"); 
    } 

    if(currentOrientation == UIDeviceOrientationLandscapeRight){ 
     NSLog(@"right"); 
    } 

} 

이제 올바른 방향으로 :

+0

네, 맞습니다. 하지만 내 html 페이지에서 어떻게 알 수 있습니까? –

관련 문제