// // MainNavigationController.swift // GoTogether // // Created by virgil on 16/3/31. // Copyright © 2016年 virgil. All rights reserved. // import UIKit class MainNavigationController: UINavigationController, UIScrollViewDelegate { var scr: UIScrollView! var arr: NSArray! var imgArray: NSMutableArray! var pageControl: UIPageControl! var intoButton: UIButton! var isOut: Bool = false override func viewDidLoad() { super.viewDidLoad() //self.makeLaunchView() if UserDefault.object(forKey: launchView) == nil { self.makeLaunchView() } // Do any additional setup after loading the view. } // MARK: ========公用透明背景View======== var backgroudView: UIView! func showBackgroudView(viewContent: UIView?, isCenter: Bool, clickOtherIsClose: Bool) { if backgroudView == nil { backgroudView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)) self.view.addSubview(backgroudView) } backgroudView.isHidden = false backgroudView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0.5) backgroudView.alpha = 0 if clickOtherIsClose { let btn = UIButton(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)) btn.addTarget(self, action: #selector(hiddenBackgroudView), for: .touchUpInside) backgroudView.addSubview(btn) } UIView.animate(withDuration: 0.3, animations: { self.backgroudView.alpha = 1 }) { (_) in } if viewContent != nil { if isCenter { viewContent!.marginTop(top: (ScreenHeight - viewContent!.frame.height)/2) viewContent!.marginLeft(left: (ScreenWidth - viewContent!.frame.width)/2) } backgroudView.addSubview(viewContent!) } } func showBackgroudView(viewContent: UIView?, isCenter: Bool) { showBackgroudView(viewContent: viewContent, isCenter: isCenter, clickOtherIsClose: true) } func showBackgroudViewFromBottom(viewContent: UIView?) { showBackgroudViewFromBottom(viewContent: viewContent, clickOtherIsClose: true) } func showBackgroudViewFromBottom(viewContent: UIView?, clickOtherIsClose: Bool) { if backgroudView == nil { backgroudView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)) self.view.addSubview(backgroudView) } backgroudView.isHidden = false backgroudView.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x000000, alpha: 0.7) backgroudView.alpha = 0 if clickOtherIsClose { let btn = UIButton(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)) btn.addTarget(self, action: #selector(hiddenBackgroudView), for: .touchUpInside) backgroudView.addSubview(btn) } UIView.animate(withDuration: 0.3) { self.backgroudView.alpha = 1 } if viewContent != nil { viewContent!.marginTop(top: ScreenHeight) viewContent!.marginLeft(left: (ScreenWidth - viewContent!.frame.width)/2) backgroudView.addSubview(viewContent!) UIView.animate(withDuration: 0.2, animations: { viewContent!.marginTop(top: ScreenHeight - viewContent!.height()) }) { (_) in } } } @objc func hiddenBackgroudView( ) { if self.backgroudView != nil { self.backgroudView.removeAll() UIView.animate(withDuration: 0.3, animations: { self.backgroudView.alpha = 0 }) { (_) in self.backgroudView.isHidden = true } } } func hiddenBackgroudViewNoAnimate() { if self.backgroudView != nil { self.backgroudView.removeAll() self.backgroudView.alpha = 0 self.backgroudView.isHidden = true } } // MARK: ========引导页======== func makeLaunchView() { let textAr1 = NSArray(objects: "不用跑遍", "不用一件件试穿", "不用遗憾的感概", "现在") let textAr2 = NSArray(objects: "各大商场搜罗中意款式", "寻找适合自己的版型", "“要是领口有我的签名绣花就好了”", "加入普兰尼奥,享受你的专属定制") // //let tap = UITapGestureRecognizer(target: self, action: "tap") //主scrollView if CommonUntils.isIphonex() { arr = NSArray(objects: "y_01_x.png", "y_02_x.png", "y_03_x.png", "y_04_x.png") //,"640_4.png" } else { arr = NSArray(objects: "y_01.png", "y_02.png", "y_03.png", "y_04.png") //,"960_4.png" } scr = UIScrollView(frame: self.view.frame) scr.contentSize = CGSize(width: self.view.frame.width * CGFloat(arr.count), height: self.view.frame.height) scr.isPagingEnabled = true scr.delegate = self scr.alpha = 1 scr.bounces = false scr.showsHorizontalScrollIndicator = false scr.showsVerticalScrollIndicator = false scr.layer.zPosition = 100000 //scr.addGestureRecognizer(tap) self.view.addSubview(scr) //小圆点 if ScreenHeight <= 480 { pageControl = UIPageControl(frame: CGRect(x: 0, y: ScreenHeight - 40, width: 320, height: 36)) } else if ScreenHeight <= 568 { pageControl = UIPageControl(frame: CGRect(x: 0, y: ScreenHeight - 60, width: 320, height: 36)) } else if ScreenHeight == 667 { pageControl = UIPageControl(frame: CGRect(x: 0, y: ScreenHeight - 60, width: 320, height: 36)) } else if ScreenHeight >= 736 { pageControl = UIPageControl(frame: CGRect(x: 0, y: ScreenHeight - 70, width: 320, height: 36)) } else { pageControl = UIPageControl(frame: CGRect(x: 0, y: ScreenHeight - 80, width: 320, height: 36)) } pageControl.isHidden = false pageControl.numberOfPages = arr.count pageControl.currentPage = 0 pageControl.currentPageIndicatorTintColor = UIColor.white pageControl.pageIndicatorTintColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x37a6ff)//CommonUntils.getUIColorFromRGB(rgbValue: 0x9e9e9e) self.view.addSubview(pageControl) pageControl.layer.zPosition = 100001 pageControl.setCenterHorizontallyForSuperView() //立即进入的btn intoButton = UIButton(type: .custom) //intoButton.backgroundColor = UIColor.red print(ScreenHeight) intoButton.frame = CGRect(x: (ScreenWidth - 160)/2, y: ScreenHeight - 140, width: 160, height: 45) intoButton.backgroundColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x37a6ff, alpha: 1.0) intoButton.setTitle("开启星创客", for: .normal) intoButton.setCornerRadius(size: 22) intoButton.setTitleColor(UIColor.white, for: .normal) intoButton.setFontSize(fontSize: 18) intoButton.addTarget(self, action: #selector(MainNavigationController.tap), for: UIControl.Event.touchUpInside) intoButton.isHidden = true intoButton.layer.zPosition = 1000002 self.view.addSubview(intoButton) if !CommonUntils.isIphonex() { intoButton.marginBottom(bottom: 8, view: pageControl) } intoButton.setCenterHorizontallyForSuperView() //加载图片文字其他 for i in 0 ..< arr.count { let imageView = UIImageView(frame: CGRect(x: self.view.frame.width * CGFloat(i), y: 0, width: self.view.frame.width, height: self.view.frame.height)) imageView.image = UIImage(named: "\(arr[i])") scr.addSubview(imageView) let topLabel: UILabel! let bottomLabel: UILabel! if ScreenHeight <= 480 { topLabel = UILabel(frame: CGRect(x: self.view.frame.width * CGFloat(i), y: 360, width: 320, height: 50)) bottomLabel = UILabel(frame: CGRect(x: self.view.frame.width * CGFloat(i), y: 400, width: 320, height: 20)) } else { topLabel = UILabel(frame: CGRect(x: self.view.frame.width * CGFloat(i), y: 410, width: 320, height: 50)) bottomLabel = UILabel(frame: CGRect(x: self.view.frame.width * CGFloat(i), y: 460, width: 320, height: 20)) } //topTextField.backgroundColor = UIColor.redColor() topLabel.textAlignment = NSTextAlignment.center topLabel.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xffffff, alpha: 0.8) topLabel.font = UIFont.systemFont(ofSize: 18) topLabel.text = textAr1[i] as? String // scr.addSubview(topLabel) //bottomTextField.backgroundColor = UIColor.redColor() bottomLabel.textAlignment = NSTextAlignment.center bottomLabel.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0xffffff, alpha: 0.8) bottomLabel.font = UIFont.systemFont(ofSize: 18) bottomLabel.text = textAr2[i] as? String //scr.addSubview(bottomLabel) } } //scrollView代理 func scrollViewDidScroll(_ scrollView: UIScrollView) { //判断引导页是否是最后一页 if scrollView.contentOffset.x >= 3 * self.view.frame.width { isOut = true //隐藏btn 显示小圆点 UIView.animate(withDuration: 0.6, animations: { () -> Void in self.intoButton.isHidden = false }) } else { isOut = false UIView.animate(withDuration: 0.6, animations: { () -> Void in self.intoButton.isHidden = true }) } } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { //计算当前第几页 let pageWidth = scrollView.frame.size.width let page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1 self.pageControl.currentPage = NSInteger(page) } //scrollView添加单击事件 @objc func tap() { if isOut { UIView.animate(withDuration: 0.6, animations: { () -> Void in self.scr.alpha = 0 self.intoButton.isHidden = true // dispatch_async(dispatch_get_main_queue(), { () -> Void in // self.navigationController?.navigationBarHidden = false // }) self.navigationController?.isNavigationBarHidden = false }, completion: { (_) -> Void in self.scr.removeFromSuperview() self.intoButton.removeFromSuperview() self.pageControl.removeFromSuperview() UserDefault.set("lunch", forKey: launchView) }) } } ///是否允许横屏 控制所有页面,模态窗口需要重写本方法 var isShouldAutorotate = false override var shouldAutorotate: Bool { return isShouldAutorotate } override var supportedInterfaceOrientations: UIInterfaceOrientationMask { if isShouldAutorotate { return [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.landscapeLeft, UIInterfaceOrientationMask.landscapeRight] } else { return [UIInterfaceOrientationMask.portrait] } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }