1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
| import UIKit
protocol PageTitleDelegate : class { func pageTitleDidSelected(pageTitle: PageTitle, pageTitleView: PageTitleView) }
class PageTitleView: UIView { var index: Int = 0 var title: String? var color: UIColor? { didSet { label.textColor = color } } fileprivate var style: MenuStyle fileprivate var isSelected: Bool = false { didSet { reloadState() } } fileprivate var width: CGFloat { return title?.size(withAttributes: [NSAttributedString.Key.font: font]).width ?? 0 } fileprivate var font: UIFont { return isSelected ? style.selectedFont : style.defaultFont } fileprivate var fontColor: UIColor { return isSelected ? style.selectedColor : style.defaultColor } fileprivate var tapCallBack: ((_ view: PageTitleView) -> Void)? private lazy var label: UILabel = { let label = UILabel() let gesture = UITapGestureRecognizer(target: self, action: #selector(action(_:))) label.addGestureRecognizer(gesture) label.isUserInteractionEnabled = true return label }() init(title: String?, isSelected: Bool, style: MenuStyle) { self.title = title self.isSelected = isSelected self.style = style super.init(frame: CGRect.zero) reloadState() self.addSubview(label) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } @objc fileprivate func action(_ tap: UITapGestureRecognizer) { if tapCallBack != nil { tapCallBack!(self) } } private func reloadState() { label.text = title label.textColor = fontColor label.font = font label.sizeToFit() } }
class PageTitle: UIView { weak var delegate: PageTitleDelegate? private var titleList: [String] private lazy var scrollView: UIScrollView = { var scrollView = UIScrollView(frame: bounds) scrollView.showsHorizontalScrollIndicator = false addSubview(scrollView) return scrollView }() private lazy var lineView: UIView = { var lineView = UIView() lineView.backgroundColor = menuStyle.lineColor ?? menuStyle.selectedColor lineView.layer.cornerRadius = menuStyle.lineCornerRadius lineView.layer.masksToBounds = true scrollView.addSubview(lineView) return lineView }() private var menuStyle: MenuStyle private var currentSelectedView: PageTitleView? private var titleViewList = [PageTitleView]() init(frame: CGRect, menuStyle: MenuStyle, titleList: [String]) { self.menuStyle = menuStyle self.titleList = titleList super.init(frame: frame) setupSubViews() } private func setupSubViews() { var totalWidth: CGFloat = 0 for (idx, title) in titleList.enumerated() { let view = PageTitleView(title: title as String, isSelected: false, style: menuStyle) view.index = idx titleViewList.append(view) let x = CGFloat(idx + 1) * menuStyle.margin + totalWidth view.frame = CGRect(x: x, y: 0, width: view.width, height: frame.height - menuStyle.lineHeight) totalWidth += view.width scrollView.addSubview(view) view.tapCallBack = { [weak self] view in guard let self = self else { return } self.changeToSelectedIndex(idx: view.index) } } scrollView.contentSize = CGSize(width: totalWidth + CGFloat(titleList.count + 1) * menuStyle.margin, height: 0) changeToSelectedIndex() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
extension PageTitle { private func changeToSelectedIndex(idx: Int = 0, progress: CGFloat = 1) { if idx > titleViewList.count - 1 || idx < 0 || titleViewList.count <= 0 { return } var fromView, toView: PageTitleView if currentSelectedView == nil { fromView = titleViewList.first! toView = fromView } else { fromView = currentSelectedView! toView = titleViewList[idx] if fromView == toView { return } } if menuStyle.lineHeight != 0 { refreshBottomLineFrame(fromView.frame, toView.frame, progress) } if menuStyle.lineColorGradual { refreshTitleViewColor(fromView, toView, progress) } if progress == 1 { refreshTitleViewState(fromView, toView) } } private func refreshBottomLineFrame(_ from: CGRect, _ to: CGRect, _ progress: CGFloat) { var from = from var to = to let lineHeight = menuStyle.lineHeight let lineWidth = menuStyle.lineWidth let y = frame.height - lineHeight let isFixedLineWidth = lineWidth != 0 let fromWidth = isFixedLineWidth ? lineWidth : from.width let toWidth = isFixedLineWidth ? lineWidth : to.width let fromMinX = isFixedLineWidth ? from.midX - lineWidth * 0.5 : from.minX let fromMaxX = isFixedLineWidth ? fromMinX + lineWidth : from.maxX let toMinX = isFixedLineWidth ? to.midX - lineWidth * 0.5 : to.minX let toMaxX = isFixedLineWidth ? toMinX + lineWidth : to.maxX from = CGRect(x: fromMinX, y: y, width: fromWidth, height: lineHeight) to = CGRect(x: toMinX, y: y, width: toWidth, height: lineHeight) let isToLeft = toMinX < fromMinX if progress < 0.5 { if isToLeft { let offsetWidth = (fromMinX - toMinX) * 2 * progress lineView.frame = CGRect(x: fromMinX - offsetWidth, y: y, width: fromMaxX - fromMinX + offsetWidth, height: lineHeight) } else { let offsetWidth = (toMaxX - fromMaxX) * 2 * progress lineView.frame = CGRect(x: fromMinX, y: y, width:from.width + offsetWidth, height: lineHeight) } } else { if isToLeft { let offsetWidth = (fromMaxX - to.maxX) * (1 - (progress - 0.5) * 2) lineView.frame = CGRect(x: toMinX, y: y, width:to.width + offsetWidth, height: lineHeight) } else { let offsetWidth = (toMinX - fromMinX) * (1 - (progress - 0.5) * 2) lineView.frame = CGRect(x: toMinX - offsetWidth, y: y, width:toMaxX - toMinX + offsetWidth, height: lineHeight) } } } private func refreshTitleViewState(_ fromView: PageTitleView, _ toView: PageTitleView) { currentSelectedView?.isSelected = false toView.isSelected = true currentSelectedView = toView let width = scrollView.bounds.width let contentWidth = scrollView.contentSize.width var offsetX = toView.center.x - width * 0.5 offsetX = max(offsetX, 0) offsetX = min(contentWidth - width, offsetX) if contentWidth <= width { let viewWidth = CGFloat(titleViewList.last?.frame.maxX ?? 0) - CGFloat(titleViewList.first?.frame.minX ?? 0) offsetX = -(width - viewWidth) * 0.5 + menuStyle.margin } scrollView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true) if delegate != nil { delegate?.pageTitleDidSelected(pageTitle: self, pageTitleView: toView) } } private func refreshTitleViewColor(_ fromView: PageTitleView, _ toView: PageTitleView, _ progress: CGFloat) { let toRGB = UIColor.rgbValue(menuStyle.selectedColor) let fromRGB = UIColor.rgbValue(menuStyle.defaultColor) let deltaRGB = (toRGB.0 - fromRGB.0, toRGB.1 - fromRGB.1, toRGB.2 - fromRGB.2) fromView.color = UIColor(r: toRGB.0 - deltaRGB.0 * progress, g: toRGB.1 - deltaRGB.1 * progress, b: toRGB.2 - deltaRGB.2 * progress) toView.color = UIColor(r: fromRGB.0 + deltaRGB.0 * progress, g: fromRGB.1 + deltaRGB.1 * progress, b: fromRGB.2 + deltaRGB.2 * progress) } }
extension PageTitle : PageContentDelegate { func pageContentDidChange(pageContent: PageContent, targetIndex: Int, progress: CGFloat) { changeToSelectedIndex(idx: targetIndex, progress: progress) } }
|