专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
添加下拉刷新提示
- 只在下拉刷新的时候显示
- 在
HMHomeViewController
定义showTipView
方法/** 显示加载多少条数据.只在下拉刷新数据的时候调用 parameter count: 条数 */ private func showTipView(count: Int) { // }
确定调用
showTipView
的地方,在loadStatus
的完成闭包里面调用/// 加载新的微博数据 func loadNewData() { print("加载新的微博数据") // 获取id最大的微博, 如果没有数据,就默认加载最新20条微博数据 let since_id = statusVMs?.first?.status.id ?? 0 // 加载微博数据 HMStatusListViewModel.shared.loadStatus(since_id: since_id, max_id: 0) { (statusVMs: [HMStatusViewModel]?, error: Error?) in // 关闭下拉刷新控件 self.refreshView.endRefreshing() // 判断错误 if error != nil { print("加载微博数据出错了: \(error)") SVProgressHUD.showError(withStatus: "加载微博数据失败,网络不给力") return } // 获取到多少条微博数据 let count = statusVMs?.count ?? 0 // 下拉刷新,显示加载了多少条微博 if since_id > 0 { self.showTipView(count: count) } // 没有加载微博数据 if count == 0 { print("没有新的微博数据") return } // 如果是下拉刷新,将获取到数据拼接在现有数据的前面 if since_id > 0 { // 下拉刷新 // 最新数据 = 新获取到的数据 + 原有的数据 print("下拉刷新,获取到: \(statusVMs?.count)"); self.statusVMs = statusVMs! + self.statusVMs! } else { // 刚进入程序,加载最新的微博数据 // 最新数据 = 新获取到的数据 self.statusVMs = statusVMs print("获取最新20条数据.获取到 \(statusVMs?.count) 条微博") } } }
懒加载
tipLabel
/// 下拉刷新提示view private lazy var tipLabel: UILabel = { let tipLabelHeight: CGFloat = 44 let tipLabel = UILabel(frame: CGRect(x: 0, y: -20 - tipLabelHeight, width: UIScreen.width, height: tipLabelHeight)) tipLabel.backgroundColor = UIColor.orange tipLabel.textColor = UIColor.white tipLabel.font = UIFont.systemFont(ofSize: 16) tipLabel.textAlignment = NSTextAlignment.center // 导航栏的frame = (0 20; 375 44) self.navigationController?.navigationBar.insertSubview(tipLabel, at: 0) return tipLabel }()
- 完善
showTipView
方法/** 显示加载多少条数据.只在下拉刷新数据的时候调用 parameter count: 条数 */ private func showTipView(count: Int) { tipLabel.text = count == 0 ? "没有微博数据" : "加载了 \(count) 条微博" // 动画 UIView.animate(withDuration: 1, animations: { self.tipLabel.frame.origin.y = 44 }) { (_) in UIView.animate(withDuration: 1, delay: 0.5, options: [], animations: { self.tipLabel.frame.origin.y = -64 }, completion: nil) } }
- 发现动画叠加导致label还会往下走
// 动画叠加导致label还会往下走,UIView动画的底层也是核心动画 private func showTipView(count: Int) { let animationKeys = tipLabel.layer.animationKeys() if animationKeys != nil { print("有动画在运行,移除: \(animationKeys)") // 让上次的动画走完 return } }