簡體   English   中英

iOS webView向左或向右滑動以進行導航

[英]iOS webView swipe left or right to navigate

我正在制作一個簡單的webView iOS 7應用程序,我希望用戶能夠向左或向右滑動以在網頁上前進或后退。 就像你在iOS中的safari一樣。

我認為它被稱為分頁 - 但我不知道如何使用它。 我該如何實現呢? 我是個菜鳥,所以如果你能一步一步地告訴我,我會很感激。

謝謝

我正在使用我的應用程序中的類似功能,這是我到目前為止已經想到的 - 我還沒有完成,但這是一種開始的方法。

首先,我假設您僅針對iOS 7進行開發 - 雖然完全有可能為iOS 6構建這個,但如果您只開發7個,則可以采用幾個簡潔的快捷方式。

使用UIScreenEdgePanGestureRecognizer處理前進/后退手勢。 您將其添加到Web視圖,如下所示:

    UIScreenEdgePanGestureRecognizer *bezelSwipeGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeBack:)];
bezelSwipeGestureRecognizer.edges = UIRectEdgeLeft;
bezelSwipeGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:bezelSwipeGestureRecognizer];

UIView *invisibleScrollPreventer = [UIView new];
invisibleScrollPreventer.frame = CGRectMake(0, 0, 10, self.view.frame.size.height);
[self.view addSubview:invisibleScrollPreventer];

這將添加反滑動手勢。 棘手的部分是invisibleScrollPreventer - 它是一個不完美的黑客,但它將避免滾動你的網頁視圖而不是做后退動作。 (可能有更好的方法來解決這個問題,但這是我目前的解決方案。)

swipeBack:方法中,您將執行以下操作:

-(void)swipeBack:(UIScreenEdgePanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        if (_webView.canGoBack) {
            [_webView goBack];
        }
    }
}

您可以執行以下相同操作,將goForward:方法添加到Web視圖中。

真正的訣竅是讓你回到(或轉發)的網頁在你向后或向前滑動時出現在屏幕上,就像iOS 7中的Safari一樣。我不是百分百肯定如何做到這一點(我還沒有在我的應用程序中構建該功能,但我很快就會這樣做)而且我猜它是一個應用了一些變暗的iOS 7 snapshotView

根據您選擇構建此項的方式,您可能還需要查看iOS 7中的自定義導航過渡 我不知道這個問題是否需要,但可能是。

祝好運!

override func viewDidLoad() {
    super.viewDidLoad()
    let url = NSURL(string: "https://www.whatever.com")
    let requestObj = NSURLRequest(URL: url!)
    myWebView.loadRequest(requestObj)



    //swipe left or right to go back or foward 
    var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

    leftSwipe.direction = .Left
    rightSwipe.direction = .Right

    view.addGestureRecognizer(leftSwipe)
    view.addGestureRecognizer(rightSwipe)


}



//swipe left to go forward
func handleSwipes(sender:UISwipeGestureRecognizer) {
    if (sender.direction == .Left) {
        myWebView.goForward()

    }
//swipe right to go backward
    if (sender.direction == .Right) {
         myWebView.goBack()

    }


}

你可以試試這個:

var webView: WKWebView!

 override func viewDidLoad() {
       super.viewDidLoad()

        let url = URL(string: "https://yourwebsite.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true  //so easy!
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM