|
| 1 | +// |
| 2 | +// FourthViewController.swift |
| 3 | +// WRNavigationBar_swift |
| 4 | +// |
| 5 | +// Created by wangrui on 2017/4/21. |
| 6 | +// Copyright © 2017年 wangrui. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +private let NAVBAR_COLORCHANGE_POINT:CGFloat = -480 |
| 12 | +private let IMAGE_HEIGHT:CGFloat = 480 |
| 13 | +private let SCROLL_DOWN_LIMIT:CGFloat = 0 |
| 14 | +private let LIMIT_OFFSET_Y:CGFloat = -(IMAGE_HEIGHT + SCROLL_DOWN_LIMIT) |
| 15 | + |
| 16 | +class AntForestController: UIViewController |
| 17 | +{ |
| 18 | + lazy var tableView:UITableView = { |
| 19 | + let table:UITableView = UITableView(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: self.view.bounds.height), style: .plain) |
| 20 | + table.contentInset = UIEdgeInsetsMake(IMAGE_HEIGHT-CGFloat(kNavBarBottom), 0, 0, 0); |
| 21 | + table.delegate = self |
| 22 | + table.dataSource = self |
| 23 | + return table |
| 24 | + }() |
| 25 | + lazy var imageView:UIImageView = { |
| 26 | + let imgView = UIImageView(frame: CGRect(x: 0, y: -IMAGE_HEIGHT, width: kScreenWidth, height: IMAGE_HEIGHT)) |
| 27 | + imgView.contentMode = UIViewContentMode.scaleAspectFill |
| 28 | + imgView.clipsToBounds = true |
| 29 | + imgView.image = UIImage(named: "mysl") |
| 30 | + return imgView |
| 31 | + }() |
| 32 | + |
| 33 | + override func viewDidLoad() |
| 34 | + { |
| 35 | + super.viewDidLoad() |
| 36 | + title = "蚂蚁森林" |
| 37 | + view.backgroundColor = UIColor.red |
| 38 | + tableView.addSubview(imageView) |
| 39 | + view.addSubview(tableView) |
| 40 | + navigationItem.rightBarButtonItem = UIBarButtonItem(title: "··· ", style: .done, target: nil, action: nil) |
| 41 | + |
| 42 | + |
| 43 | + navBarBarTintColor = .white |
| 44 | + navBarBackgroundAlpha = 0 |
| 45 | + } |
| 46 | + |
| 47 | + deinit { |
| 48 | + tableView.delegate = nil |
| 49 | + print("FourthVC deinit") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// MARK: - viewWillAppear .. ScrollViewDidScroll |
| 54 | +extension AntForestController |
| 55 | +{ |
| 56 | + func scrollViewDidScroll(_ scrollView: UIScrollView) |
| 57 | + { |
| 58 | + let offsetY = scrollView.contentOffset.y |
| 59 | + if (offsetY > NAVBAR_COLORCHANGE_POINT) |
| 60 | + { |
| 61 | + let alpha = (offsetY - NAVBAR_COLORCHANGE_POINT) / CGFloat(kNavBarBottom) |
| 62 | + navBarBackgroundAlpha = alpha |
| 63 | + if (alpha > 0.5) { |
| 64 | + navBarTintColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1.0) |
| 65 | + navBarTitleColor = .black |
| 66 | + statusBarStyle = .default |
| 67 | + } else { |
| 68 | + navBarTintColor = .white |
| 69 | + navBarTitleColor = .white |
| 70 | + statusBarStyle = .lightContent |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + navBarBackgroundAlpha = 0 |
| 76 | + navBarTintColor = .white |
| 77 | + navBarTitleColor = .white |
| 78 | + statusBarStyle = .lightContent |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + // 限制下拉距离 |
| 85 | + if (offsetY < LIMIT_OFFSET_Y) { |
| 86 | + scrollView.contentOffset = CGPoint.init(x: 0, y: LIMIT_OFFSET_Y) |
| 87 | + } |
| 88 | + |
| 89 | + // 改变图片框的大小 (上滑的时候不改变) |
| 90 | + // 这里不能使用offsetY,因为当(offsetY < LIMIT_OFFSET_Y)的时候,y = LIMIT_OFFSET_Y 不等于 offsetY |
| 91 | + let newOffsetY = scrollView.contentOffset.y |
| 92 | + if (newOffsetY < -IMAGE_HEIGHT) |
| 93 | + { |
| 94 | + imageView.frame = CGRect(x: 0, y: newOffsetY, width: kScreenWidth, height: -newOffsetY) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +extension AntForestController: UITableViewDelegate,UITableViewDataSource |
| 101 | +{ |
| 102 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 103 | + return 15 |
| 104 | + } |
| 105 | + |
| 106 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell |
| 107 | + { |
| 108 | + let cell = UITableViewCell.init(style: .default, reuseIdentifier: nil) |
| 109 | + let str = String(format: "WRNavigationBar %zd", indexPath.row) |
| 110 | + cell.textLabel?.text = str |
| 111 | + cell.textLabel?.font = UIFont.systemFont(ofSize: 15) |
| 112 | + return cell |
| 113 | + } |
| 114 | + |
| 115 | + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) |
| 116 | + { |
| 117 | + tableView.deselectRow(at: indexPath, animated: true) |
| 118 | + let vc:UIViewController = UIViewController() |
| 119 | + vc.view.backgroundColor = UIColor.white |
| 120 | + let str = String(format: "WRNavigationBar %zd", indexPath.row) |
| 121 | + vc.title = str |
| 122 | + navigationController?.pushViewController(vc, animated: true) |
| 123 | + } |
| 124 | +} |
0 commit comments