Advertisement
Guest User

MyViewController

a guest
Jan 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.77 KB | None | 0 0
  1. //
  2. //  EventsAndPromotionViewController.swift
  3. //  Castrol
  4. //
  5. //  Created by Afiq Hamdan on 11/29/17.
  6. //  Copyright © 2017 Cliqers. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import ZKCarousel
  11. import WebKit
  12.  
  13. class EventsAndPromotionViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
  14.    
  15.     @IBOutlet weak var menuButton: UIBarButtonItem!
  16.     @IBOutlet weak var arrowImage: UIImageView!
  17.     @IBOutlet weak var webViewHeightConstraint: NSLayoutConstraint!
  18.     @IBOutlet weak var eventPromotionLabel: UILabel!
  19.     @IBOutlet weak var webViewContainer: UIView!
  20.     @IBOutlet weak var scrollView: UIScrollView!
  21.     @IBOutlet var carousel: ZKCarousel! = ZKCarousel()
  22.    
  23.     var isViewMore: Bool = false
  24.     var eventTitles = [String]()
  25.     var eventUrls = [String]()
  26.     var webView: WKWebView!
  27.  
  28.     override func viewDidLoad() {
  29.         super.viewDidLoad()
  30.        
  31.         self.title = "Events & Promotion"
  32.         carousel.delegate = self
  33.        
  34.         // setup swreveal
  35.         if revealViewController() != nil {
  36.             menuButton.target = SWRevealViewController()
  37.             menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
  38.             self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
  39.             self.view.addGestureRecognizer(revealViewController().tapGestureRecognizer())
  40.             revealViewController().rightViewRevealWidth = 160
  41.         }
  42.        
  43.         eventPromotionLabel?.text = "Castrol Service & Win 2017"
  44.         let title = ["Castrol Service & Win 2017", "Castrol Service & Win 2018", "Castrol Service & Win 2019"]
  45.         eventTitles.append(contentsOf: title)
  46.         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"]
  47.         eventUrls.append(contentsOf: url)
  48.        
  49.         arrowImage.image = #imageLiteral(resourceName: "down-arrow")
  50.         setupCarousel()
  51.         setupWebView()
  52.         self.webViewContainer.frame.size.height = 4000
  53.  
  54.     }
  55.  
  56.    
  57.     override func didReceiveMemoryWarning() {
  58.         super.didReceiveMemoryWarning()
  59.         // Dispose of any resources that can be recreated.
  60.     }
  61.    
  62.     fileprivate func setupCarousel() {
  63.        
  64.         //create slide array
  65.         carousel.backgroundColor = UIColor.clear
  66.         carousel.alpha = 1
  67.         let slide1 = ZKCarouselSlide(image: #imageLiteral(resourceName: "news&Promotion1"), title: "", description: "")
  68.         let slide2 = ZKCarouselSlide(image: #imageLiteral(resourceName: "news&Promotion1"), title: "", description: "")
  69.         let slide3 = ZKCarouselSlide(image: #imageLiteral(resourceName: "news&Promotion1"), title: "", description: "")
  70.        
  71.         //Add slide to carousel
  72.         carousel.slides = [slide1, slide2, slide3]
  73.        
  74.     }
  75.    
  76.     fileprivate func setupWebView() {
  77.         let webConfiguration = WKWebViewConfiguration()
  78.         let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.webViewContainer.frame.size.height))
  79.         self.webView = WKWebView(frame: customFrame , configuration: webConfiguration)
  80.         webView.translatesAutoresizingMaskIntoConstraints = false
  81.         self.webViewContainer.addSubview(webView)
  82.         webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor).isActive = true
  83.         webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor).isActive = true
  84.         webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor).isActive = true
  85.         webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor).isActive = true
  86.         webView.heightAnchor.constraint(equalTo: webViewContainer.heightAnchor).isActive = true
  87.         webView.uiDelegate = self
  88.         webView.navigationDelegate = self
  89.        
  90. //        webView.frame.size.height = 1
  91. //        webView.frame.size = webView.sizeThatFits(.zero)
  92. //        webView.scrollView.isScrollEnabled=false;
  93. //       // myWebViewHeightConstraint.constant = webView.scrollView.contentSize.height
  94. //        webView.sizeToFit()
  95.        
  96.        // webViewHeightConstraint.constant = 0
  97.         webViewContainer.isHidden = true
  98.         self.view.layoutIfNeeded()
  99.  
  100.         let myURL = URL(string: "https://www.apple.com")
  101.         let myRequest = URLRequest(url: myURL!)
  102.         webView.scrollView.isScrollEnabled = false
  103.         webView.load(myRequest)
  104.        
  105.     }
  106.    
  107.    
  108.     @IBAction func viewMoreButtonTapped(_ sender: Any) {
  109.        
  110.         isViewMore = !isViewMore
  111.  
  112.         if isViewMore {
  113.             UIView.animate(withDuration: 0.5, animations: {
  114.                 //self.webViewHeightConstraint.constant = 555
  115.                 self.webViewContainer.isHidden = false
  116.                 self.arrowImage.image = #imageLiteral(resourceName: "up-arrow")
  117.                 self.view.layoutIfNeeded()
  118.             })
  119.         } else {
  120.             UIView.animate(withDuration: 0.5, animations: {
  121. //                self.webViewHeightConstraint.constant = 0
  122.                 self.webViewContainer.isHidden = true
  123.                 self.arrowImage.image = #imageLiteral(resourceName: "down-arrow")
  124.                 self.view.layoutIfNeeded()
  125.             })
  126.         }
  127.     }
  128.    
  129.     func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  130.        
  131.         if webView.isLoading == false {
  132.             webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
  133.                 if let height = result as? CGFloat {
  134.                     print("website height: \(height)")
  135.                     print("view height: \(self?.view.frame.height)")
  136.                     print("scroll view height: \(self?.scrollView.frame.height)")
  137.                     print("web container height: \(self?.webViewContainer.frame.size.height)")
  138.                     print("web view height: \(webView.frame.size.height)")
  139.                     print("container constraint height: \(self?.webViewHeightConstraint.constant)")
  140.  
  141.                    // webView.frame.size.height += height
  142. //                    self?.view.frame = CGRect(x: self!.view.frame.origin.x, y: self!.view.frame.origin.y, width: self!.view.frame.size.width, height: height)
  143. //                    self?.scrollView.frame = CGRect(x: self!.scrollView.frame.origin.x, y: self!.scrollView.frame.origin.y, width: self!.scrollView.frame.size.width, height: height)
  144. //                    self?.webViewHeightConstraint.constant = height
  145.                 }
  146.             })
  147.         }
  148.        
  149.     }
  150.    
  151.    
  152.     /*
  153.      // MARK: - Navigation
  154.      
  155.      // In a storyboard-based application, you will often want to do a little preparation before navigation
  156.      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  157.      // Get the new view controller using segue.destinationViewController.
  158.      // Pass the selected object to the new view controller.
  159.      }
  160.      */
  161.    
  162. }
  163.  
  164. extension EventsAndPromotionViewController : lembuDelegate {
  165.  
  166.     func getIndexWhenScrolling(currentIndex: Int) {
  167.         if currentIndex == 0 {
  168.             eventPromotionLabel.text =  eventTitles[0]
  169.             let myURL = URL(string: eventUrls[0])
  170.             let myRequest = URLRequest(url: myURL!)
  171.             webView.load(myRequest)
  172.         } else if currentIndex == 1 {
  173.             eventPromotionLabel.text =  eventTitles[1]
  174.             let myURL = URL(string: eventUrls[1])
  175.             let myRequest = URLRequest(url: myURL!)
  176.             webView.load(myRequest)
  177.         } else if currentIndex == 2 {
  178.             eventPromotionLabel.text =  eventTitles[2]
  179.             let myURL = URL(string: eventUrls[2])
  180.             let myRequest = URLRequest(url: myURL!)
  181.             webView.load(myRequest)
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement