|
| 1 | +// |
| 2 | +// NeteaseNewsFooter.swift |
| 3 | +// Demo |
| 4 | +// |
| 5 | +// Created by Leo on 2017/11/13. |
| 6 | +// Copyright © 2017年 Leo. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import PullToRefreshKit |
| 11 | + |
| 12 | +class NeteaseNewsFooter: UIView, RefreshableFooter { |
| 13 | + var containerView = UIView() |
| 14 | + var textlabel = UILabel() |
| 15 | + var shapeLayer = CAShapeLayer() |
| 16 | + override init(frame: CGRect) { |
| 17 | + super.init(frame: frame) |
| 18 | + addSubview(containerView) |
| 19 | + textlabel.font = UIFont.systemFont(ofSize: 15) |
| 20 | + textlabel.textColor = UIColor.darkGray |
| 21 | + containerView.layer.cornerRadius = 4.0; |
| 22 | + containerView.layer.masksToBounds = true |
| 23 | + containerView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0) |
| 24 | + containerView.layer.shadowRadius = 10.0; |
| 25 | + containerView.layer.shadowColor = UIColor.black.cgColor; |
| 26 | + containerView.layer.shadowOpacity = 0.20; |
| 27 | + containerView.layer.addSublayer(shapeLayer) |
| 28 | + containerView.layer.borderColor = UIColor.black.withAlphaComponent(0.2).cgColor |
| 29 | + containerView.layer.borderWidth = 0.5 |
| 30 | + containerView.addSubview(textlabel) |
| 31 | + setUpCircleLayer() |
| 32 | + textlabel.text = "上拉加载更多" |
| 33 | + shapeLayer.isHidden = true |
| 34 | + } |
| 35 | + |
| 36 | + override func layoutSubviews() { |
| 37 | + super.layoutSubviews() |
| 38 | + containerView.frame = CGRect(x: 8.0, y: 10.0, width: self.frame.width - 16.0, height: self.frame.size.height - 20.0) |
| 39 | + textlabel.sizeToFit() |
| 40 | + textlabel.center = CGPoint(x: containerView.frame.size.width / 2.0, y: containerView.frame.size.height/2.0) |
| 41 | + shapeLayer.position = CGPoint(x: textlabel.frame.origin.x - 30.0, y: containerView.frame.size.height/2.0 + 10.0) |
| 42 | + } |
| 43 | + public required init?(coder aDecoder: NSCoder) { |
| 44 | + fatalError("init(coder:) has not been implemented") |
| 45 | + } |
| 46 | + func setUpCircleLayer(){ |
| 47 | + let bezierPath = UIBezierPath(arcCenter: CGPoint(x: 20, y: 20), |
| 48 | + radius: 12.0, |
| 49 | + startAngle:-CGFloat.pi/2, |
| 50 | + endAngle: CGFloat.pi/2.0 * 3.0, |
| 51 | + clockwise: true) |
| 52 | + shapeLayer.path = bezierPath.cgPath |
| 53 | + shapeLayer.strokeColor = UIColor.lightGray.cgColor |
| 54 | + shapeLayer.fillColor = UIColor.clear.cgColor |
| 55 | + shapeLayer.strokeStart = 0.3 |
| 56 | + shapeLayer.strokeEnd = 0.8 |
| 57 | + shapeLayer.lineWidth = 1.0 |
| 58 | + shapeLayer.lineCap = kCALineCapRound |
| 59 | + shapeLayer.bounds = CGRect(x: 0, y: 0,width: 40, height: 40) |
| 60 | + shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5) |
| 61 | + layer.addSublayer(shapeLayer) |
| 62 | + } |
| 63 | + |
| 64 | + // MARK: - RefreshableHeader - |
| 65 | + |
| 66 | + func heightForFooter() -> CGFloat { |
| 67 | + return 65 |
| 68 | + } |
| 69 | + |
| 70 | + func didUpdateToNoMoreData() { |
| 71 | + textlabel.text = "到底了..." |
| 72 | + shapeLayer.isHidden = true |
| 73 | + shapeLayer.removeAnimation(forKey: "rotate") |
| 74 | + setNeedsLayout() |
| 75 | + } |
| 76 | + |
| 77 | + func didResetToDefault() { |
| 78 | + textlabel.text = "上拉加载更多" |
| 79 | + shapeLayer.isHidden = true |
| 80 | + shapeLayer.removeAnimation(forKey: "rotate") |
| 81 | + setNeedsLayout() |
| 82 | + } |
| 83 | + |
| 84 | + func didEndRefreshing() { |
| 85 | + textlabel.text = "上拉加载更多" |
| 86 | + shapeLayer.isHidden = true |
| 87 | + shapeLayer.removeAnimation(forKey: "rotate") |
| 88 | + setNeedsLayout() |
| 89 | + } |
| 90 | + |
| 91 | + func didBeginRefreshing() { |
| 92 | + textlabel.text = "正在加载中..." |
| 93 | + let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation.z") |
| 94 | + rotateAnimation.toValue = NSNumber(value: Double.pi * 2.0) |
| 95 | + rotateAnimation.duration = 0.6 |
| 96 | + rotateAnimation.isCumulative = true |
| 97 | + rotateAnimation.repeatCount = 10000000 |
| 98 | + shapeLayer.add(rotateAnimation, forKey: "rotate") |
| 99 | + setNeedsLayout() |
| 100 | + shapeLayer.isHidden = false |
| 101 | + } |
| 102 | + |
| 103 | + func shouldBeginRefreshingWhenScroll() -> Bool { |
| 104 | + return true |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments