如何检测显示AMP(加速移动页面)的WKWebView上的滚动

时间:2018-01-30 12:40:22

标签: ios wkwebview accelerated-mobile-page

我有一个<?php include 'config.php'; ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script type="text/javascript" src="https://www.youtube.com/player_api"> </script> </head> <body> <center style="margin-left:0px"> <label style="font-size:2em;color:tomato"><b>Today's Video</b></label> <table> <tr> <td> <?php $sql="SELECT `video_path` FROM `videos` WHERE `category`='Films' and postedby='Admin' and id in (select max(id) as id FROM `videos` WHERE `category`='Films')"; $res=mysqli_query($db,$sql); if($row=mysqli_fetch_array($res)) { $vid=$row['video_path']."? rel=0&wmode=Opaque&enablejsapi=1;showinfo=1;controls=1"; echo '<iframe src='.$vid.' class="yt_players" height="150" width="150" id="player" frameborder="0" allowfullscreen="allowfullscreen" ></iframe> <br/>'; } ?> </td> <td> <?php $sql="SELECT `video_path` FROM `videos` WHERE `category`='Political' and postedby='Admin' and id in (select max(id) as id FROM `videos` WHERE `category`='Political')"; $res=mysqli_query($db,$sql); if($row=mysqli_fetch_array($res)) { $vid=$row['video_path']."? rel=0&wmode=Opaque&enablejsapi=1;showinfo=1;controls=1"; echo '<iframe src='.$vid.' class="yt_players" height="150" width="150" id="player1" frameborder="0" allowfullscreen="allowfullscreen" > </iframe><br/>'; } ?> </td> </tr> <tr><td><br/></td><td><br/></td></tr> <tr> <td> <?php $sql="SELECT `video_path` FROM `videos` WHERE `category`='Shopping' and postedby='Admin' and id in (select max(id) as id FROM `videos` WHERE `category`='Shopping')"; $res=mysqli_query($db,$sql); if($row=mysqli_fetch_array($res)) { $vid=$row['video_path']."? rel=0&wmode=Opaque&enablejsapi=1;showinfo=1;controls=1"; echo '<iframe src='.$vid.' height="150" class="yt_players" width="150" id="player2" frameborder="0" allowfullscreen="allowfullscreen" > </iframe><br/>'; } ?> </td> <td> <?php $sql="SELECT `video_path` FROM `videos` WHERE `category`='Services' and postedby='Admin' and id in (select max(id) as id FROM `lookon_videos` WHERE `category`='Services')"; $res=mysqli_query($db,$sql); if($row=mysqli_fetch_array($res)) { $vid=$row['video_path']."? rel=0&wmode=Opaque&enablejsapi=1;showinfo=1;controls=1"; echo '<iframe src='.$vid.' class="yt_players" height="150" width="150" id="player3" frameborder="0" allowfullscreen="allowfullscreen" ></iframe> <br/>'; } ?> </td> </tr> </table><br/> <script type="text/javascript"> var players = new Array(); function onYouTubeIframeAPIReady() { var temp = $("iframe.yt_players"); for (var i = 0; i < temp.length; i++) { var t = new YT.Player($(temp[i]).attr('id'), { events: { 'onStateChange': onPlayerStateChange } }); players.push(t); } } onYouTubeIframeAPIReady(); function onPlayerStateChange(event) { if (event.data == YT.PlayerState.PLAYING) { var temp = event.target.a.src; // var tempPlayers = $("iframe.yt_players"); for (var i = 0; i < players.length; i++) { if (players[i].a.src != temp) players[i].stopVideo(); } } } </script> </center> </body> </html> ,我需要知道它何时滚动,所以我将其滚动视图的委托绑定到我的控制器,以便使用WKWebView委托方法:

UIScrollView

它适用于几乎所有网页,但当用户进行谷歌搜索时,选择一个AMP页面,然后向上或向下滚动,我无法检测到滚动。

有没有办法知道AMP页面是否滚动?

2 个答案:

答案 0 :(得分:0)

我希望这有效(它适用于我),存储lastContentOffset的Y位置并将其与当前进行比较。

private var lastContentOffset: CGFloat = 0

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (self.lastContentOffset > scrollView.contentOffset.y) {
         print("move up")
    }
    else if (self.lastContentOffset < scrollView.contentOffset.y) {
        print("move down")
    }
    self.lastContentOffset = scrollView.contentOffset.y
}

答案 1 :(得分:0)

class ViewController: WKNavigationDelegate, UIScrollViewDelegate

var webView: WKWebView!

override func loadView() {
        super.loadView()
        webView = WKWebView()
        webView.navigationDelegate = self
        webView.scrollView.delegate = self
        view = webView
    }

func scrollViewDidScroll(_ scrollView: UIScrollView) {  
        print(scrollView.contentOffset.y)
    }