Created
July 18, 2019 12:00
-
-
Save Haeuncs/6e3b6149a54943ce7f7bf86f59ad6468 to your computer and use it in GitHub Desktop.
segment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// testWeb | |
// | |
// Created by LEE HAEUN on 2019/07/18. | |
// Copyright © 2019 LEE HAEUN. All rights reserved. | |
// | |
import UIKit | |
import WebKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
initView() | |
setupWebViewGesture() | |
} | |
func initView(){ | |
view.addSubview(myWebView) | |
view.addSubview(segment) | |
NSLayoutConstraint.activate([ | |
myWebView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), | |
myWebView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
myWebView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
myWebView.bottomAnchor.constraint(equalTo: segment.topAnchor), | |
segment.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
segment.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
segment.heightAnchor.constraint(equalToConstant: 60), | |
segment.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor) | |
]) | |
} | |
let segArr = ["facebook","twitter","apple","google"] | |
let urlArr = [ | |
URL(string: "https://www.facebook.com/"), | |
URL(string: "https://www.twitter.com/"), | |
URL(string: "https://www.apple.com/"), | |
URL(string: "https://www.google.com/") | |
] | |
lazy var myWebView : WKWebView = { | |
let view = WKWebView() | |
//view.allowsBackForwardNavigationGestures = true | |
view.translatesAutoresizingMaskIntoConstraints = false | |
return view | |
}() | |
lazy var segment: UISegmentedControl = { | |
let segment = UISegmentedControl(items: segArr) | |
segment.translatesAutoresizingMaskIntoConstraints = false | |
segment.addTarget(self, action: #selector(segmentEvent), for: .valueChanged) | |
return segment | |
}() | |
} | |
extension ViewController { | |
func setupWebViewGesture(){ | |
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:))) | |
let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:))) | |
swipeLeftRecognizer.direction = .left | |
swipeRightRecognizer.direction = .right | |
myWebView.addGestureRecognizer(swipeLeftRecognizer) | |
myWebView.addGestureRecognizer(swipeRightRecognizer) | |
} | |
///segment | |
@objc func segmentEvent(){ | |
let url: URL? | |
url = urlArr[segment.selectedSegmentIndex] | |
// url 이 nil 이면 return | |
guard let checkURL = url else{ | |
return | |
} | |
let requset: URLRequest = URLRequest(url: checkURL) | |
myWebView.stopLoading() | |
myWebView.load(requset) | |
} | |
//제스쳐 | |
@objc private func handleSwipe(recognizer: UISwipeGestureRecognizer) { | |
if (recognizer.direction == .left) { | |
if myWebView.canGoForward { | |
myWebView.goForward() | |
if let url = myWebView.url?.absoluteURL { | |
checkURL(url: url) | |
} | |
} | |
} | |
if (recognizer.direction == .right) { | |
if myWebView.canGoBack { | |
myWebView.goBack() | |
if let url = myWebView.url?.absoluteURL { | |
checkURL(url: url) | |
} | |
} | |
} | |
} | |
} | |
extension ViewController { | |
func checkURL(url: URL){ | |
let index = urlArr.firstIndex(of: url) | |
//index 가 nil 이면 index = 0 | |
segment.selectedSegmentIndex = index ?? 0 | |
} | |
} | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment