From 2e6b6d5ea2d4339580f67ca4989a1f0dcfce7dd1 Mon Sep 17 00:00:00 2001 From: NSNirvana Date: Wed, 21 Oct 2015 16:59:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=9A=E4=BA=86=E9=83=A8=E5=88=86=E7=BB=86?= =?UTF-8?q?=E8=8A=82=E8=B0=83=E6=95=B4=E5=B9=B6=E5=B0=9D=E8=AF=95=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E4=B8=BB=E9=A2=98=E6=97=A5=E6=8A=A5=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zhihuDaily 2.0/Base.lproj/Main.storyboard | 4 +- zhihuDaily 2.0/MainTableViewController.swift | 25 ++++ zhihuDaily 2.0/ThemeViewController.swift | 32 +++++- zhihuDaily 2.0/WebViewController.swift | 114 +++++++++++++------ 4 files changed, 139 insertions(+), 36 deletions(-) diff --git a/zhihuDaily 2.0/Base.lproj/Main.storyboard b/zhihuDaily 2.0/Base.lproj/Main.storyboard index b658acc..ddc96eb 100644 --- a/zhihuDaily 2.0/Base.lproj/Main.storyboard +++ b/zhihuDaily 2.0/Base.lproj/Main.storyboard @@ -17,7 +17,7 @@ - + @@ -58,7 +58,7 @@ - + diff --git a/zhihuDaily 2.0/MainTableViewController.swift b/zhihuDaily 2.0/MainTableViewController.swift index 43689de..294fe61 100644 --- a/zhihuDaily 2.0/MainTableViewController.swift +++ b/zhihuDaily 2.0/MainTableViewController.swift @@ -14,6 +14,7 @@ class MainTableViewController: UITableViewController, SDCycleScrollViewDelegate, var animator: ZFModalTransitionAnimator! var selectedNewsId = "" + var selectedIndex: [Int] = [] override func viewDidLoad() { super.viewDidLoad() @@ -110,6 +111,13 @@ class MainTableViewController: UITableViewController, SDCycleScrollViewDelegate, let cell = tableView.dequeueReusableCellWithIdentifier("tableContentViewCell") as! TableContentViewCell let data = appCloud().contentStory[indexPath.row] + //验证是否已被点击过 + if let _ = selectedIndex.indexOf(indexPath.row) { + cell.titleLabel.textColor = UIColor.lightGrayColor() + } else { + cell.titleLabel.textColor = UIColor.blackColor() + } + cell.imagesView.image = UIImage(named: data.images[0]) cell.titleLabel.text = data.title @@ -131,6 +139,13 @@ class MainTableViewController: UITableViewController, SDCycleScrollViewDelegate, let cell = tableView.dequeueReusableCellWithIdentifier("tableContentViewCell") as! TableContentViewCell let data = appCloud().pastContentStory[newIndex] as! ContentStoryModel + //验证是否已被点击过 + if let _ = selectedIndex.indexOf(indexPath.row) { + cell.titleLabel.textColor = UIColor.lightGrayColor() + } else { + cell.titleLabel.textColor = UIColor.blackColor() + } + cell.imagesView.image = UIImage(named: data.images[0]) cell.titleLabel.text = data.title @@ -144,6 +159,16 @@ class MainTableViewController: UITableViewController, SDCycleScrollViewDelegate, //tableView点击事件 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + + //保证点击的是TableContentViewCell + guard tableView.cellForRowAtIndexPath(indexPath) is TableContentViewCell else { + return + } + + //记录已被选中的indexPath并改变其textColor + selectedIndex.append(indexPath.row) + (tableView.cellForRowAtIndexPath(indexPath) as! TableContentViewCell).titleLabel.textColor = UIColor.lightGrayColor() + //拿到webViewController let webViewController = self.storyboard?.instantiateViewControllerWithIdentifier("webViewController") as!WebViewController webViewController.modalPresentationStyle = UIModalPresentationStyle.FullScreen diff --git a/zhihuDaily 2.0/ThemeViewController.swift b/zhihuDaily 2.0/ThemeViewController.swift index 915b270..8c2a0a1 100644 --- a/zhihuDaily 2.0/ThemeViewController.swift +++ b/zhihuDaily 2.0/ThemeViewController.swift @@ -12,6 +12,9 @@ class ThemeViewController: UIViewController { @IBOutlet weak var tableView: UITableView! + var animator: ZFModalTransitionAnimator! + var selectedNewsId = "" + override func viewDidLoad() { super.viewDidLoad() @@ -49,6 +52,10 @@ class ThemeViewController: UIViewController { self.tableView.showsVerticalScrollIndicator = false } + override func viewWillAppear(animated: Bool) { +// let rect = CGRectMake(0, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) +// self.tableView.frame = rect + } //设置StatusBar颜色 override func preferredStatusBarStyle() -> UIStatusBarStyle { @@ -73,7 +80,6 @@ class ThemeViewController: UIViewController { } extension ThemeViewController: UITableViewDelegate, UITableViewDataSource, ParallaxHeaderViewDelegate { - //实现Parallax效果 func scrollViewDidScroll(scrollView: UIScrollView) { let header = self.tableView.tableHeaderView as! ParallaxHeaderView @@ -128,4 +134,28 @@ extension ThemeViewController: UITableViewDelegate, UITableViewDataSource, Paral } return 92 } + + //处理UITableViewDelegate + func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + //拿到webViewController + let webViewController = self.storyboard?.instantiateViewControllerWithIdentifier("webViewController") as!WebViewController + + //对animator进行初始化 + animator = ZFModalTransitionAnimator(modalViewController: webViewController) + self.animator.dragable = true + self.animator.bounces = false + self.animator.behindViewAlpha = 0.7 + self.animator.behindViewScale = 0.7 + self.animator.transitionDuration = 0.7 + self.animator.direction = ZFModalTransitonDirection.Right + + //设置webViewController + webViewController.transitioningDelegate = self.animator + webViewController.newsId = "Jst Try" + + //实施转场 + self.presentViewController(webViewController, animated: true) { () -> Void in + + } + } } diff --git a/zhihuDaily 2.0/WebViewController.swift b/zhihuDaily 2.0/WebViewController.swift index 4c0acfc..4aded07 100644 --- a/zhihuDaily 2.0/WebViewController.swift +++ b/zhihuDaily 2.0/WebViewController.swift @@ -25,6 +25,7 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV var dragging = false var triggered = false var newsId = "" + var hasImage = false //滑到对应位置时调整StatusBar var statusBarFlag = true { @@ -55,6 +56,10 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV //避免webScrollView的ContentView过长 挡住底层View self.view.clipsToBounds = true + if hasImage == false { + statusBarBackground.backgroundColor = UIColor.whiteColor() + } + //对scrollView做基本配置 self.webView.scrollView.delegate = self self.webView.scrollView.clipsToBounds = false @@ -63,8 +68,30 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV } override func viewWillAppear(animated: Bool) { - //loadParallaxHeader("") loadWebView("") + if hasImage { + loadParallaxHeader("") + } else { + loadNormalHeader() + } + } + + //加载普通header + func loadNormalHeader() { + //"载入上一篇"imageView + refreshImageView = UIImageView(frame: CGRectMake(self.view.frame.width / 2 - 47, -30, 15, 15)) + refreshImageView.contentMode = UIViewContentMode.ScaleAspectFill + refreshImageView.image = UIImage(named: "arrow")?.imageWithRenderingMode(.AlwaysTemplate) + refreshImageView.tintColor = UIColor(red: 215/255.0, green: 215/255.0, blue: 215/255.0, alpha: 1) + self.webView.scrollView.addSubview(refreshImageView) + + //载入上一篇Label + let refreshLabel = UILabel(frame: CGRectMake(12, -45, self.view.frame.width, 45)) + refreshLabel.text = "载入上一篇" + refreshLabel.textAlignment = NSTextAlignment.Center + refreshLabel.textColor = UIColor(red: 215/255.0, green: 215/255.0, blue: 215/255.0, alpha: 1) + refreshLabel.font = UIFont(name: "HelveticaNeue", size: 14) + self.webView.scrollView.addSubview(refreshLabel) } //加载图片 @@ -129,7 +156,7 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV //加载WebView func loadWebView(newsId: String) { //获取网络数据,包括body css image image_source title 并拼接body与css后加载 - Alamofire.request(.GET, "http://news-at.zhihu.com/api/4/news/7235309").responseJSON { (_, _, dataResult) -> Void in + Alamofire.request(.GET, "http://news-at.zhihu.com/api/4/news/7287535").responseJSON { (_, _, dataResult) -> Void in let body = JSON(dataResult.value!)["body"].string! let css = JSON(dataResult.value!)["css"][0].string! @@ -149,20 +176,56 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV //实现Parallax效果 func scrollViewDidScroll(scrollView: UIScrollView) { - let incrementY = scrollView.contentOffset.y - if incrementY < 0 { + //判断是否含图 + if hasImage { + let incrementY = scrollView.contentOffset.y + if incrementY < 0 { + + //不断设置titleLabel及sourceLabel以保证frame正确 + titleLabel.frame = CGRectMake(15, orginalHeight - 80 - incrementY, self.view.frame.width - 30, 60) + sourceLabel.frame = CGRectMake(15, orginalHeight - 20 - incrementY, self.view.frame.width - 30, 15) + + //不断添加删除blurView.layer.sublayers![0]以保证frame正确 + blurView.frame = CGRectMake(0, -85 - incrementY, self.view.frame.width, orginalHeight + 85) + blurView.layer.sublayers![0].removeFromSuperlayer() + blurView.insertTwiceTransparentGradient() + + //如果下拉超过65pixels则改变图片方向 + if incrementY <= -65 { + arrowState = true + //如果此时是第一次检测到松手则加载上一篇 + guard dragging || triggered else { + loadNewArticle(true) + triggered = true + return + } + } else { + arrowState = false + } + + //使Label不被遮挡 + imageView.bringSubviewToFront(titleLabel) + imageView.bringSubviewToFront(sourceLabel) + } - //不断设置titleLabel及sourceLabel以保证frame正确 - titleLabel.frame = CGRectMake(15, orginalHeight - 80 - incrementY, self.view.frame.width - 30, 60) - sourceLabel.frame = CGRectMake(15, orginalHeight - 20 - incrementY, self.view.frame.width - 30, 15) + //监听contentOffsetY以改变StatusBarUI + if incrementY > 223 { + if statusBarFlag { + statusBarFlag = false + } + statusBarBackground.backgroundColor = UIColor.whiteColor() + } else { + guard statusBarFlag else { + statusBarFlag = true + return + } + statusBarBackground.backgroundColor = UIColor.clearColor() + } - //不断添加删除blurView.layer.sublayers![0]以保证frame正确 - blurView.frame = CGRectMake(0, -85 - incrementY, self.view.frame.width, orginalHeight + 85) - blurView.layer.sublayers![0].removeFromSuperlayer() - blurView.insertTwiceTransparentGradient() - + webHeaderView.layoutWebHeaderViewForScrollViewOffset(scrollView.contentOffset) + } else { //如果下拉超过65pixels则改变图片方向 - if incrementY <= -65 { + if self.webView.scrollView.contentOffset.y <= -50 { arrowState = true //如果此时是第一次检测到松手则加载上一篇 guard dragging || triggered else { @@ -173,27 +236,7 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV } else { arrowState = false } - - //使Label不被遮挡 - imageView.bringSubviewToFront(titleLabel) - imageView.bringSubviewToFront(sourceLabel) } - - //监听contentOffsetY以改变StatusBarUI - if incrementY > 223 { - if statusBarFlag { - statusBarFlag = false - } - statusBarBackground.backgroundColor = UIColor.whiteColor() - } else { - guard statusBarFlag else { - statusBarFlag = true - return - } - statusBarBackground.backgroundColor = UIColor.clearColor() - } - - webHeaderView.layoutWebHeaderViewForScrollViewOffset(scrollView.contentOffset) } //记录下拉状态 @@ -245,6 +288,11 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV //依据statusBarFlag返回StatusBar颜色 override func preferredStatusBarStyle() -> UIStatusBarStyle { + //无图的情况 + guard hasImage else { + return .Default + } + if statusBarFlag { //bug:当切换页面后该函数调用的self是最初的self,其他更改的都是新self,所以这里会有问题 return .LightContent