Skip to content

Commit

Permalink
做了部分细节调整并尝试解决主题日报文章列表的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zpz1237 committed Oct 21, 2015
1 parent 5a3b02c commit 2e6b6d5
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 36 deletions.
4 changes: 2 additions & 2 deletions zhihuDaily 2.0/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<inset key="separatorInset" minX="15" minY="0.0" maxX="15" maxY="0.0"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="tableContentViewCell" rowHeight="93" id="dAT-gO-dGZ" customClass="TableContentViewCell" customModule="知乎日报" customModuleProvider="target">
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="tableContentViewCell" rowHeight="93" id="dAT-gO-dGZ" customClass="TableContentViewCell" customModule="知乎日报" customModuleProvider="target">
<rect key="frame" x="0.0" y="92" width="600" height="93"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="dAT-gO-dGZ" id="ehw-CV-q4a">
Expand Down Expand Up @@ -58,7 +58,7 @@
<outlet property="titleLabel" destination="Kde-KL-4gF" id="W1X-25-bHK"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="tableSeparatorViewCell" id="g34-M4-bI7" customClass="TableSeparatorViewCell" customModule="知乎日报" customModuleProvider="target">
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="tableSeparatorViewCell" id="g34-M4-bI7" customClass="TableSeparatorViewCell" customModule="知乎日报" customModuleProvider="target">
<rect key="frame" x="0.0" y="185" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="g34-M4-bI7" id="Fhn-gb-174">
Expand Down
25 changes: 25 additions & 0 deletions zhihuDaily 2.0/MainTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MainTableViewController: UITableViewController, SDCycleScrollViewDelegate,

var animator: ZFModalTransitionAnimator!
var selectedNewsId = ""
var selectedIndex: [Int] = []

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
32 changes: 31 additions & 1 deletion zhihuDaily 2.0/ThemeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class ThemeViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var animator: ZFModalTransitionAnimator!
var selectedNewsId = ""

override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -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 {
Expand All @@ -73,7 +80,6 @@ class ThemeViewController: UIViewController {
}

extension ThemeViewController: UITableViewDelegate, UITableViewDataSource, ParallaxHeaderViewDelegate {

//实现Parallax效果
func scrollViewDidScroll(scrollView: UIScrollView) {
let header = self.tableView.tableHeaderView as! ParallaxHeaderView
Expand Down Expand Up @@ -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

}
}
}
114 changes: 81 additions & 33 deletions zhihuDaily 2.0/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WebViewController: UIViewController, UIScrollViewDelegate, ParallaxHeaderV
var dragging = false
var triggered = false
var newsId = ""
var hasImage = false

//滑到对应位置时调整StatusBar
var statusBarFlag = true {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

//加载图片
Expand Down Expand Up @@ -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!

Expand All @@ -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 {
Expand All @@ -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)
}

//记录下拉状态
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e6b6d5

Please sign in to comment.