Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import ZKCarousel
- import WebKit
- class EventsAndPromotionViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
- @IBOutlet weak var menuButton: UIBarButtonItem!
- @IBOutlet weak var arrowImage: UIImageView!
- @IBOutlet weak var webViewHeightConstraint: NSLayoutConstraint!
- @IBOutlet weak var eventPromotionLabel: UILabel!
- @IBOutlet weak var webViewContainer: UIView!
- @IBOutlet weak var scrollView: UIScrollView!
- @IBOutlet var carousel: ZKCarousel! = ZKCarousel()
- var isViewMore: Bool = false
- var eventTitles = [String]()
- var eventUrls = [String]()
- var webView: WKWebView!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.title = "Events & Promotion"
- carousel.delegate = self
- // setup swreveal
- if revealViewController() != nil {
- menuButton.target = SWRevealViewController()
- menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
- self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
- self.view.addGestureRecognizer(revealViewController().tapGestureRecognizer())
- revealViewController().rightViewRevealWidth = 160
- }
- eventPromotionLabel?.text = "Castrol Service & Win 2017"
- let title = ["Castrol Service & Win 2017", "Castrol Service & Win 2018", "Castrol Service & Win 2019"]
- eventTitles.append(contentsOf: title)
- let url = ["https://www.apple.com", "https://www.castrol.com/en_my/malaysia.html", "https://www.castrol.com/en_my/malaysia/services/castrol-auto-service/castrol-parts-warranty.html"]
- eventUrls.append(contentsOf: url)
- arrowImage.image = #imageLiteral(resourceName: "down-arrow")
- setupCarousel()
- setupWebView()
- self.webViewContainer.frame.size.height = 4000
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- fileprivate func setupCarousel() {
- //create slide array
- carousel.backgroundColor = UIColor.clear
- carousel.alpha = 1
- let slide1 = ZKCarouselSlide(image: #imageLiteral(resourceName: "news&Promotion1"), title: "", description: "")
- let slide2 = ZKCarouselSlide(image: #imageLiteral(resourceName: "news&Promotion1"), title: "", description: "")
- let slide3 = ZKCarouselSlide(image: #imageLiteral(resourceName: "news&Promotion1"), title: "", description: "")
- //Add slide to carousel
- carousel.slides = [slide1, slide2, slide3]
- }
- fileprivate func setupWebView() {
- let webConfiguration = WKWebViewConfiguration()
- let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.webViewContainer.frame.size.height))
- self.webView = WKWebView(frame: customFrame , configuration: webConfiguration)
- webView.translatesAutoresizingMaskIntoConstraints = false
- self.webViewContainer.addSubview(webView)
- webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor).isActive = true
- webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor).isActive = true
- webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor).isActive = true
- webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor).isActive = true
- webView.heightAnchor.constraint(equalTo: webViewContainer.heightAnchor).isActive = true
- webView.uiDelegate = self
- webView.navigationDelegate = self
- // webView.frame.size.height = 1
- // webView.frame.size = webView.sizeThatFits(.zero)
- // webView.scrollView.isScrollEnabled=false;
- // // myWebViewHeightConstraint.constant = webView.scrollView.contentSize.height
- // webView.sizeToFit()
- // webViewHeightConstraint.constant = 0
- webViewContainer.isHidden = true
- self.view.layoutIfNeeded()
- let myURL = URL(string: "https://www.apple.com")
- let myRequest = URLRequest(url: myURL!)
- webView.scrollView.isScrollEnabled = false
- webView.load(myRequest)
- }
- @IBAction func viewMoreButtonTapped(_ sender: Any) {
- isViewMore = !isViewMore
- if isViewMore {
- UIView.animate(withDuration: 0.5, animations: {
- //self.webViewHeightConstraint.constant = 555
- self.webViewContainer.isHidden = false
- self.arrowImage.image = #imageLiteral(resourceName: "up-arrow")
- self.view.layoutIfNeeded()
- })
- } else {
- UIView.animate(withDuration: 0.5, animations: {
- // self.webViewHeightConstraint.constant = 0
- self.webViewContainer.isHidden = true
- self.arrowImage.image = #imageLiteral(resourceName: "down-arrow")
- self.view.layoutIfNeeded()
- })
- }
- }
- func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
- if webView.isLoading == false {
- webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
- if let height = result as? CGFloat {
- print("website height: \(height)")
- print("view height: \(self?.view.frame.height)")
- print("scroll view height: \(self?.scrollView.frame.height)")
- print("web container height: \(self?.webViewContainer.frame.size.height)")
- print("web view height: \(webView.frame.size.height)")
- print("container constraint height: \(self?.webViewHeightConstraint.constant)")
- // webView.frame.size.height += height
- // self?.view.frame = CGRect(x: self!.view.frame.origin.x, y: self!.view.frame.origin.y, width: self!.view.frame.size.width, height: height)
- // self?.scrollView.frame = CGRect(x: self!.scrollView.frame.origin.x, y: self!.scrollView.frame.origin.y, width: self!.scrollView.frame.size.width, height: height)
- // self?.webViewHeightConstraint.constant = height
- }
- })
- }
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
- extension EventsAndPromotionViewController : lembuDelegate {
- func getIndexWhenScrolling(currentIndex: Int) {
- if currentIndex == 0 {
- eventPromotionLabel.text = eventTitles[0]
- let myURL = URL(string: eventUrls[0])
- let myRequest = URLRequest(url: myURL!)
- webView.load(myRequest)
- } else if currentIndex == 1 {
- eventPromotionLabel.text = eventTitles[1]
- let myURL = URL(string: eventUrls[1])
- let myRequest = URLRequest(url: myURL!)
- webView.load(myRequest)
- } else if currentIndex == 2 {
- eventPromotionLabel.text = eventTitles[2]
- let myURL = URL(string: eventUrls[2])
- let myRequest = URLRequest(url: myURL!)
- webView.load(myRequest)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement